Completed
Push — master ( e607fe...66e77b )
by Daniele
02:36
created
source/FluidXml.php 3 patches
Doc Comments   +21 added lines, -5 removed lines patch added patch discarded remove patch
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
  *
62 62
  * @param array $arguments Options that influence the construction of the XML document.
63 63
  *
64
- * @return FluidXml A new FluidXml instance.
64
+ * @return FluidContext A new FluidXml instance.
65 65
  */
66 66
 function fluidify(...$arguments)
67 67
 {
@@ -140,12 +140,16 @@  discard block
 block discarded – undo
140 140
          * $xml->query("/doc")->query("book[@id='123']");
141 141
          * ```
142 142
          *
143
-         * @param string $xpath The XPath to execute.
143
+         * @param string[] $xpath The XPath to execute.
144 144
          *
145 145
          * @return FluidContext The context associated to the DOMNodeList.
146 146
          */
147 147
         public function query(...$xpath);
148 148
         public function times($times, callable $fn = null);
149
+
150
+        /**
151
+         * @return FluidContext
152
+         */
149 153
         public function each(callable $fn);
150 154
 
151 155
         /**
@@ -162,9 +166,6 @@  discard block
 block discarded – undo
162 166
          * ```
163 167
          *
164 168
          * @param string|array $child The child/children to add.
165
-         * @param string $value The child text content.
166
-         * @param bool $switchContext Whether to return the current context
167
-         *                            or the context of the created node.
168 169
          *
169 170
          * @return FluidContext The context associated to the DOMNodeList.
170 171
          */
@@ -179,6 +180,10 @@  discard block
 block discarded – undo
179 180
         public function remove(...$xpath);
180 181
         public function xml($strip = false);
181 182
         // Aliases:
183
+
184
+        /**
185
+         * @return FluidContext
186
+         */
182 187
         public function add($child, ...$optionals);
183 188
         public function prepend($sibling, ...$optionals);
184 189
         public function insertSiblingBefore($sibling, ...$optionals);
@@ -513,6 +518,10 @@  discard block
 block discarded – undo
513 518
                 return new FluidContext($context, $this->namespaces);
514 519
         }
515 520
 
521
+        /**
522
+         * @param FluidContext $help_context
523
+         * @param FluidContext $new_context
524
+         */
516 525
         protected function chooseContext($help_context, $new_context)
517 526
         {
518 527
                 // If the two contextes are diffent, the user has requested
@@ -1014,6 +1023,10 @@  discard block
 block discarded – undo
1014 1023
                 throw new \Exception('XML document not supported.');
1015 1024
         }
1016 1025
 
1026
+        /**
1027
+         * @param string $name
1028
+         * @param string $value
1029
+         */
1017 1030
         protected function createElement($name, $value = null)
1018 1031
         {
1019 1032
                 // The DOMElement instance must be different for every node,
@@ -1342,6 +1355,9 @@  discard block
 block discarded – undo
1342 1355
         private $context;
1343 1356
         private $times;
1344 1357
 
1358
+        /**
1359
+         * @param FluidContext $context
1360
+         */
1345 1361
         public function __construct($context, $times)
1346 1362
         {
1347 1363
                 $this->context = $context;
Please login to merge, or discard this patch.
Indentation   +26 added lines, -28 removed lines patch added patch discarded remove patch
@@ -133,7 +133,6 @@  discard block
 block discarded – undo
133 133
          *
134 134
          * ```php
135 135
          * $xml = fluidxml();
136
-
137 136
          * $xml->query("/doc/book[@id='123']");
138 137
          *
139 138
          * // Relative queries are valid.
@@ -153,7 +152,6 @@  discard block
 block discarded – undo
153 152
          *
154 153
          * ```php
155 154
          * $xml = fluidxml();
156
-
157 155
          * $xml->appendChild('title', 'The Theory Of Everything');
158 156
          * $xml->appendChild([ 'author' => 'S. Hawking' ]);
159 157
          *
@@ -268,9 +266,9 @@  discard block
 block discarded – undo
268 266
 class FluidXml implements FluidInterface
269 267
 {
270 268
         use FluidNamespaceTrait,
271
-            NewableTrait,
272
-            ReservedCallTrait,          // For compatibility with PHP 5.6.
273
-            ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
269
+                NewableTrait,
270
+                ReservedCallTrait,          // For compatibility with PHP 5.6.
271
+                ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
274 272
 
275 273
         const ROOT_NODE = 'doc';
276 274
 
@@ -301,9 +299,9 @@  discard block
 block discarded – undo
301 299
         public function __construct($root = null, $options = [])
302 300
         {
303 301
                 $defaults = [ 'root'       => self::ROOT_NODE,
304
-                              'version'    => '1.0',
305
-                              'encoding'   => 'UTF-8',
306
-                              'stylesheet' => null ];
302
+                                'version'    => '1.0',
303
+                                'encoding'   => 'UTF-8',
304
+                                'stylesheet' => null ];
307 305
 
308 306
                 if (\is_string($root)) {
309 307
                         // The root option can be specified as first argument
@@ -327,9 +325,9 @@  discard block
 block discarded – undo
327 325
 
328 326
                 if ($opts['stylesheet']) {
329 327
                         $attrs = 'type="text/xsl" '
330
-                               . "encoding=\"{$opts['encoding']}\" "
331
-                               . 'indent="yes" '
332
-                               . "href=\"{$opts['stylesheet']}\"";
328
+                                . "encoding=\"{$opts['encoding']}\" "
329
+                                . 'indent="yes" '
330
+                                . "href=\"{$opts['stylesheet']}\"";
333 331
                         $stylesheet = new \DOMProcessingInstruction('xml-stylesheet', $attrs);
334 332
 
335 333
                         $this->dom->insertBefore($stylesheet, $this->dom->documentElement);
@@ -528,9 +526,9 @@  discard block
 block discarded – undo
528 526
 class FluidContext implements FluidInterface, \ArrayAccess, \Iterator
529 527
 {
530 528
         use FluidNamespaceTrait,
531
-            NewableTrait,
532
-            ReservedCallTrait,          // For compatibility with PHP 5.6.
533
-            ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
529
+                NewableTrait,
530
+                ReservedCallTrait,          // For compatibility with PHP 5.6.
531
+                ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
534 532
 
535 533
         private $dom;
536 534
         private $nodes = [];
@@ -990,18 +988,18 @@  discard block
 block discarded – undo
990 988
         protected function handleInsertion(...$arguments)
991 989
         {
992 990
                 $check_sequence = [ 'specialContentHandler',
993
-                                    'specialAttributeHandler',
994
-                                    'stringStringHandler',
995
-                                    'stringMixedHandler',
996
-                                    'integerArrayHandler',
997
-                                    'integerStringNotXmlHandler',
998
-                                    'integerXmlHandler',
999
-                                    'integerDomdocumentHandler',
1000
-                                    'integerDomnodelistHandler',
1001
-                                    'integerDomnodeHandler',
1002
-                                    'integerSimplexmlHandler',
1003
-                                    'integerFluidxmlHandler',
1004
-                                    'integerFluidcontextHandler' ];
991
+                                        'specialAttributeHandler',
992
+                                        'stringStringHandler',
993
+                                        'stringMixedHandler',
994
+                                        'integerArrayHandler',
995
+                                        'integerStringNotXmlHandler',
996
+                                        'integerXmlHandler',
997
+                                        'integerDomdocumentHandler',
998
+                                        'integerDomnodelistHandler',
999
+                                        'integerDomnodeHandler',
1000
+                                        'integerSimplexmlHandler',
1001
+                                        'integerFluidxmlHandler',
1002
+                                        'integerFluidcontextHandler' ];
1005 1003
 
1006 1004
                 foreach ($check_sequence as $check) {
1007 1005
                         $ret = $this->$check(...$arguments);
@@ -1273,8 +1271,8 @@  discard block
 block discarded – undo
1273 1271
         const MODE_EXPLICIT = 1;
1274 1272
 
1275 1273
         private $config = [ self::ID   => '',
1276
-                            self::URI  => '',
1277
-                            self::MODE => self::MODE_EXPLICIT ];
1274
+                                self::URI  => '',
1275
+                                self::MODE => self::MODE_EXPLICIT ];
1278 1276
 
1279 1277
         public function __construct($id, $uri, $mode = 1)
1280 1278
         {
Please login to merge, or discard this patch.
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 
81 81
 function is_an_xml_string($string)
82 82
 {
83
-        if (! \is_string($string)) {
83
+        if (!\is_string($string)) {
84 84
                 return false;
85 85
         }
86 86
 
@@ -244,7 +244,7 @@  discard block
 block discarded – undo
244 244
                 $namespaces = [];
245 245
 
246 246
                 if (\is_string($arguments[0])) {
247
-                        $args = [ $arguments[0], $arguments[1] ];
247
+                        $args = [$arguments[0], $arguments[1]];
248 248
 
249 249
                         if (isset($arguments[2])) {
250 250
                                 $args[] = $arguments[2];
@@ -269,8 +269,8 @@  discard block
 block discarded – undo
269 269
 {
270 270
         use FluidNamespaceTrait,
271 271
             NewableTrait,
272
-            ReservedCallTrait,          // For compatibility with PHP 5.6.
273
-            ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
272
+            ReservedCallTrait, // For compatibility with PHP 5.6.
273
+            ReservedCallStaticTrait; // For compatibility with PHP 5.6.
274 274
 
275 275
         const ROOT_NODE = 'doc';
276 276
 
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
 
279 279
         public static function load($document)
280 280
         {
281
-                if (\is_string($document) && ! is_an_xml_string($document)) {
281
+                if (\is_string($document) && !is_an_xml_string($document)) {
282 282
                         // Removes any empty new line at the beginning,
283 283
                         // otherwise the first character check fails.
284 284
 
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
                                 $document = \file_get_contents($file);
291 291
                         }
292 292
 
293
-                        if (! $is_file || ! $is_readable || ! $document) {
293
+                        if (!$is_file || !$is_readable || !$document) {
294 294
                                 throw new \Exception("File '$file' not accessible.");
295 295
                         }
296 296
                 }
@@ -300,10 +300,10 @@  discard block
 block discarded – undo
300 300
 
301 301
         public function __construct($root = null, $options = [])
302 302
         {
303
-                $defaults = [ 'root'       => self::ROOT_NODE,
303
+                $defaults = ['root'       => self::ROOT_NODE,
304 304
                               'version'    => '1.0',
305 305
                               'encoding'   => 'UTF-8',
306
-                              'stylesheet' => null ];
306
+                              'stylesheet' => null];
307 307
 
308 308
                 if (\is_string($root)) {
309 309
                         // The root option can be specified as first argument
@@ -497,14 +497,14 @@  discard block
 block discarded – undo
497 497
 
498 498
         protected function newContext($context = null)
499 499
         {
500
-                if (! $context) {
500
+                if (!$context) {
501 501
                         $context = $this->dom->documentElement;
502 502
                 }
503 503
 
504 504
                 // If the user has requested ['root' => null] at construction time
505 505
                 // the 'documentElement' property is null because we have not created
506 506
                 // a root node yet.
507
-                if (! $context) {
507
+                if (!$context) {
508 508
                         // Whether there is not a root node, the DOMDocument is
509 509
                         // promoted as root node.
510 510
                         $context = $this->dom;
@@ -529,8 +529,8 @@  discard block
 block discarded – undo
529 529
 {
530 530
         use FluidNamespaceTrait,
531 531
             NewableTrait,
532
-            ReservedCallTrait,          // For compatibility with PHP 5.6.
533
-            ReservedCallStaticTrait;    // For compatibility with PHP 5.6.
532
+            ReservedCallTrait, // For compatibility with PHP 5.6.
533
+            ReservedCallStaticTrait; // For compatibility with PHP 5.6.
534 534
 
535 535
         private $dom;
536 536
         private $nodes = [];
@@ -538,8 +538,8 @@  discard block
 block discarded – undo
538 538
 
539 539
         public function __construct($context, array $namespaces = [])
540 540
         {
541
-                if (! \is_array($context)) {
542
-                        $context = [ $context ];
541
+                if (!\is_array($context)) {
542
+                        $context = [$context];
543 543
                 }
544 544
 
545 545
                 foreach ($context as $n) {
@@ -560,7 +560,7 @@  discard block
 block discarded – undo
560 560
                         }
561 561
                 }
562 562
 
563
-                if (! empty($namespaces)) {
563
+                if (!empty($namespaces)) {
564 564
                         $this->namespace(...\array_values($namespaces));
565 565
                 }
566 566
         }
@@ -689,7 +689,7 @@  discard block
 block discarded – undo
689 689
                                 }
690 690
                         }
691 691
 
692
-                        if (! $found) {
692
+                        if (!$found) {
693 693
                                 $unique_results[] = $r;
694 694
                         }
695 695
                 }
@@ -744,7 +744,7 @@  discard block
 block discarded – undo
744 744
         // appendChild($child, $value?, $attributes? = [], $switchContext? = false)
745 745
         public function appendChild($child, ...$optionals)
746 746
         {
747
-                return $this->insertElement($child, $optionals, [ static::class, 'doAppendChild' ]);
747
+                return $this->insertElement($child, $optionals, [static::class, 'doAppendChild']);
748 748
         }
749 749
 
750 750
         // Alias of appendChild().
@@ -760,7 +760,7 @@  discard block
 block discarded – undo
760 760
 
761 761
         public function prependSibling($sibling, ...$optionals)
762 762
         {
763
-                return $this->insertElement($sibling, $optionals, [ static::class, 'doPrependSibling' ]);
763
+                return $this->insertElement($sibling, $optionals, [static::class, 'doPrependSibling']);
764 764
         }
765 765
 
766 766
         // Alias of prependSibling().
@@ -783,7 +783,7 @@  discard block
 block discarded – undo
783 783
 
784 784
         public function appendSibling($sibling, ...$optionals)
785 785
         {
786
-                return $this->insertElement($sibling, $optionals, [ static::class, 'doAppendSibling' ]);
786
+                return $this->insertElement($sibling, $optionals, [static::class, 'doAppendSibling']);
787 787
         }
788 788
 
789 789
         // Alias of appendSibling().
@@ -811,7 +811,7 @@  discard block
 block discarded – undo
811 811
                 // the user has passed two arguments:
812 812
                 // 1. is the attribute name
813 813
                 // 2. is the attribute value
814
-                if (! \is_array($arguments[0])) {
814
+                if (!\is_array($arguments[0])) {
815 815
                         $attrs = [$arguments[0] => $arguments[1]];
816 816
                 }
817 817
 
@@ -935,8 +935,8 @@  discard block
 block discarded – undo
935 935
 
936 936
         protected function insertElement($element, array $optionals, callable $fn)
937 937
         {
938
-                if (! \is_array($element)) {
939
-                        $element = [ $element ];
938
+                if (!\is_array($element)) {
939
+                        $element = [$element];
940 940
                 }
941 941
 
942 942
                 $switch_context = false;
@@ -976,7 +976,7 @@  discard block
 block discarded – undo
976 976
                 // offers to the user and is the same of:
977 977
                 // 1. appending a child switching the context
978 978
                 // 2. setting the attributes over the new context.
979
-                if (! empty($attributes)) {
979
+                if (!empty($attributes)) {
980 980
                         $context->setAttribute($attributes);
981 981
                 }
982 982
 
@@ -989,7 +989,7 @@  discard block
 block discarded – undo
989 989
 
990 990
         protected function handleInsertion(...$arguments)
991 991
         {
992
-                $check_sequence = [ 'specialContentHandler',
992
+                $check_sequence = ['specialContentHandler',
993 993
                                     'specialAttributeHandler',
994 994
                                     'stringStringHandler',
995 995
                                     'stringMixedHandler',
@@ -1001,7 +1001,7 @@  discard block
 block discarded – undo
1001 1001
                                     'integerDomnodeHandler',
1002 1002
                                     'integerSimplexmlHandler',
1003 1003
                                     'integerFluidxmlHandler',
1004
-                                    'integerFluidcontextHandler' ];
1004
+                                    'integerFluidcontextHandler'];
1005 1005
 
1006 1006
                 foreach ($check_sequence as $check) {
1007 1007
                         $ret = $this->$check(...$arguments);
@@ -1048,8 +1048,8 @@  discard block
 block discarded – undo
1048 1048
 
1049 1049
         protected function attachNodes($parent, $nodes, $fn)
1050 1050
         {
1051
-                if (! \is_array($nodes) && ! $nodes instanceof \Traversable) {
1052
-                        $nodes = [ $nodes ];
1051
+                if (!\is_array($nodes) && !$nodes instanceof \Traversable) {
1052
+                        $nodes = [$nodes];
1053 1053
                 }
1054 1054
 
1055 1055
                 $context = [];
@@ -1064,7 +1064,7 @@  discard block
 block discarded – undo
1064 1064
 
1065 1065
         protected function specialContentHandler($parent, $k, $v, $optionals, $fn)
1066 1066
         {
1067
-                if (! \is_string($k) || $k !== '@'|| ! \is_string($v)) {
1067
+                if (!\is_string($k) || $k !== '@' || !\is_string($v)) {
1068 1068
                         return false;
1069 1069
                 }
1070 1070
 
@@ -1085,7 +1085,7 @@  discard block
 block discarded – undo
1085 1085
 
1086 1086
         protected function specialAttributeHandler($parent, $k, $v, $optionals, $fn)
1087 1087
         {
1088
-                if (! \is_string($k) || $k[0] !== '@' || ! \is_string($v)) {
1088
+                if (!\is_string($k) || $k[0] !== '@' || !\is_string($v)) {
1089 1089
                         return false;
1090 1090
                 }
1091 1091
 
@@ -1100,7 +1100,7 @@  discard block
 block discarded – undo
1100 1100
 
1101 1101
         protected function stringStringHandler($parent, $k, $v, $optionals, $fn)
1102 1102
         {
1103
-                if (! \is_string($k) || ! \is_string($v)) {
1103
+                if (!\is_string($k) || !\is_string($v)) {
1104 1104
                         return false;
1105 1105
                 }
1106 1106
 
@@ -1110,12 +1110,12 @@  discard block
 block discarded – undo
1110 1110
                 $el = $this->createElement($k, $v);
1111 1111
                 $el = \call_user_func($fn, $parent, $el);
1112 1112
 
1113
-                return [ $el ];
1113
+                return [$el];
1114 1114
         }
1115 1115
 
1116 1116
         protected function stringMixedHandler($parent, $k, $v, $optionals, $fn)
1117 1117
         {
1118
-                if (! \is_string($k) || \is_string($v)) {
1118
+                if (!\is_string($k) || \is_string($v)) {
1119 1119
                         return false;
1120 1120
                 }
1121 1121
 
@@ -1130,12 +1130,12 @@  discard block
 block discarded – undo
1130 1130
                 // they are supplied, so 'appendChild' is the perfect operation.
1131 1131
                 $this->newContext($el)->appendChild($v, ...$optionals);
1132 1132
 
1133
-                return [ $el ];
1133
+                return [$el];
1134 1134
         }
1135 1135
 
1136 1136
         protected function integerArrayHandler($parent, $k, $v, $optionals, $fn)
1137 1137
         {
1138
-                if (! \is_int($k) || ! \is_array($v)) {
1138
+                if (!\is_int($k) || !\is_array($v)) {
1139 1139
                         return false;
1140 1140
                 }
1141 1141
 
@@ -1155,7 +1155,7 @@  discard block
 block discarded – undo
1155 1155
 
1156 1156
         protected function integerStringNotXmlHandler($parent, $k, $v, $optionals, $fn)
1157 1157
         {
1158
-                if (! \is_int($k) || ! \is_string($v) || is_an_xml_string($v)) {
1158
+                if (!\is_int($k) || !\is_string($v) || is_an_xml_string($v)) {
1159 1159
                         return false;
1160 1160
                 }
1161 1161
 
@@ -1165,12 +1165,12 @@  discard block
 block discarded – undo
1165 1165
                 $el = $this->createElement($v);
1166 1166
                 $el = \call_user_func($fn, $parent, $el);
1167 1167
 
1168
-                return [ $el ];
1168
+                return [$el];
1169 1169
         }
1170 1170
 
1171 1171
         protected function integerXmlHandler($parent, $k, $v, $optionals, $fn)
1172 1172
         {
1173
-                if (! \is_int($k) || ! is_an_xml_string($v)) {
1173
+                if (!\is_int($k) || !is_an_xml_string($v)) {
1174 1174
                         return false;
1175 1175
                 }
1176 1176
 
@@ -1204,7 +1204,7 @@  discard block
 block discarded – undo
1204 1204
 
1205 1205
         protected function integerDomdocumentHandler($parent, $k, $v, $optionals, $fn)
1206 1206
         {
1207
-                if (! \is_int($k) || ! $v instanceof \DOMDocument) {
1207
+                if (!\is_int($k) || !$v instanceof \DOMDocument) {
1208 1208
                         return false;
1209 1209
                 }
1210 1210
 
@@ -1219,7 +1219,7 @@  discard block
 block discarded – undo
1219 1219
 
1220 1220
         protected function integerDomnodelistHandler($parent, $k, $v, $optionals, $fn)
1221 1221
         {
1222
-                if (! \is_int($k) || ! $v instanceof \DOMNodeList) {
1222
+                if (!\is_int($k) || !$v instanceof \DOMNodeList) {
1223 1223
                         return false;
1224 1224
                 }
1225 1225
 
@@ -1228,7 +1228,7 @@  discard block
 block discarded – undo
1228 1228
 
1229 1229
         protected function integerDomnodeHandler($parent, $k, $v, $optionals, $fn)
1230 1230
         {
1231
-                if (! \is_int($k) || ! $v instanceof \DOMNode) {
1231
+                if (!\is_int($k) || !$v instanceof \DOMNode) {
1232 1232
                         return false;
1233 1233
                 }
1234 1234
 
@@ -1237,7 +1237,7 @@  discard block
 block discarded – undo
1237 1237
 
1238 1238
         protected function integerSimplexmlHandler($parent, $k, $v, $optionals, $fn)
1239 1239
         {
1240
-                if (! \is_int($k) || ! $v instanceof \SimpleXMLElement) {
1240
+                if (!\is_int($k) || !$v instanceof \SimpleXMLElement) {
1241 1241
                         return false;
1242 1242
                 }
1243 1243
 
@@ -1246,7 +1246,7 @@  discard block
 block discarded – undo
1246 1246
 
1247 1247
         protected function integerFluidxmlHandler($parent, $k, $v, $optionals, $fn)
1248 1248
         {
1249
-                if (! \is_int($k) || ! $v instanceof FluidXml) {
1249
+                if (!\is_int($k) || !$v instanceof FluidXml) {
1250 1250
                         return false;
1251 1251
                 }
1252 1252
 
@@ -1255,7 +1255,7 @@  discard block
 block discarded – undo
1255 1255
 
1256 1256
         protected function integerFluidcontextHandler($parent, $k, $v, $optionals, $fn)
1257 1257
         {
1258
-                if (! \is_int($k) || ! $v instanceof FluidContext) {
1258
+                if (!\is_int($k) || !$v instanceof FluidContext) {
1259 1259
                         return false;
1260 1260
                 }
1261 1261
 
@@ -1265,16 +1265,16 @@  discard block
 block discarded – undo
1265 1265
 
1266 1266
 class FluidNamespace
1267 1267
 {
1268
-        const ID   = 'id'  ;
1269
-        const URI  = 'uri' ;
1268
+        const ID   = 'id';
1269
+        const URI  = 'uri';
1270 1270
         const MODE = 'mode';
1271 1271
 
1272 1272
         const MODE_IMPLICIT = 0;
1273 1273
         const MODE_EXPLICIT = 1;
1274 1274
 
1275
-        private $config = [ self::ID   => '',
1275
+        private $config = [self::ID   => '',
1276 1276
                             self::URI  => '',
1277
-                            self::MODE => self::MODE_EXPLICIT ];
1277
+                            self::MODE => self::MODE_EXPLICIT];
1278 1278
 
1279 1279
         public function __construct($id, $uri, $mode = 1)
1280 1280
         {
Please login to merge, or discard this patch.