Quiq/Zugferd/Tree.pm

filter: Statementstmt 28.4% 23 / 81 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 7 / 14 Podpod 100% 5 / 5 totaltotal 25.1% 35 / 139 CC 18 SCAR 37.5
CC
18
Cov
20%
resolvePlaceholders
110.0
__ANON__
30.0
__ANON__
12.0
CRAP 42.6
« Quiq/Section/Parser.pm ↑ Summary Quiq/Template.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Zugferd::Tree - Operatonen auf ZUGFeRD-Baum
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein ZUGFeRD-Baum ist die Repräsentation von ZUGFeRD-XML in Form einer
16 Perl-Datenstruktur. Diese Repräsentation wird genutzt, um die XML-Struktur
17 geeignet bearbeiten zu können.
18  
19 =cut
20  
21 # -----------------------------------------------------------------------------
22  
23 package Quiq::Zugferd::Tree;
24 2 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 our $VERSION = '1.240';
31  
32 2 ▶use Quiq::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Quiq::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Quiq::AnsiColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # -----------------------------------------------------------------------------
37  
38 =head1 METHODS
39  
40 =head2 Klassenmethoden
41  
42 =head3 new() - Konstruktor
43  
44 =head4 Synopsis
45  
46 $ztr = $class->new($ref);
47  
48 =head4 Description
49  
50 Instantiiere einen ZUGFeRD-Baum und liefere eine Referenz auf dieses
51 Objekt zurück.
52  
53 =cut
54  
55 # -----------------------------------------------------------------------------
56  
57 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
58 0 my ($class,$ref) = @_;
59 0 return bless $ref,$class;
60 }
61  
62 # -----------------------------------------------------------------------------
63  
64 =head2 Objektmethoden
65  
66 =head3 getSubTree() - Liefere Mehrfach-Element
67  
68 =head4 Synopsis
69  
70 $ztr = $ztr->getSubTree($keyPath,$placeholder)
71  
72 =head4 Arguments
73  
74 =over 4
75  
76 =item $keyPath
77  
78 Pfad zu Array
79  
80 =item $placeholder
81  
82 Name des Platzhalters
83  
84 =back
85  
86 =head4 Description
87  
88 Liefere die Struktur, die das erste Element des Arrays iat, das
89 $keyPath referenziert, und ersetze die Referenz durch den Platzhalter
90 $placeholder.
91  
92 =cut
93  
94 # -----------------------------------------------------------------------------
95  
96 0 ▶sub getSubTree {
Subroutine getSubTree: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($self,$keyPath,$placeholder) = @_;
98  
99 0 my $tree = $self->getDeep($keyPath)->[0];
100 0 $self->setDeep($keyPath,$placeholder);
101  
102 0 return bless $tree,'Quiq::Zugferd::Tree';
103 }
104  
105 # -----------------------------------------------------------------------------
106  
107 =head3 processSubTree() - Verarbeite Subbaum
108  
109 =head4 Synopsis
110  
111 $treeA = $ztr->processSubTree($path,$placeholder,\@arr,sub {
112 my ($ztr,$h,$i) = @_;
113 ...
114 $t->resolvePlaceholders(
115 @keyVal
116 );
117
118 return $t;
119 });
120  
121 =head4 Arguments
122  
123 =over 4
124  
125 =item $path
126  
127 Pfad zum Subbaum
128  
129 =item $placeholder
130  
131 Platzhalter, der in den Baum $ztr unter dem Pfad $path eingesetzt wird.
132  
133 =item @arr
134  
135 Liste der Elemente, aus denen die Platzhalter im Subbaum
136 ersetzt werden.
137  
138 =item sub {}
139  
140 Subroutine, die die Einsetzung in einen Subbaum vornimmt
141  
142 =back
143  
144 =head4 Returns
145  
146 (Object) (Sub-)Baum mit ersetzen Platzhaltern
147  
148 =head4 Description
149  
150 Ersetze im Subbaum $name die Platzhalter aus den Elementen von @arr.
151  
152 =cut
153  
154 # -----------------------------------------------------------------------------
155  
156 0 ▶sub processSubTree {
Subroutine processSubTree: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self,$path,$placeholder,$arr,$sub) = @_;
158  
159 0 my $t0 = $self->getSubTree($path,$placeholder);
160  
161 0 my $i = 0;
162 0 my @arr;
163 0 for my $e (@$arr) {
164 0 my $t = Quiq::Storable->clone($t0);
165 0 push @arr,$sub->($self,$t,$e,$i++);
166 }
167  
168 0 return \@arr;
169 }
170  
171 # -----------------------------------------------------------------------------
172  
173 =head3 reduceTree() - Reduziere den Baum
174  
175 =head4 Synopsis
176  
177 $ztr = $ztr->reduceTree;
178 $ztr = $ztr->reduceTree($sub);
179  
180 =head4 Arguments
181  
182 =over 4
183  
184 =item $sub
185  
186 Referenz auf Subroutine, die unaufgelöste Werte entfernt. Default:
187  
188 sub {
189 my $val = shift;
190 if (defined $val && $val =~ /^__\w+__$/) {
191 $val = undef;
192 }
193 return $val;
194 }
195  
196 =back
197  
198 =head4 Description
199  
200 Reduziere den ZUGFeRD-Baum auf ein Minumum, d.h.
201  
202 =over 2
203  
204 =item *
205  
206 Entferne alle Knoten mit unaufgelösten Werten
207  
208 =item *
209  
210 Entferne alle leeren Knoten
211  
212 =back
213  
214 =cut
215  
216 # -----------------------------------------------------------------------------
217  
218 0 ▶sub reduceTree {
Subroutine reduceTree: not called CC=2 SCAR=17.9
Pod documented
219 0 my ($self,$sub) = @_;
220  
221 # Entferne Knoten mit unaufgelösten Werten (Default: ___XXX__ Platzhalter)
222  
223 0 ▶ $sub //= Quiq::Tree->setLeafValue($self,sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
224 0 my $val = shift;
225 # if (defined $val && $val =~ /^__\w+__$/) {
226 0 ▶ if (defined $val && $val =~ /^B[GT]-.*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val =~ /^B[GT]-.*$/) 0 0 0
Condition0%0/3
defined $val and $val =~ /^B[GT]-.*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and $val =~ /^B[GT]-.*$/
defined $val $val =~ /^B[GT]-.*$/
Truth table0%0/3
A and B
A= defined $valB= $val =~ /^B[GT]-.*$/
ABresult
0X0
100
111
227 0 $val = undef;
228 }
229 0 return $val;
230 0 ▶ });
Condition0%0/3
$sub //= "Quiq::Tree"->setLeafValue($self, sub { my $val = shift(); if (defined $val and $val =~ /^B[GT]-.*$/) { $val = undef; } ; return $val; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$sub //= "Quiq::Tree"->setLeafValue($self, sub { my $val = shift(); if (defined $val and $val =~ /^B[GT]-.*$/) { $val = undef; } ; return $val; } )
$sub "Quiq::Tree"->setLeafValue($self, sub { my $val = shift(); if (defined $val and $val =~ /^B[GT]-.*$/) { $val = undef; } ; return $val; } )
Truth table0%0/3
A //= B
A= $subB= "Quiq::Tree"->setLeafValue($self, sub { my $val = shift(); if (defined $val and $val =~ /^B[GT]-.*$/) { $val = undef; } ; return $val; } )
ABresult
1X1
011
000
231  
232 # Entferne alle leeren Knoten
233 0 Quiq::Tree->removeEmptyNodes($self);
234  
235 0 return $self;
236 }
237  
238 # -----------------------------------------------------------------------------
239  
240 =head3 resolvePlaceholders() - Ersetze Platzhalter
241  
242 =head4 Synopsis
243  
244 $ztr->resolvePlaceholders(@keyVal,%options);
245  
246 =head4 Arguments
247  
248 =over 4
249  
250 =item @keyVal
251  
252 Liste der Platzhalter und ihrer Werte
253  
254 =back
255  
256 =head4 Options
257  
258 =over 4
259  
260 =item -label => $text (Default: '')
261  
262 Versieh den Abschnitt der Platzhalter (bei -showPlaceHolders=>1) mit
263 der Beschriftung $label.
264  
265 =item -showPlaceholders => $bool (Default: 0)
266  
267 Gibt die Liste der Platzhalter auf STDOUT aus
268  
269 =back
270  
271 =head4 Description
272  
273 Durchlaufe den ZUGFeRD-Baum rekursiv und ersetze auf den Blattknoten
274 die Keys durch ihre Werte. Blattknoten-Werte, die unter den Keys
275 nicht vorkommen, werden auf C<undef> gesetzt (und ggf. später durch
276 reduceTree() entfernt).
277  
278 Fehlt einer der Platzhalter (key) im Baum oder kommen Platzhalter
279 mehrfach vor, wird eine Exception geworfen.
280  
281 =cut
282  
283 # -----------------------------------------------------------------------------
284  
285 2 my $a = Quiq::AnsiColor->new(1);
286  
287 0 ▶sub resolvePlaceholders {
Subroutine resolvePlaceholders: not called CC=10 SCAR=47.0
Pod documented
288 0 my $self = shift;
289 # @_: $zug,@keyVal,%options
290  
291 # Optionen und Argumente
292
293 0 my $label = '';
294 0 my $showPlaceholders = 0;
295
296 0 my $argA = $self->parameters(0,undef,\@_,
297 -label => \$label,
298 -showPlaceholders => \$showPlaceholders,
299 );
300 # @$argA;
301
302 0 ▶ if ($showPlaceholders) {
Branch: 1 branch
BranchTrueFalseTotal
if ($showPlaceholders) 0 0 0
303 0 say "--$label--";
304 0 ▶ for (my $i = 0; $i < @$argA; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$argA) 0 0 0
305 0 my $key = $argA->[$i];
306 0 my $val = $argA->[$i+1];
307 0 ▶ printf "%s = %s %s\n",$key,defined($val)? "'$val'": 'undef',
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
308 $a->str('dark green','?'); # $zug->bt($key)->text);
309 }
310 0 print "\n";
311 }
312  
313 # my %map = @_;
314 0 my %map = @$argA;
315  
316 # Operation ausführen
317  
318 # * Prüfe, dass kein Schlüssel doppelt vorkommen
319  
320 0 my %seen; # gesehene Platzhalter
321 my @arr; # doppelte Platzhalter
322 0 ▶ for (my $i = 0; $i < @_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
323 0 my $key = $_[$i];
324 0 ▶ if (exists $seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) 0 0 0
325 0 push @arr,$key;
326 }
327 0 $seen{$key} = 0;
328 }
329  
330 0 ▶ if (@arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arr) 0 0 0
331 0 $self->throw(
332 'TREE-00099: Duplicate placeholders',
333 Placeholders => join(', ',@arr),
334 );
335 }
336  
337 # Ersetze Platzhalter
338  
339 0 ▶ Quiq::Tree->setLeafValue($self,sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
340 0 my $val = shift; # akt. Knotenwert
341 0 ▶ if (defined $val) { # Wir ersetzen nur definierte Werte
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
342 0 ▶ if (exists $map{$val}) { # wir haben einen Platzhalter-Knoten
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map{$val}) 0 0 0
343 0 $seen{$val} = 1;
344 0 my $newVal = $map{$val}; # neuer Wert
345 # Wir setzen den neuen Wert nur, wenn er nicht leer ist
346 0 ▶ if (defined($newVal) && $newVal ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newVal and $newVal ne "") 0 0 0
Condition0%0/3
defined $newVal and $newVal ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $newVal and $newVal ne ""
defined $newVal $newVal ne ""
Truth table0%0/3
A and B
A= defined $newValB= $newVal ne ""
ABresult
0X0
100
111
347 0 return $newVal;
348 }
349 }
350 }
351 0 return undef;
352 0 });
353  
354 # Prüfe, dass wir alle Platzhalter ersetzt haben
355  
356 0 @arr = (); # nicht gefundene Platzhalter
357 0 for my $key (keys %map) {
358 0 ▶ if (!$seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$key}) 0 0 0
359 0 push @arr,$key;
360 }
361 }
362 0 ▶ if (@arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arr) 0 0 0
363 0 $self->throw(
364 'TREE-00099: Non-existent Placeholders',
365 Placeholders => join(', ',@arr),
366 );
367 }
368  
369 0 return;
370 }
371  
372 # -----------------------------------------------------------------------------
373  
374 =head1 VERSION
375  
376 1.240
377  
378 =head1 AUTHOR
379  
380 Frank Seitz, L<http://fseitz.de/>
381  
382 =head1 COPYRIGHT
383  
384 Copyright (C) 2026 Frank Seitz
385  
386 =head1 LICENSE
387  
388 This code is free software; you can redistribute it and/or modify
389 it under the same terms as Perl itself.
390  
391 =cut
392  
393 # -----------------------------------------------------------------------------
394  
395 2 1;
396  
397 # eof
« Quiq/Section/Parser.pm ↑ Summary Quiq/Template.pm »