├── COPYING ├── README.md ├── composer.json ├── composer.lock └── src ├── BufferedFileRead.php ├── BufferedFileReaders.php ├── DOMReadingIteration.php ├── XMLAttributeFilter.php ├── XMLAttributeFilterBase.php ├── XMLAttributeIterator.php ├── XMLAttributePreg.php ├── XMLBuild.php ├── XMLChildElementIterator.php ├── XMLChildIterator.php ├── XMLElementIterator.php ├── XMLElementXpathFilter.php ├── XMLNodeTypeFilter.php ├── XMLReaderAggregate.php ├── XMLReaderElement.php ├── XMLReaderFilterBase.php ├── XMLReaderIteration.php ├── XMLReaderIterator.php ├── XMLReaderNextIteration.php ├── XMLReaderNode.php ├── XMLSequenceStream.php ├── XMLSequenceStreamPath.php └── XMLWritingIteration.php /COPYING: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published by 637 | the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Iterators for [PHP `XMLReader`] for Ease of Parsing 2 | 3 | [PHP `XMLReader`]: http://php.net/XMLReader 4 | 5 | ### Change Log: 6 | 7 | - `0.1.12` maintenance release with fixes. 8 | 9 | - `0.1.11` maintenance release with fixes. added `XMLReader::CDATA` and 10 | `XMLReader::WHITESPACE` node support for `XMLWritingIteration`. added 11 | `XMLReaderItertor::skipNextRead()` for nested iterations. 12 | 13 | - `0.1.10` maintenance release with fixes. 14 | 15 | - `0.1.9` maintenance release with fixes. added `XMLReaderNode::expand()`. 16 | 17 | - `0.1.8` maintenance release with fixes. 18 | 19 | - `0.1.7` maintenance release with fixes. 20 | 21 | - `0.1.6` maintenance release with fixes. added xml-file-scanner 22 | command-line tool example. 23 | 24 | - `0.1.5` maintenance release with tests and new `XMLReaderNextIteration` 25 | to iterate in `XMLReader::next()` fashion. 26 | 27 | - `0.1.4` maintenance release with fixes. 28 | 29 | - `0.1.3` added `XMLSequenceStream`, a PHP stream wrapper to read XML from 30 | files which are a sequence of XML documents. Works transparently with 31 | `XMLReader`. 32 | 33 | - `0.1.2` added `XMLWritingIteration`, an iteration to write with 34 | `XMLWriter` from `XMLReader`. 35 | 36 | - `0.1.0` composer support has been added. 37 | 38 | - `0.0.23` first try of a compatibility layer for PHP installs with a libxml 39 | version below version 2.6.20. 40 | functions with compatibility checks are `XMLReaderNode::readOuterXml()` 41 | and `XMLReaderNode::readString()`. 42 | 43 | - `0.0.21` moved library into new repository and added 44 | `XMLReaderAggregate`. 45 | 46 | - `0.0.19` added `XMLElementXpathFilter`, a `FilterIterator` for 47 | `XMLReaderIterator` by an xpath expression. 48 | 49 | ~~~php 50 | $reader = new XMLReader(); 51 | $reader->open($xmlFile); 52 | $it = new XMLElementIterator($reader); 53 | 54 | $list = new XMLElementXpathFilter( 55 | $it, 56 | '//user[@id = "1" or @id = "6"]//message' 57 | ); 58 | 59 | foreach($list as $message) { 60 | echo " * ", $message->readString(), "\n"; 61 | } 62 | ~~~ 63 | 64 | ### Stackoverflow Q&A for the XMLReader Iterators 65 | 66 | the latest on top (for more examples, checkout the 67 | [`examples`] folder): 68 | 69 | - [Add Tag Element using XMLReader and SimpleXML or XMLWriter](https://stackoverflow.com/q/69455574/367456) 70 | - [How to distinguish between empty element and null-size string in DOMDocument?](http://stackoverflow.com/a/24109776/367456) 71 | - [PHP XML parser: How to read only part of the XML document?](http://stackoverflow.com/a/15443517/367456) 72 | - [Parse XML with PHP and XMLReader](http://stackoverflow.com/a/15351723/367456) 73 | - [Getting XML Attribute with XMLReader and PHP](http://stackoverflow.com/a/15399491/367456) 74 | 75 | [`examples`]: https://github.com/hakre/XMLReaderIterator/tree/master/examples 76 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hakre/xmlreaderiterator", 3 | "description": "Iterators for PHP XMLReader for ease of parsing", 4 | "keywords": ["XML", "XMLReader", "Iterator"], 5 | "homepage": "https://github.com/hakre/XMLReaderIterator", 6 | "minimum-stability": "stable", 7 | "license": "AGPL-3.0-or-later", 8 | "authors": [ 9 | { 10 | "name": "hakre", 11 | "homepage": "https://hakre.wordpress.com/" 12 | } 13 | ], 14 | "autoload": { 15 | "classmap": ["src/"] 16 | }, 17 | "require": { 18 | "ext-dom": "*", 19 | "ext-simplexml": "*", 20 | "ext-xmlreader": "*", 21 | "ext-xmlwriter": "*" 22 | }, 23 | "require-dev": { 24 | "ext-bz2": "*", 25 | "ext-libxml": "*", 26 | "phpunit/phpunit": "3.7.*" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "e54d33ad0eab5cbd4997fcd1fd5fb150", 8 | "packages": [], 9 | "packages-dev": [ 10 | { 11 | "name": "phpunit/php-code-coverage", 12 | "version": "1.2.18", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/sebastianbergmann/php-code-coverage.git", 16 | "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", 21 | "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "php": ">=5.3.3", 26 | "phpunit/php-file-iterator": ">=1.3.0@stable", 27 | "phpunit/php-text-template": ">=1.2.0@stable", 28 | "phpunit/php-token-stream": ">=1.1.3,<1.3.0" 29 | }, 30 | "require-dev": { 31 | "phpunit/phpunit": "3.7.*@dev" 32 | }, 33 | "suggest": { 34 | "ext-dom": "*", 35 | "ext-xdebug": ">=2.0.5" 36 | }, 37 | "type": "library", 38 | "extra": { 39 | "branch-alias": { 40 | "dev-master": "1.2.x-dev" 41 | } 42 | }, 43 | "autoload": { 44 | "classmap": [ 45 | "PHP/" 46 | ] 47 | }, 48 | "notification-url": "https://packagist.org/downloads/", 49 | "include-path": [ 50 | "" 51 | ], 52 | "license": [ 53 | "BSD-3-Clause" 54 | ], 55 | "authors": [ 56 | { 57 | "name": "Sebastian Bergmann", 58 | "email": "sb@sebastian-bergmann.de", 59 | "role": "lead" 60 | } 61 | ], 62 | "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", 63 | "homepage": "https://github.com/sebastianbergmann/php-code-coverage", 64 | "keywords": [ 65 | "coverage", 66 | "testing", 67 | "xunit" 68 | ], 69 | "time": "2014-09-02T10:13:14+00:00" 70 | }, 71 | { 72 | "name": "phpunit/php-file-iterator", 73 | "version": "1.4.5", 74 | "source": { 75 | "type": "git", 76 | "url": "https://github.com/sebastianbergmann/php-file-iterator.git", 77 | "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4" 78 | }, 79 | "dist": { 80 | "type": "zip", 81 | "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/730b01bc3e867237eaac355e06a36b85dd93a8b4", 82 | "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4", 83 | "shasum": "" 84 | }, 85 | "require": { 86 | "php": ">=5.3.3" 87 | }, 88 | "type": "library", 89 | "extra": { 90 | "branch-alias": { 91 | "dev-master": "1.4.x-dev" 92 | } 93 | }, 94 | "autoload": { 95 | "classmap": [ 96 | "src/" 97 | ] 98 | }, 99 | "notification-url": "https://packagist.org/downloads/", 100 | "license": [ 101 | "BSD-3-Clause" 102 | ], 103 | "authors": [ 104 | { 105 | "name": "Sebastian Bergmann", 106 | "email": "sb@sebastian-bergmann.de", 107 | "role": "lead" 108 | } 109 | ], 110 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 111 | "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", 112 | "keywords": [ 113 | "filesystem", 114 | "iterator" 115 | ], 116 | "support": { 117 | "irc": "irc://irc.freenode.net/phpunit", 118 | "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", 119 | "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/1.4.5" 120 | }, 121 | "time": "2017-11-27T13:52:08+00:00" 122 | }, 123 | { 124 | "name": "phpunit/php-text-template", 125 | "version": "1.2.1", 126 | "source": { 127 | "type": "git", 128 | "url": "https://github.com/sebastianbergmann/php-text-template.git", 129 | "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" 130 | }, 131 | "dist": { 132 | "type": "zip", 133 | "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", 134 | "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", 135 | "shasum": "" 136 | }, 137 | "require": { 138 | "php": ">=5.3.3" 139 | }, 140 | "type": "library", 141 | "autoload": { 142 | "classmap": [ 143 | "src/" 144 | ] 145 | }, 146 | "notification-url": "https://packagist.org/downloads/", 147 | "license": [ 148 | "BSD-3-Clause" 149 | ], 150 | "authors": [ 151 | { 152 | "name": "Sebastian Bergmann", 153 | "email": "sebastian@phpunit.de", 154 | "role": "lead" 155 | } 156 | ], 157 | "description": "Simple template engine.", 158 | "homepage": "https://github.com/sebastianbergmann/php-text-template/", 159 | "keywords": [ 160 | "template" 161 | ], 162 | "support": { 163 | "issues": "https://github.com/sebastianbergmann/php-text-template/issues", 164 | "source": "https://github.com/sebastianbergmann/php-text-template/tree/1.2.1" 165 | }, 166 | "time": "2015-06-21T13:50:34+00:00" 167 | }, 168 | { 169 | "name": "phpunit/php-timer", 170 | "version": "1.0.9", 171 | "source": { 172 | "type": "git", 173 | "url": "https://github.com/sebastianbergmann/php-timer.git", 174 | "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f" 175 | }, 176 | "dist": { 177 | "type": "zip", 178 | "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", 179 | "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", 180 | "shasum": "" 181 | }, 182 | "require": { 183 | "php": "^5.3.3 || ^7.0" 184 | }, 185 | "require-dev": { 186 | "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" 187 | }, 188 | "type": "library", 189 | "extra": { 190 | "branch-alias": { 191 | "dev-master": "1.0-dev" 192 | } 193 | }, 194 | "autoload": { 195 | "classmap": [ 196 | "src/" 197 | ] 198 | }, 199 | "notification-url": "https://packagist.org/downloads/", 200 | "license": [ 201 | "BSD-3-Clause" 202 | ], 203 | "authors": [ 204 | { 205 | "name": "Sebastian Bergmann", 206 | "email": "sb@sebastian-bergmann.de", 207 | "role": "lead" 208 | } 209 | ], 210 | "description": "Utility class for timing", 211 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 212 | "keywords": [ 213 | "timer" 214 | ], 215 | "support": { 216 | "issues": "https://github.com/sebastianbergmann/php-timer/issues", 217 | "source": "https://github.com/sebastianbergmann/php-timer/tree/master" 218 | }, 219 | "time": "2017-02-26T11:10:40+00:00" 220 | }, 221 | { 222 | "name": "phpunit/php-token-stream", 223 | "version": "1.2.2", 224 | "source": { 225 | "type": "git", 226 | "url": "https://github.com/sebastianbergmann/php-token-stream.git", 227 | "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" 228 | }, 229 | "dist": { 230 | "type": "zip", 231 | "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", 232 | "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", 233 | "shasum": "" 234 | }, 235 | "require": { 236 | "ext-tokenizer": "*", 237 | "php": ">=5.3.3" 238 | }, 239 | "type": "library", 240 | "extra": { 241 | "branch-alias": { 242 | "dev-master": "1.2-dev" 243 | } 244 | }, 245 | "autoload": { 246 | "classmap": [ 247 | "PHP/" 248 | ] 249 | }, 250 | "notification-url": "https://packagist.org/downloads/", 251 | "include-path": [ 252 | "" 253 | ], 254 | "license": [ 255 | "BSD-3-Clause" 256 | ], 257 | "authors": [ 258 | { 259 | "name": "Sebastian Bergmann", 260 | "email": "sb@sebastian-bergmann.de", 261 | "role": "lead" 262 | } 263 | ], 264 | "description": "Wrapper around PHP's tokenizer extension.", 265 | "homepage": "https://github.com/sebastianbergmann/php-token-stream/", 266 | "keywords": [ 267 | "tokenizer" 268 | ], 269 | "abandoned": true, 270 | "time": "2014-03-03T05:10:30+00:00" 271 | }, 272 | { 273 | "name": "phpunit/phpunit", 274 | "version": "3.7.38", 275 | "source": { 276 | "type": "git", 277 | "url": "https://github.com/sebastianbergmann/phpunit.git", 278 | "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6" 279 | }, 280 | "dist": { 281 | "type": "zip", 282 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/38709dc22d519a3d1be46849868aa2ddf822bcf6", 283 | "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6", 284 | "shasum": "" 285 | }, 286 | "require": { 287 | "ext-ctype": "*", 288 | "ext-dom": "*", 289 | "ext-json": "*", 290 | "ext-pcre": "*", 291 | "ext-reflection": "*", 292 | "ext-spl": "*", 293 | "php": ">=5.3.3", 294 | "phpunit/php-code-coverage": "~1.2", 295 | "phpunit/php-file-iterator": "~1.3", 296 | "phpunit/php-text-template": "~1.1", 297 | "phpunit/php-timer": "~1.0", 298 | "phpunit/phpunit-mock-objects": "~1.2", 299 | "symfony/yaml": "~2.0" 300 | }, 301 | "require-dev": { 302 | "pear-pear.php.net/pear": "1.9.4" 303 | }, 304 | "suggest": { 305 | "phpunit/php-invoker": "~1.1" 306 | }, 307 | "bin": [ 308 | "composer/bin/phpunit" 309 | ], 310 | "type": "library", 311 | "extra": { 312 | "branch-alias": { 313 | "dev-master": "3.7.x-dev" 314 | } 315 | }, 316 | "autoload": { 317 | "classmap": [ 318 | "PHPUnit/" 319 | ] 320 | }, 321 | "notification-url": "https://packagist.org/downloads/", 322 | "include-path": [ 323 | "", 324 | "../../symfony/yaml/" 325 | ], 326 | "license": [ 327 | "BSD-3-Clause" 328 | ], 329 | "authors": [ 330 | { 331 | "name": "Sebastian Bergmann", 332 | "email": "sebastian@phpunit.de", 333 | "role": "lead" 334 | } 335 | ], 336 | "description": "The PHP Unit Testing framework.", 337 | "homepage": "http://www.phpunit.de/", 338 | "keywords": [ 339 | "phpunit", 340 | "testing", 341 | "xunit" 342 | ], 343 | "support": { 344 | "irc": "irc://irc.freenode.net/phpunit", 345 | "issues": "https://github.com/sebastianbergmann/phpunit/issues", 346 | "source": "https://github.com/sebastianbergmann/phpunit/tree/3.7" 347 | }, 348 | "time": "2014-10-17T09:04:17+00:00" 349 | }, 350 | { 351 | "name": "phpunit/phpunit-mock-objects", 352 | "version": "1.2.3", 353 | "source": { 354 | "type": "git", 355 | "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", 356 | "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875" 357 | }, 358 | "dist": { 359 | "type": "zip", 360 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5794e3c5c5ba0fb037b11d8151add2a07fa82875", 361 | "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875", 362 | "shasum": "" 363 | }, 364 | "require": { 365 | "php": ">=5.3.3", 366 | "phpunit/php-text-template": ">=1.1.1@stable" 367 | }, 368 | "suggest": { 369 | "ext-soap": "*" 370 | }, 371 | "type": "library", 372 | "autoload": { 373 | "classmap": [ 374 | "PHPUnit/" 375 | ] 376 | }, 377 | "notification-url": "https://packagist.org/downloads/", 378 | "include-path": [ 379 | "" 380 | ], 381 | "license": [ 382 | "BSD-3-Clause" 383 | ], 384 | "authors": [ 385 | { 386 | "name": "Sebastian Bergmann", 387 | "email": "sb@sebastian-bergmann.de", 388 | "role": "lead" 389 | } 390 | ], 391 | "description": "Mock Object library for PHPUnit", 392 | "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", 393 | "keywords": [ 394 | "mock", 395 | "xunit" 396 | ], 397 | "abandoned": true, 398 | "time": "2013-01-13T10:24:48+00:00" 399 | }, 400 | { 401 | "name": "symfony/polyfill-ctype", 402 | "version": "v1.25.0", 403 | "source": { 404 | "type": "git", 405 | "url": "https://github.com/symfony/polyfill-ctype.git", 406 | "reference": "30885182c981ab175d4d034db0f6f469898070ab" 407 | }, 408 | "dist": { 409 | "type": "zip", 410 | "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", 411 | "reference": "30885182c981ab175d4d034db0f6f469898070ab", 412 | "shasum": "" 413 | }, 414 | "require": { 415 | "php": ">=7.1" 416 | }, 417 | "provide": { 418 | "ext-ctype": "*" 419 | }, 420 | "suggest": { 421 | "ext-ctype": "For best performance" 422 | }, 423 | "type": "library", 424 | "extra": { 425 | "branch-alias": { 426 | "dev-main": "1.23-dev" 427 | }, 428 | "thanks": { 429 | "name": "symfony/polyfill", 430 | "url": "https://github.com/symfony/polyfill" 431 | } 432 | }, 433 | "autoload": { 434 | "files": [ 435 | "bootstrap.php" 436 | ], 437 | "psr-4": { 438 | "Symfony\\Polyfill\\Ctype\\": "" 439 | } 440 | }, 441 | "notification-url": "https://packagist.org/downloads/", 442 | "license": [ 443 | "MIT" 444 | ], 445 | "authors": [ 446 | { 447 | "name": "Gert de Pagter", 448 | "email": "BackEndTea@gmail.com" 449 | }, 450 | { 451 | "name": "Symfony Community", 452 | "homepage": "https://symfony.com/contributors" 453 | } 454 | ], 455 | "description": "Symfony polyfill for ctype functions", 456 | "homepage": "https://symfony.com", 457 | "keywords": [ 458 | "compatibility", 459 | "ctype", 460 | "polyfill", 461 | "portable" 462 | ], 463 | "support": { 464 | "source": "https://github.com/symfony/polyfill-ctype/tree/v1.25.0" 465 | }, 466 | "funding": [ 467 | { 468 | "url": "https://symfony.com/sponsor", 469 | "type": "custom" 470 | }, 471 | { 472 | "url": "https://github.com/fabpot", 473 | "type": "github" 474 | }, 475 | { 476 | "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", 477 | "type": "tidelift" 478 | } 479 | ], 480 | "time": "2021-10-20T20:35:02+00:00" 481 | }, 482 | { 483 | "name": "symfony/yaml", 484 | "version": "v2.8.52", 485 | "source": { 486 | "type": "git", 487 | "url": "https://github.com/symfony/yaml.git", 488 | "reference": "02c1859112aa779d9ab394ae4f3381911d84052b" 489 | }, 490 | "dist": { 491 | "type": "zip", 492 | "url": "https://api.github.com/repos/symfony/yaml/zipball/02c1859112aa779d9ab394ae4f3381911d84052b", 493 | "reference": "02c1859112aa779d9ab394ae4f3381911d84052b", 494 | "shasum": "" 495 | }, 496 | "require": { 497 | "php": ">=5.3.9", 498 | "symfony/polyfill-ctype": "~1.8" 499 | }, 500 | "type": "library", 501 | "extra": { 502 | "branch-alias": { 503 | "dev-master": "2.8-dev" 504 | } 505 | }, 506 | "autoload": { 507 | "psr-4": { 508 | "Symfony\\Component\\Yaml\\": "" 509 | }, 510 | "exclude-from-classmap": [ 511 | "/Tests/" 512 | ] 513 | }, 514 | "notification-url": "https://packagist.org/downloads/", 515 | "license": [ 516 | "MIT" 517 | ], 518 | "authors": [ 519 | { 520 | "name": "Fabien Potencier", 521 | "email": "fabien@symfony.com" 522 | }, 523 | { 524 | "name": "Symfony Community", 525 | "homepage": "https://symfony.com/contributors" 526 | } 527 | ], 528 | "description": "Symfony Yaml Component", 529 | "homepage": "https://symfony.com", 530 | "support": { 531 | "source": "https://github.com/symfony/yaml/tree/v2.8.52" 532 | }, 533 | "time": "2018-11-11T11:18:13+00:00" 534 | } 535 | ], 536 | "aliases": [], 537 | "minimum-stability": "stable", 538 | "stability-flags": [], 539 | "prefer-stable": false, 540 | "prefer-lowest": false, 541 | "platform": { 542 | "ext-dom": "*", 543 | "ext-simplexml": "*", 544 | "ext-xmlreader": "*", 545 | "ext-xmlwriter": "*" 546 | }, 547 | "platform-dev": { 548 | "ext-bz2": "*", 549 | "ext-libxml": "*" 550 | }, 551 | "plugin-api-version": "1.1.0" 552 | } 553 | -------------------------------------------------------------------------------- /src/BufferedFileRead.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class BufferedFileRead 26 | * 27 | * @since 0.1.3 28 | */ 29 | final class BufferedFileRead 30 | { 31 | const MODE_READ_BINARY = 'rb'; 32 | /** 33 | * @var string 34 | */ 35 | public $buffer; 36 | 37 | /** 38 | * @var resource 39 | */ 40 | private $handle; 41 | 42 | /** 43 | * @var string 44 | */ 45 | private $file; 46 | 47 | /** 48 | * number of bytes to have *maximum* ahead in buffer at read 49 | * 50 | * @var int 51 | * @see readAhead 52 | */ 53 | private $maxAhead = 8192; 54 | 55 | /** 56 | * number of bytes to read ahead. can not be larger than 57 | * maxAhead. 58 | * 59 | * @var int 60 | * @see maxAhead 61 | */ 62 | private $readAhead = 0; 63 | 64 | /** 65 | * @param $filename 66 | * @param $mode 67 | * @param null $use_include_path 68 | * @param null $context 69 | * 70 | * @return bool 71 | */ 72 | public function fopen($filename, $mode, $use_include_path = null, $context = null) { 73 | 74 | if ($mode !== self::MODE_READ_BINARY) { 75 | $message = sprintf( 76 | "unsupported mode '%s', only '%s' is supported for buffered file read", $mode, self::MODE_READ_BINARY 77 | ); 78 | trigger_error($message); 79 | 80 | return false; 81 | } 82 | 83 | if ($context === null) { 84 | $handle = fopen($filename, self::MODE_READ_BINARY, (bool)$use_include_path); 85 | } else { 86 | $handle = fopen($filename, self::MODE_READ_BINARY, (bool)$use_include_path, $context); 87 | } 88 | 89 | if (!$handle) { 90 | return false; 91 | } 92 | 93 | $this->file = $filename; 94 | $this->handle = $handle; 95 | 96 | return true; 97 | } 98 | 99 | /** 100 | * appends up to $count bytes to the buffer up to 101 | * the read-ahead limit 102 | * 103 | * @param $count 104 | * 105 | * @return int|bool length of buffer or FALSE on error 106 | */ 107 | public function append($count) 108 | { 109 | $bufferLen = null === $this->buffer ? 0 : strlen($this->buffer); 110 | 111 | if ($bufferLen >= $count + $this->maxAhead) { 112 | return $bufferLen; 113 | } 114 | 115 | ($ahead = $this->readAhead) 116 | && ($delta = $bufferLen - $ahead) < 0 117 | && $count -= $delta; 118 | 119 | $read = fread($this->handle, $count); 120 | if ($read === false) { 121 | throw new UnexpectedValueException('Can not deal with fread() errors.'); 122 | } 123 | 124 | if ($readLen = strlen($read)) { 125 | $this->buffer .= $read; 126 | $bufferLen += $readLen; 127 | } 128 | 129 | return $bufferLen; 130 | } 131 | 132 | /** 133 | * shift bytes from buffer 134 | * 135 | * @param $bytes - up to buffer-length bytes 136 | * 137 | * @return string 138 | */ 139 | public function shift($bytes) 140 | { 141 | $bufferLen = strlen($this->buffer); 142 | 143 | if ($bytes === $bufferLen) { 144 | $return = $this->buffer; 145 | $this->buffer = ''; 146 | } else { 147 | $return = substr($this->buffer, 0, $bytes); 148 | $this->buffer = substr($this->buffer, $bytes); 149 | } 150 | 151 | return $return; 152 | } 153 | 154 | public function fread($count) { 155 | return fread($this->handle, $count); 156 | } 157 | 158 | public function feof() { 159 | return feof($this->handle); 160 | } 161 | 162 | /** 163 | * @return string 164 | */ 165 | public function getFile() { 166 | return $this->file; 167 | } 168 | 169 | public function __toString() { 170 | return $this->file; 171 | } 172 | 173 | /** 174 | * @return int 175 | */ 176 | public function getReadAhead() { 177 | return $this->readAhead; 178 | } 179 | 180 | /** 181 | * @param int $readAhead 182 | */ 183 | public function setReadAhead($readAhead) { 184 | $this->readAhead = max(0, (int)$readAhead); 185 | } 186 | 187 | public function close() { 188 | if ($this->handle && fclose($this->handle)) { 189 | $this->handle = null; 190 | } 191 | 192 | $this->buffer = ''; 193 | } 194 | 195 | public function __destruct() { 196 | $this->close(); 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /src/BufferedFileReaders.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class BufferedFileReaders 26 | * 27 | * Brigade of BufferedFileRead objects as keyed instances based on 28 | * their filename. 29 | * 30 | * @since 0.1.3 31 | */ 32 | class BufferedFileReaders 33 | { 34 | /** 35 | * this wrapper is a multi-singleton based on the filename 36 | * 37 | * @var BufferedFileRead[] 38 | */ 39 | private $readers; 40 | 41 | /** 42 | * @param $filename 43 | * @param $mode 44 | * @param $use_include_path 45 | * @param $context 46 | * 47 | * @return BufferedFileRead or null on error 48 | */ 49 | public function getReaderForFile($filename, $mode, $use_include_path, $context) 50 | { 51 | $readers = $this->readers; 52 | if (!isset($readers[$filename])) { 53 | $reader = new BufferedFileRead(); 54 | $result = $reader->fopen($filename, $mode, $use_include_path, $context); 55 | 56 | return $this->readers[$filename] = $result ? $reader : null; 57 | } 58 | return $readers[$filename]; 59 | } 60 | 61 | public function close() 62 | { 63 | if (!$this->readers) { 64 | return; 65 | } 66 | 67 | foreach ($this->readers as $reader) { 68 | $reader && $reader->close(); 69 | } 70 | 71 | $this->readers = null; 72 | } 73 | 74 | public function removeReaderForFile($filename) 75 | { 76 | if (!isset($this->readers[$filename])) { 77 | return false; 78 | } 79 | 80 | $this->readers[$filename]->close(); 81 | 82 | unset($this->readers[$filename]); 83 | 84 | return true; 85 | } 86 | 87 | public function isFileConsumed($filename) 88 | { 89 | if (!isset($this->readers[$filename]) || !$reader = $this->readers[$filename]) { 90 | return false; 91 | } 92 | 93 | if ($reader->feof() && !strlen($reader->buffer)) { 94 | return true; 95 | } 96 | 97 | return false; 98 | } 99 | 100 | public function __destruct() 101 | { 102 | $this->close(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/DOMReadingIteration.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | 25 | /** 26 | * Class DOMReadingIteration 27 | * 28 | * @since 0.1.0 29 | */ 30 | class DOMReadingIteration extends IteratorIterator 31 | { 32 | private $rootNode; 33 | 34 | private $reader; 35 | const XMLNS = 'xmlns'; 36 | 37 | /** 38 | * @var array|DOMNode[] 39 | */ 40 | private $stack; 41 | 42 | /** 43 | * @var int 44 | */ 45 | private $depth; 46 | 47 | /** 48 | * @var int 49 | */ 50 | private $lastDepth; 51 | 52 | /** 53 | * @var DOMNode 54 | */ 55 | private $node; 56 | 57 | /** 58 | * @var DOMNode 59 | */ 60 | private $lastNode; 61 | 62 | public function __construct(DOMNode $node, XMLReader $reader) 63 | { 64 | $this->rootNode = $node; 65 | $this->reader = $reader; 66 | parent::__construct(new XMLReaderIteration($reader)); 67 | } 68 | 69 | /** 70 | * The element by marked by type XMLReader::END_ELEMENT 71 | * is empty (has no children) but not self-closing. 72 | * 73 | * @return bool 74 | */ 75 | public function isEndElementOfEmptyElement() 76 | { 77 | return 78 | $this->reader->nodeType === XMLReader::END_ELEMENT 79 | && $this->lastDepth === $this->reader->depth 80 | && $this->lastNode instanceof DOMElement 81 | && !$this->reader->isEmptyElement; 82 | } 83 | 84 | #[\ReturnTypeWillChange] 85 | public function rewind() 86 | { 87 | $this->stack = array($this->rootNode); 88 | parent::rewind(); 89 | $this->build(); 90 | } 91 | 92 | private function build() 93 | { 94 | if (!$this->valid()) { 95 | $this->depth = NULL; 96 | $this->lastDepth = NULL; 97 | $this->node = NULL; 98 | $this->lastNode = NULL; 99 | $this->stack = NULL; 100 | return; 101 | } 102 | 103 | $depth = $this->reader->depth; 104 | 105 | switch ($this->reader->nodeType) { 106 | case XMLReader::ELEMENT: 107 | $parent = $this->stack[$depth]; 108 | $prefix = $this->reader->prefix; 109 | /* @var $node DOMElement */ 110 | if ($prefix) { 111 | $uri = $parent->lookupNamespaceURI($prefix) ?: $this->nsUriSelfLookup($prefix); 112 | if ($uri === NULL) { 113 | trigger_error(sprintf('Unable to lookup NS URI for element prefix "%s"', $prefix)); 114 | } 115 | /* @var $doc DOMDocument */ 116 | $doc = ($parent->ownerDocument?:$parent); 117 | $node = $doc->createElementNS($uri, $this->reader->name); 118 | $node = $parent->appendChild($node); 119 | } else { 120 | $node = $parent->appendChild(new DOMElement($this->reader->name)); 121 | } 122 | $this->stack[$depth + 1] = $node; 123 | if ($this->reader->moveToFirstAttribute()) { 124 | $nsUris = array(); 125 | do { 126 | if ($this->reader->prefix === self::XMLNS) { 127 | $nsUris[$this->reader->localName] = $this->reader->value; 128 | } 129 | } while ($this->reader->moveToNextAttribute()); 130 | 131 | $this->reader->moveToFirstAttribute(); 132 | do { 133 | $prefix = $this->reader->prefix; 134 | if ($prefix === self::XMLNS) { 135 | $node->setAttributeNS('http://www.w3.org/2000/xmlns/', $this->reader->name, $this->reader->value); 136 | } elseif ($prefix) { 137 | $uri = $parent->lookupNamespaceUri($prefix) ?: @$nsUris[$prefix]; 138 | if ($uri === NULL) { 139 | trigger_error(sprintf('Unable to lookup NS URI for attribute prefix "%s"', $prefix)); 140 | } 141 | $node->setAttributeNS($uri, $this->reader->name, $this->reader->value); 142 | } else { 143 | $node->appendChild(new DOMAttr($this->reader->name, $this->reader->value)); 144 | } 145 | } while ($this->reader->moveToNextAttribute()); 146 | } 147 | break; 148 | 149 | case XMLReader::END_ELEMENT: 150 | $node = NULL; 151 | break; 152 | 153 | case XMLReader::COMMENT: 154 | $node = $this->stack[$depth]->appendChild(new DOMComment($this->reader->value)); 155 | break; 156 | 157 | case XMLReader::SIGNIFICANT_WHITESPACE: 158 | case XMLReader::TEXT: 159 | case XMLReader::WHITESPACE: 160 | $node = $this->stack[$depth]->appendChild(new DOMText($this->reader->value)); 161 | break; 162 | 163 | case XMLReader::PI: 164 | $node = $this->stack[$depth]->appendChild(new DOMProcessingInstruction($this->reader->name, $this->reader->value)); 165 | break; 166 | 167 | default: 168 | $node = NULL; 169 | $message = sprintf('Unhandled XMLReader node type %s', XMLReaderNode::dump($this->reader, TRUE)); 170 | trigger_error($message); 171 | } 172 | 173 | $this->depth = $depth; 174 | $this->node = $node; 175 | } 176 | 177 | private function nsUriSelfLookup($prefix) { 178 | $uri = NULL; 179 | 180 | if ($this->reader->moveToFirstAttribute()) { 181 | do { 182 | if ($this->reader->prefix === self::XMLNS && $this->reader->localName === $prefix) { 183 | $uri = $this->reader->value; 184 | break; 185 | } 186 | } while ($this->reader->moveToNextAttribute()); 187 | $this->reader->moveToElement(); 188 | } 189 | 190 | return $uri; 191 | } 192 | 193 | #[\ReturnTypeWillChange] 194 | public function next() 195 | { 196 | parent::next(); 197 | $this->lastDepth = $this->depth; 198 | $this->lastNode = $this->node; 199 | $this->build(); 200 | } 201 | 202 | /** 203 | * @return \DOMNode 204 | */ 205 | public function getLastNode() 206 | { 207 | return $this->lastNode; 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/XMLAttributeFilter.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLAttributeFilter 26 | * 27 | * FilterIterator for attribute value(s) 28 | */ 29 | class XMLAttributeFilter extends XMLAttributeFilterBase 30 | { 31 | private $compare; 32 | private $invert; 33 | 34 | /** 35 | * @param XMLElementIterator $elements 36 | * @param string $attr name of the attribute, '*' for every attribute 37 | * @param string|array $compare value(s) to compare against 38 | * @param bool $invert 39 | */ 40 | public function __construct(XMLElementIterator $elements, $attr, $compare, $invert = false) 41 | { 42 | 43 | parent::__construct($elements, $attr); 44 | 45 | $this->compare = (array) $compare; 46 | $this->invert = (bool) $invert; 47 | } 48 | 49 | #[\ReturnTypeWillChange] 50 | public function accept() 51 | { 52 | $result = $this->search($this->getAttributeValues(), $this->compare); 53 | 54 | return $this->invert ? !$result : $result; 55 | } 56 | 57 | private function search($values, $compares) 58 | { 59 | foreach ($compares as $compare) { 60 | if (in_array($compare, $values)) { 61 | return true; 62 | } 63 | } 64 | 65 | return false; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/XMLAttributeFilterBase.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLAttributeFilterBase 26 | */ 27 | abstract class XMLAttributeFilterBase extends XMLReaderFilterBase 28 | { 29 | private $attr; 30 | 31 | /** 32 | * @param XMLElementIterator $elements 33 | * @param string $attr name of the attribute, '*' for every attribute 34 | */ 35 | public function __construct(XMLElementIterator $elements, $attr) 36 | { 37 | parent::__construct($elements); 38 | $this->attr = $attr; 39 | } 40 | 41 | protected function getAttributeValues() 42 | { 43 | /* @var $node XMLReaderNode */ 44 | $node = parent::current(); 45 | if ('*' === $this->attr) { 46 | $attributes = $node->getAttributes()->getArrayCopy(); 47 | } else { 48 | $attributes = (array) $node->getAttribute($this->attr); 49 | } 50 | 51 | return $attributes; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/XMLAttributeIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLAttributeIterator 26 | * 27 | * Iterator over all attributes of the current node (if any) 28 | */ 29 | class XMLAttributeIterator implements Iterator, Countable, ArrayAccess, XMLReaderAggregate 30 | { 31 | private $reader; 32 | private $valid; 33 | private $array; 34 | 35 | public function __construct(XMLReader $reader) 36 | { 37 | $this->reader = $reader; 38 | } 39 | 40 | #[\ReturnTypeWillChange] 41 | public function count() 42 | { 43 | return $this->reader->attributeCount; 44 | } 45 | 46 | #[\ReturnTypeWillChange] 47 | public function current() 48 | { 49 | return $this->reader->value; 50 | } 51 | 52 | #[\ReturnTypeWillChange] 53 | public function key() 54 | { 55 | return $this->reader->name; 56 | } 57 | 58 | #[\ReturnTypeWillChange] 59 | public function next() 60 | { 61 | $this->valid = $this->reader->moveToNextAttribute(); 62 | if (!$this->valid) { 63 | $this->reader->moveToElement(); 64 | } 65 | } 66 | 67 | #[\ReturnTypeWillChange] 68 | public function rewind() 69 | { 70 | $this->valid = $this->reader->moveToFirstAttribute(); 71 | } 72 | 73 | #[\ReturnTypeWillChange] 74 | public function valid() 75 | { 76 | return $this->valid; 77 | } 78 | 79 | public function getArrayCopy() 80 | { 81 | if ($this->array === null) { 82 | $this->array = iterator_to_array($this); 83 | } 84 | 85 | return $this->array; 86 | } 87 | 88 | public function getAttributeNames() 89 | { 90 | return array_keys($this->getArrayCopy()); 91 | } 92 | 93 | #[\ReturnTypeWillChange] 94 | public function offsetExists($offset) 95 | { 96 | $attributes = $this->getArrayCopy(); 97 | 98 | return isset($attributes[$offset]); 99 | } 100 | 101 | #[\ReturnTypeWillChange] 102 | public function offsetGet($offset) 103 | { 104 | $attributes = $this->getArrayCopy(); 105 | 106 | return $attributes[$offset]; 107 | } 108 | 109 | #[\ReturnTypeWillChange] 110 | public function offsetSet($offset, $value) 111 | { 112 | throw new BadMethodCallException('XMLReader attributes are read-only'); 113 | } 114 | 115 | #[\ReturnTypeWillChange] 116 | public function offsetUnset($offset) 117 | { 118 | throw new BadMethodCallException('XMLReader attributes are read-only'); 119 | } 120 | 121 | /** 122 | * @return XMLReader 123 | */ 124 | public function getReader() 125 | { 126 | return $this->reader; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/XMLAttributePreg.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLAttributePreg 26 | * 27 | * PCRE regular expression based filter for elements with a certain attribute value 28 | */ 29 | class XMLAttributePreg extends XMLAttributeFilterBase 30 | { 31 | private $pattern; 32 | private $invert; 33 | 34 | /** 35 | * @param XMLElementIterator $elements 36 | * @param string $attr name of the attribute, '*' for every attribute 37 | * @param string $pattern pcre based regex pattern for the attribute value 38 | * @param bool $invert 39 | * @throws InvalidArgumentException 40 | */ 41 | public function __construct(XMLElementIterator $elements, $attr, $pattern, $invert = false) 42 | { 43 | parent::__construct($elements, $attr); 44 | 45 | if (false === preg_match((string)$pattern, '')) { 46 | throw new InvalidArgumentException("Invalid pcre pattern '$pattern'."); 47 | } 48 | $this->pattern = $pattern; 49 | $this->invert = (bool) $invert; 50 | } 51 | 52 | #[\ReturnTypeWillChange] 53 | public function accept() 54 | { 55 | return (bool) preg_grep($this->pattern, $this->getAttributeValues(), $this->invert ? PREG_GREP_INVERT : 0); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/XMLBuild.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Module XMLBuild 26 | * 27 | * Some string functions helping to create XML 28 | * 29 | * @since 0.0.23 30 | */ 31 | abstract class XMLBuild 32 | { 33 | 34 | /** 35 | * indentLines() 36 | * 37 | * this will add a line-separator at the end of the last line because if it was 38 | * empty it is not any longer and deserves one. 39 | * 40 | * @param string $lines 41 | * @param string $indent (optional) 42 | * 43 | * @return string 44 | */ 45 | public static function indentLines($lines, $indent = ' ') 46 | { 47 | $lineSeparator = "\n"; 48 | $buffer = ''; 49 | $line = strtok($lines, $lineSeparator); 50 | while ($line) { 51 | $buffer .= $indent . $line . $lineSeparator; 52 | $line = strtok($lineSeparator); 53 | } 54 | strtok('', ''); 55 | 56 | return $buffer; 57 | } 58 | 59 | /** 60 | * @param string $name 61 | * @param array|Traversable $attributes attributeName => attributeValue string pairs 62 | * @param bool $emptyTag create an empty element tag (commonly known as short tags) 63 | * 64 | * @return string 65 | */ 66 | public static function startTag($name, $attributes, $emptyTag = false) 67 | { 68 | $buffer = '<' . $name; 69 | $buffer .= static::attributes($attributes); 70 | $buffer .= $emptyTag ? '/>' : '>'; 71 | 72 | return $buffer; 73 | } 74 | 75 | /** 76 | * @param array|Traversable $attributes attributeName => attributeValue string pairs 77 | * 78 | * @return string 79 | */ 80 | public static function attributes($attributes) 81 | { 82 | $buffer = ''; 83 | 84 | foreach ($attributes as $name => $value) { 85 | $buffer .= ' ' . $name . '="' . static::attributeValue($value) . '"'; 86 | } 87 | 88 | return $buffer; 89 | } 90 | 91 | /** 92 | * @param string $value 93 | * @see XMLBuild::numericEntitiesSingleByte 94 | * 95 | * @return string 96 | */ 97 | public static function attributeValue($value) 98 | { 99 | $buffer = $value; 100 | 101 | // REC-xml/#AVNormalize - preserve 102 | // REC-xml/#sec-line-ends - preserve 103 | $buffer = preg_replace_callback('~\r\n|\r(?!\n)|\t~', array('self', 'numericEntitiesSingleByte'), $buffer); 104 | 105 | return htmlspecialchars($buffer, ENT_QUOTES, 'UTF-8', false); 106 | } 107 | 108 | /** 109 | * @param string $name 110 | * @param array|Traversable $attributes attributeName => attributeValue string pairs 111 | * @param string $innerXML 112 | * 113 | * @return string 114 | */ 115 | public static function wrapTag($name, $attributes, $innerXML) 116 | { 117 | if (!strlen($innerXML)) { 118 | return XMLBuild::startTag($name, $attributes, true); 119 | } 120 | 121 | return 122 | XMLBuild::startTag($name, $attributes) 123 | . "\n" 124 | . XMLBuild::indentLines($innerXML) 125 | . ""; 126 | } 127 | 128 | /** 129 | * @param XMLReader $reader 130 | * 131 | * @return string 132 | */ 133 | public static function readerNode(XMLReader $reader) 134 | { 135 | switch ($reader->nodeType) { 136 | case XMLREADER::NONE: 137 | return '%(0)%'; 138 | 139 | case XMLReader::ELEMENT: 140 | return XMLBuild::startTag($reader->name, new XMLAttributeIterator($reader)); 141 | 142 | default: 143 | $node = new XMLReaderNode($reader); 144 | $nodeTypeName = $node->getNodeTypeName(); 145 | $nodeType = $reader->nodeType; 146 | return sprintf('%%%s (%d)%%', $nodeTypeName, $nodeType); 147 | } 148 | } 149 | 150 | /** 151 | * limit string to maximum length and C-escape non-printable ASCII chars 152 | * 153 | * @param string $str 154 | * @param int|null $maxLen optional, defaults to 20 (null), 0 (or below) for 512 block size 155 | * @return string 156 | */ 157 | public static function displayString($str, $maxLen = null) 158 | { 159 | null === $maxLen && $maxLen = 20; 160 | $maxLen = (int) $maxLen; 161 | ($maxLen < 1) && $maxLen = 512; 162 | ($maxLen < 4) && $maxLen = 3; 163 | 164 | $buffer = $str; 165 | $len = strlen($buffer); 166 | if ($len > $maxLen) { 167 | $buffer = substr($buffer, 0, $maxLen - 3) . '...'; 168 | } 169 | return addcslashes($buffer, "\0..\37\42\134\177..\377"); 170 | } 171 | 172 | /** 173 | * dump representation of a string 174 | * 175 | * @param string $str 176 | * @param int|null $maxLen {@see XMLBuild::displayString()} 177 | * @return string 178 | */ 179 | public static function dumpString($str, $maxLen = null) 180 | { 181 | $buffer = self::displayString($str, $maxLen); 182 | return sprintf('(%d) "%s"', strlen($str), $buffer); 183 | } 184 | 185 | /** 186 | * @param array $matches 187 | * 188 | * @return string 189 | * @see attributeValue() 190 | */ 191 | private static function numericEntitiesSingleByte($matches) 192 | { 193 | $buffer = str_split($matches[0]); 194 | foreach ($buffer as &$char) { 195 | $char = sprintf('&#%d;', ord($char)); 196 | } 197 | 198 | return implode('', $buffer); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /src/XMLChildElementIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLChildElementIterator 26 | * 27 | * Iterate over child element nodes of the current XMLReader node 28 | */ 29 | class XMLChildElementIterator extends XMLElementIterator 30 | { 31 | /** 32 | * @var null|int 33 | */ 34 | private $stopDepth; 35 | 36 | /** 37 | * @var bool 38 | */ 39 | private $descendTree; 40 | 41 | /** 42 | * @var bool 43 | */ 44 | private $didRewind; 45 | 46 | /** 47 | * @var int 48 | */ 49 | private $index; 50 | 51 | /** 52 | * @var string|null 53 | */ 54 | private $name; 55 | 56 | /** 57 | * @inheritdoc 58 | * 59 | * @param bool $descendantAxis traverse children of children 60 | */ 61 | public function __construct(XMLReader $reader, $name = null, $descendantAxis = false) 62 | { 63 | parent::__construct($reader); 64 | $this->name = $name; 65 | $this->descendTree = $descendantAxis; 66 | } 67 | 68 | /** 69 | * @throws UnexpectedValueException 70 | * @return void 71 | */ 72 | public function rewind() 73 | { 74 | // this iterator can not really rewind. instead it places itself onto the 75 | // first child element - if any. 76 | if ($this->didRewind) { 77 | return; 78 | } 79 | 80 | if ($this->reader->nodeType === XMLReader::NONE) { 81 | !$this->moveToNextByNodeType(XMLReader::ELEMENT); 82 | } 83 | 84 | if ($this->stopDepth === null) { 85 | $this->stopDepth = $this->reader->depth; 86 | } 87 | 88 | // move to first child element - if any 89 | $result = $this->nextChildElementByName($this->name); 90 | 91 | $this->index = $result ? 0 : null; 92 | $this->didRewind = true; 93 | } 94 | 95 | public function next() 96 | { 97 | if (!$this->valid()) { 98 | return; 99 | } 100 | 101 | $this->index++; 102 | $this->nextChildElementByName($this->name); 103 | } 104 | 105 | public function valid() 106 | { 107 | if (!$this->didRewind) { 108 | return false; 109 | } 110 | 111 | $depth = $this->reader->depth; 112 | if ($depth <= $this->stopDepth) { 113 | return false; 114 | } 115 | if (!$this->descendTree && $depth !== $this->stopDepth + 1) { 116 | return false; 117 | } 118 | if ($this->name === null || $this->reader->name === $this->name) { 119 | return $this->reader->nodeType === XMLReader::ELEMENT; // always true here if reader in sync with $this 120 | } 121 | 122 | return false; 123 | } 124 | 125 | /** 126 | * @return XMLReaderNode|null 127 | */ 128 | public function current() 129 | { 130 | $this->didRewind || $this->rewind(); 131 | return $this->valid() ? parent::current() : null; 132 | } 133 | 134 | /** 135 | * @return int 136 | */ 137 | public function key() 138 | { 139 | return $this->index; 140 | } 141 | 142 | /** 143 | * move to next child element by name 144 | * 145 | * @param string|null $name 146 | * @return bool 147 | */ 148 | private function nextChildElementByName($name = null) 149 | { 150 | while ($next = $this->nextElement()) { 151 | $depth = $this->reader->depth; 152 | if ($depth <= $this->stopDepth) { 153 | return false; 154 | } 155 | if (!$this->descendTree && $depth !== $this->stopDepth + 1) { 156 | continue; 157 | } 158 | if ($name === null || $this->reader->name === $name) { 159 | break; 160 | } 161 | } 162 | 163 | return (bool)$next; 164 | } 165 | 166 | /** 167 | * @return bool 168 | */ 169 | private function nextElement() 170 | { 171 | while ($this->reader->read()) { 172 | if (XMLReader::ELEMENT !== $this->reader->nodeType) { 173 | continue; 174 | } 175 | $this->touchElementStack(); 176 | return true; 177 | } 178 | return false; 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/XMLChildIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLChildIterator 26 | * 27 | * Iterate over child-nodes of the current XMLReader node 28 | */ 29 | class XMLChildIterator extends XMLReaderIterator 30 | { 31 | private $stopDepth; 32 | 33 | public function __construct(XMLReader $reader) 34 | { 35 | parent::__construct($reader); 36 | $this->stopDepth = $reader->depth; 37 | } 38 | 39 | public function rewind() 40 | { 41 | parent::next(); 42 | parent::rewind(); 43 | } 44 | 45 | public function valid() 46 | { 47 | $parent = parent::valid(); 48 | 49 | return $parent and $this->reader->depth > $this->stopDepth; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/XMLElementIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLElementIterator 26 | * 27 | * Iterate over XMLReader element nodes 28 | * 29 | * @method string readString() inherited from IteratorIterator decoration of XMLReader 30 | */ 31 | class XMLElementIterator extends XMLReaderIterator 32 | { 33 | private $index; 34 | private $name; 35 | private $didRewind; 36 | 37 | /** 38 | * @param XMLReader $reader 39 | * @param null|string $name element name, leave empty or use '*' for all elements 40 | */ 41 | public function __construct(XMLReader $reader, $name = null) 42 | { 43 | parent::__construct($reader); 44 | $this->setName($name); 45 | } 46 | 47 | /** 48 | * @return void 49 | */ 50 | public function rewind() 51 | { 52 | parent::rewind(); 53 | $this->ensureCurrentElementState(); 54 | $this->didRewind = true; 55 | $this->index = 0; 56 | } 57 | 58 | /** 59 | * @return XMLReaderNode|null 60 | */ 61 | public function current() 62 | { 63 | $this->didRewind || self::rewind(); 64 | 65 | $this->ensureCurrentElementState(); 66 | 67 | return self::valid() ? new XMLReaderNode($this->reader) : null; 68 | } 69 | 70 | public function key() 71 | { 72 | return $this->index; 73 | } 74 | 75 | public function next() 76 | { 77 | if (parent::valid()) { 78 | $this->index++; 79 | } 80 | parent::next(); 81 | $this->ensureCurrentElementState(); 82 | } 83 | 84 | /** 85 | * @return array 86 | */ 87 | public function toArray() 88 | { 89 | $array = array(); 90 | 91 | $this->didRewind || $this->rewind(); 92 | 93 | if (!$this->valid()) { 94 | return array(); 95 | } 96 | 97 | $this->ensureCurrentElementState(); 98 | 99 | while ($this->valid()) { 100 | $element = new XMLReaderNode($this->reader); 101 | if ($this->reader->hasValue) { 102 | $string = $this->reader->value; 103 | } else { 104 | $string = $element->readString(); 105 | } 106 | if ($this->name) { 107 | $array[] = $string; 108 | } else { 109 | $array[$element->name] = $string; 110 | } 111 | $this->moveToNextElementByName($this->name); 112 | } 113 | 114 | return $array; 115 | } 116 | 117 | /** 118 | * @return string 119 | */ 120 | public function __toString() 121 | { 122 | return $this->readString(); 123 | } 124 | 125 | /** 126 | * decorate method calls 127 | * 128 | * @param string $name 129 | * @param array $args 130 | * 131 | * @return mixed 132 | */ 133 | public function __call($name, $args) 134 | { 135 | return call_user_func_array(array($this->current(), $name), $args); 136 | } 137 | 138 | /** 139 | * decorate property get 140 | * 141 | * @param string $name 142 | * 143 | * @return string 144 | */ 145 | public function __get($name) 146 | { 147 | return $this->current()->$name; 148 | } 149 | 150 | /** 151 | * @param null|string $name 152 | */ 153 | public function setName($name = null) 154 | { 155 | $this->name = '*' === $name ? null : $name; 156 | } 157 | 158 | /** 159 | * take care the underlying XMLReader is at an element with a fitting name (if $this is looking for a name) 160 | */ 161 | private function ensureCurrentElementState() 162 | { 163 | if ($this->reader->nodeType !== XMLReader::ELEMENT) { 164 | $this->moveToNextElementByName($this->name); 165 | } elseif ($this->name && $this->name !== $this->reader->name) { 166 | $this->moveToNextElementByName($this->name); 167 | } 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/XMLElementXpathFilter.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLElementXpathFilter 26 | * 27 | * Filter an XMLReaderIterator with an Xpath expression 28 | * 29 | * @since 0.0.19 30 | * 31 | * @method XMLElementIterator getInnerIterator() 32 | * @method XMLReaderNode current() 33 | */ 34 | class XMLElementXpathFilter extends XMLReaderFilterBase 35 | { 36 | private $expression; 37 | 38 | public function __construct(XMLElementIterator $iterator, $expression) 39 | { 40 | parent::__construct($iterator); 41 | $this->expression = $expression; 42 | } 43 | 44 | #[\ReturnTypeWillChange] 45 | public function accept() 46 | { 47 | $buffer = $this->getInnerIterator()->getNodeTree(); 48 | $result = simplexml_load_string($buffer)->xpath($this->expression); 49 | $count = count($result); 50 | if (0 === $count) { 51 | return false; 52 | } 53 | 54 | return !($result[$count - 1]->children()->count()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/XMLNodeTypeFilter.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLTypeFilter 26 | * 27 | * FilterIterator to only accept one or more specific XMLReader nodeTypes 28 | * 29 | */ 30 | class XMLNodeTypeFilter extends XMLReaderFilterBase 31 | { 32 | private $allowed; 33 | private $reader; 34 | private $invert; 35 | 36 | /** 37 | * @param XMLReaderIterator $iterator 38 | * @param int|int[] $nodeType one or more type constants 39 | * XMLReader::NONE XMLReader::ELEMENT XMLReader::ATTRIBUTE XMLReader::TEXT 40 | * XMLReader::CDATA XMLReader::ENTITY_REF XMLReader::ENTITY XMLReader::PI 41 | * XMLReader::COMMENT XMLReader::DOC XMLReader::DOC_TYPE XMLReader::DOC_FRAGMENT 42 | * XMLReader::NOTATION XMLReader::WHITESPACE XMLReader::SIGNIFICANT_WHITESPACE 43 | * XMLReader::END_ELEMENT XMLReader::END_ENTITY XMLReader::XML_DECLARATION 44 | * @param bool $invert 45 | */ 46 | public function __construct(XMLReaderIterator $iterator, $nodeType, $invert = false) 47 | { 48 | parent::__construct($iterator); 49 | $this->allowed = (array) $nodeType; 50 | $this->reader = $iterator->getReader(); 51 | $this->invert = $invert; 52 | } 53 | 54 | #[\ReturnTypeWillChange] 55 | public function accept() 56 | { 57 | $result = in_array($this->reader->nodeType, $this->allowed); 58 | 59 | return $this->invert ? !$result : $result; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/XMLReaderAggregate.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderAggregate 26 | * 27 | * @since 0.0.21 28 | */ 29 | interface XMLReaderAggregate 30 | { 31 | /** 32 | * @return XMLReader 33 | */ 34 | public function getReader(); 35 | } 36 | -------------------------------------------------------------------------------- /src/XMLReaderElement.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderElement 26 | * 27 | * This node is used in the elementStack 28 | * 29 | * @since 0.0.19 30 | */ 31 | class XMLReaderElement extends XMLReaderNode 32 | { 33 | private $name_; 34 | private $attributes_; 35 | 36 | public function __construct(XMLReader $reader) 37 | { 38 | parent::__construct($reader); 39 | $this->initializeFrom($reader); 40 | } 41 | 42 | public function getXMLElementAround($innerXML = '') 43 | { 44 | return XMLBuild::wrapTag($this->name_, $this->attributes_, $innerXML); 45 | } 46 | 47 | public function getAttributes() 48 | { 49 | return $this->attributes_; 50 | } 51 | 52 | public function getAttribute($name, $default = null) 53 | { 54 | return isset($this->attributes_[$name]) 55 | ? $this->attributes_[$name] : $default; 56 | } 57 | 58 | public function __toString() 59 | { 60 | return $this->name_; 61 | } 62 | 63 | private function initializeFrom(XMLReader $reader) 64 | { 65 | if ($reader->nodeType !== XMLReader::ELEMENT) { 66 | $node = new XMLReaderNode($reader); 67 | throw new RuntimeException(sprintf( 68 | 'Reader must be at an XMLReader::ELEMENT, is XMLReader::%s given.', 69 | $node->getNodeTypeName() 70 | )); 71 | } 72 | $this->name_ = $reader->name; 73 | $this->attributes_ = parent::getAttributes()->getArrayCopy(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/XMLReaderFilterBase.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderFilterBase 26 | * 27 | * @since 0.0.21 28 | * 29 | * @method XMLReaderIterator getInnerIterator() 30 | */ 31 | abstract class XMLReaderFilterBase extends FilterIterator implements XMLReaderAggregate 32 | { 33 | 34 | public function __construct(XMLReaderIterator $elements) { 35 | parent::__construct($elements); 36 | } 37 | 38 | /** 39 | * @return XMLReader 40 | */ 41 | public function getReader() 42 | { 43 | return $this->getInnerIterator()->getReader(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/XMLReaderIteration.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderIteration 26 | * 27 | * Very basic XMLReader iteration 28 | * 29 | * @since 0.1.0 30 | */ 31 | class XMLReaderIteration implements Iterator 32 | { 33 | /** 34 | * @var XMLReader 35 | */ 36 | private $reader; 37 | 38 | /** 39 | * @var boolean 40 | */ 41 | private $valid; 42 | 43 | /** 44 | * @var int 45 | */ 46 | private $index; 47 | 48 | /** 49 | * @var bool 50 | */ 51 | private $skipNextRead; 52 | 53 | public function __construct(XMLReader $reader) 54 | { 55 | $this->reader = $reader; 56 | } 57 | 58 | /** 59 | * skip the next read on next "next()" 60 | * 61 | * this is useful of the reader has moved to the next node already inside a 62 | * foreach iteration and the next "next()" would move the reader one too 63 | * far. 64 | * 65 | * @see next 66 | */ 67 | public function skipNextRead() 68 | { 69 | $this->skipNextRead = true; 70 | } 71 | 72 | /** 73 | * @return XMLReader 74 | */ 75 | #[\ReturnTypeWillChange] 76 | public function current() 77 | { 78 | return $this->reader; 79 | } 80 | 81 | #[\ReturnTypeWillChange] 82 | public function next() 83 | { 84 | $this->index++; 85 | 86 | if ($this->skipNextRead) { 87 | $this->skipNextRead = false; 88 | $this->valid = $this->reader->nodeType; 89 | } else { 90 | $this->valid = $this->reader->read(); 91 | } 92 | } 93 | 94 | #[\ReturnTypeWillChange] 95 | public function key() 96 | { 97 | return $this->index; 98 | } 99 | 100 | #[\ReturnTypeWillChange] 101 | public function valid() 102 | { 103 | return $this->valid; 104 | } 105 | 106 | #[\ReturnTypeWillChange] 107 | public function rewind() 108 | { 109 | if ($this->reader->nodeType !== XMLReader::NONE) { 110 | throw new BadMethodCallException('Reader can not be rewound'); 111 | } 112 | 113 | $this->index = 0; 114 | $this->valid = $this->reader->read(); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/XMLReaderIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderIterator 26 | * 27 | * Iterate over all nodes of a reader 28 | */ 29 | class XMLReaderIterator implements Iterator, XMLReaderAggregate 30 | { 31 | /** 32 | * @var XMLReader 33 | */ 34 | protected $reader; 35 | 36 | /** 37 | * @var int 38 | */ 39 | private $index; 40 | 41 | /** 42 | * stores the result of the last XMLReader::read() operation. 43 | * 44 | * additionally, it's set to true if not initialized (null) on @see XMLReaderIterator::rewind() 45 | * 46 | * @var bool 47 | */ 48 | private $lastRead; 49 | 50 | /** 51 | * @var bool 52 | */ 53 | private $skipNextRead; 54 | 55 | /** 56 | * @var array 57 | */ 58 | private $elementStack; 59 | 60 | public function __construct(XMLReader $reader) 61 | { 62 | $this->reader = $reader; 63 | } 64 | 65 | public function getReader() 66 | { 67 | return $this->reader; 68 | } 69 | 70 | /** 71 | * skip the next read on next "next()" 72 | * 73 | * compare {@see XMLReaderIteration::skipNextRead()} 74 | * 75 | * @see XMLReaderIterator::next() 76 | * 77 | */ 78 | public function skipNextRead() 79 | { 80 | $this->skipNextRead = true; 81 | } 82 | 83 | public function moveToNextElementByName($name = null) 84 | { 85 | while (self::moveToNextElement()) { 86 | if (!$name || $name === $this->reader->name) { 87 | break; 88 | } 89 | self::next(); 90 | } 91 | ; 92 | 93 | return self::valid() ? self::current() : false; 94 | } 95 | 96 | public function moveToNextElement() 97 | { 98 | return $this->moveToNextByNodeType(XMLReader::ELEMENT); 99 | } 100 | 101 | /** 102 | * @param int $nodeType 103 | * 104 | * @return bool|\XMLReaderNode 105 | */ 106 | public function moveToNextByNodeType($nodeType) 107 | { 108 | if (null === self::valid()) { 109 | self::rewind(); 110 | } elseif (self::valid()) { 111 | self::next(); 112 | } 113 | 114 | while (self::valid()) { 115 | if ($this->reader->nodeType === $nodeType) { 116 | break; 117 | } 118 | self::next(); 119 | } 120 | 121 | return self::valid() ? self::current() : false; 122 | } 123 | 124 | #[\ReturnTypeWillChange] 125 | public function rewind() 126 | { 127 | $this->skipNextRead = false; 128 | 129 | // this iterator can not really rewind 130 | if ($this->reader->nodeType === XMLREADER::NONE) { 131 | self::next(); 132 | } elseif ($this->lastRead === null) { 133 | $this->lastRead = true; 134 | } 135 | $this->index = 0; 136 | } 137 | 138 | /** 139 | * @return bool 140 | */ 141 | #[\ReturnTypeWillChange] 142 | public function valid() 143 | { 144 | return $this->lastRead; 145 | } 146 | 147 | /** 148 | * @return XMLReaderNode 149 | */ 150 | #[\ReturnTypeWillChange] 151 | public function current() 152 | { 153 | return $this->lastRead ? new XMLReaderNode($this->reader) : null; 154 | } 155 | 156 | #[\ReturnTypeWillChange] 157 | public function key() 158 | { 159 | return $this->index; 160 | } 161 | 162 | #[\ReturnTypeWillChange] 163 | public function next() 164 | { 165 | $this->index ++; 166 | 167 | if ($this->skipNextRead) { 168 | $this->skipNextRead = false; 169 | $this->lastRead = $this->reader->nodeType !== XMLReader::NONE; 170 | } elseif ($this->lastRead = $this->reader->read() and $this->reader->nodeType === XMLReader::ELEMENT) { 171 | $this->touchElementStack(); 172 | } 173 | } 174 | 175 | /** 176 | * @return string 177 | * @since 0.0.19 178 | */ 179 | public function getNodePath() 180 | { 181 | return '/' . implode('/', $this->elementStack); 182 | } 183 | 184 | /** 185 | * @return string 186 | * @since 0.0.19 187 | */ 188 | public function getNodeTree() 189 | { 190 | $stack = $this->elementStack; 191 | $buffer = ''; 192 | /* @var $element XMLReaderElement */ 193 | while ($element = array_pop($stack)) { 194 | $buffer = $element->getXMLElementAround($buffer); 195 | } 196 | 197 | return $buffer; 198 | } 199 | 200 | /** 201 | * touch the internal element-stack 202 | * 203 | * update the element-stack for the current reader node - which must be 204 | * of type XMLReader::ELEMENT otherwise undefined. 205 | * 206 | * @return void 207 | */ 208 | protected function touchElementStack() 209 | { 210 | $depth = $this->reader->depth; 211 | $this->elementStack[$depth] = new XMLReaderElement($this->reader); 212 | if (count($this->elementStack) !== $depth + 1) { 213 | $this->elementStack = array_slice($this->elementStack, 0, $depth + 1); 214 | } 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /src/XMLReaderNextIteration.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderNextIteration 26 | * 27 | * Iteration over XMLReader skipping subtrees 28 | * 29 | * @link http://php.net/manual/en/xmlreader.next.php 30 | * 31 | * @since 0.1.5 32 | */ 33 | class XMLReaderNextIteration implements Iterator 34 | { 35 | /** 36 | * @var XMLReader 37 | */ 38 | private $reader; 39 | private $index; 40 | private $valid; 41 | private $localName; 42 | 43 | public function __construct(XMLReader $reader, $localName = null) 44 | { 45 | $this->reader = $reader; 46 | $this->localName = $localName; 47 | } 48 | 49 | #[\ReturnTypeWillChange] 50 | public function rewind() 51 | { 52 | // XMLReader can not rewind, instead we move on if before the first node 53 | $this->moveReaderToCurrent(); 54 | 55 | $this->index = 0; 56 | } 57 | 58 | #[\ReturnTypeWillChange] 59 | public function valid() 60 | { 61 | return $this->valid; 62 | } 63 | 64 | #[\ReturnTypeWillChange] 65 | public function current() 66 | { 67 | return $this->reader; 68 | } 69 | 70 | #[\ReturnTypeWillChange] 71 | public function key() 72 | { 73 | return $this->index; 74 | } 75 | 76 | #[\ReturnTypeWillChange] 77 | public function next() 78 | { 79 | $this->valid && $this->index++; 80 | if ($this->localName) { 81 | $this->valid = $this->reader->next($this->localName); 82 | } else { 83 | $this->valid = $this->reader->next(); 84 | } 85 | } 86 | 87 | /** 88 | * move cursor to the next element but only if it's not yet there 89 | */ 90 | private function moveReaderToCurrent() 91 | { 92 | if ( 93 | ($this->reader->nodeType === XMLReader::NONE) 94 | or ($this->reader->nodeType !== XMLReader::ELEMENT) 95 | or ($this->localName && $this->localName !== $this->reader->localName) 96 | ) { 97 | self::next(); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/XMLReaderNode.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLReaderNode 26 | * 27 | * @property string name inherited from XMLReader via IteratorIterator decoration 28 | */ 29 | class XMLReaderNode implements XMLReaderAggregate 30 | { 31 | /** @var XMLReader */ 32 | private $reader; 33 | 34 | /** @var int */ 35 | private $nodeType; 36 | 37 | /** @var string */ 38 | private $name; 39 | 40 | /** @var string */ 41 | private $localName; 42 | 43 | /** 44 | * cache for expansion into SimpleXMLElement 45 | * 46 | * @var null|SimpleXMLElement 47 | * @see asSimpleXML 48 | */ 49 | private $simpleXML; 50 | 51 | /** 52 | * cache for XMLAttributeIterator 53 | * 54 | * @var null|XMLAttributeIterator 55 | * @see getAttributes 56 | */ 57 | private $attributes; 58 | 59 | /** @var string */ 60 | private $string; 61 | 62 | public function __construct(XMLReader $reader) 63 | { 64 | $this->reader = $reader; 65 | $this->nodeType = $reader->nodeType; 66 | $this->name = $reader->name; 67 | } 68 | 69 | public function __toString() 70 | { 71 | if (null === $this->string) { 72 | $this->string = $this->readString(); 73 | } 74 | 75 | return $this->string; 76 | } 77 | 78 | /** 79 | * SimpleXMLElement for XMLReader::ELEMENT 80 | * 81 | * @param string $className SimpleXMLElement class name of the simplexml element 82 | * @return SimpleXMLElement|null in case the current node can not be converted into a SimpleXMLElement 83 | * @since 0.1.4 84 | */ 85 | public function getSimpleXMLElement($className = null) 86 | { 87 | if (null === $this->simpleXML) { 88 | if ($this->reader->nodeType !== XMLReader::ELEMENT) { 89 | return null; 90 | } 91 | 92 | $this->simpleXML = simplexml_import_dom($this->expand(), $className); 93 | } 94 | 95 | if (is_string($className) && !($this->simpleXML instanceof $className)) { 96 | $this->simpleXML = simplexml_import_dom(dom_import_simplexml($this->simpleXML), $className); 97 | } 98 | 99 | return $this->simpleXML; 100 | } 101 | 102 | /** 103 | * @deprecated since v0.1.4, use {@see getSimpleXMLElement()} instead 104 | * @return null|SimpleXMLElement 105 | */ 106 | public function asSimpleXML() 107 | { 108 | trigger_error('Deprecated ' . __METHOD__ . '() - use getSimpleXMLElement() in the future', E_USER_NOTICE); 109 | 110 | return $this->getSimpleXMLElement(); 111 | } 112 | 113 | /** 114 | * @return XMLAttributeIterator|XMLReaderNode[] 115 | */ 116 | public function getAttributes() 117 | { 118 | if (null === $this->attributes) { 119 | $this->attributes = new XMLAttributeIterator($this->reader); 120 | } 121 | 122 | return $this->attributes; 123 | } 124 | 125 | /** 126 | * @param string $name attribute name 127 | * @param string $default (optional) if the attribute with $name does not exists, the value to return 128 | * 129 | * @return null|string value of the attribute, if attribute with $name does not exists null (by $default) 130 | */ 131 | public function getAttribute($name, $default = null) 132 | { 133 | $value = $this->reader->getAttribute($name); 134 | 135 | return null !== $value ? $value : $default; 136 | } 137 | 138 | /** 139 | * @param string $name (optional) element name, null or '*' stand for each element 140 | * @param bool $descendantAxis descend into children of children and so on? 141 | * 142 | * @return XMLChildElementIterator|XMLReaderNode[] 143 | */ 144 | public function getChildElements($name = null, $descendantAxis = false) 145 | { 146 | return new XMLChildElementIterator($this->reader, $name, $descendantAxis); 147 | } 148 | 149 | /** 150 | * @return XMLChildIterator|XMLReaderNode[] 151 | */ 152 | public function getChildren() 153 | { 154 | return new XMLChildIterator($this->reader); 155 | } 156 | 157 | /** 158 | * @return string name 159 | */ 160 | public function getName() 161 | { 162 | return $this->name; 163 | } 164 | 165 | /** 166 | * @return string local name 167 | */ 168 | public function getLocalName() 169 | { 170 | return $this->localName; 171 | } 172 | 173 | public function getReader() 174 | { 175 | return $this->reader; 176 | } 177 | 178 | /** 179 | * Decorated method 180 | * 181 | * @throws BadMethodCallException in case XMLReader can not expand the node 182 | * @return string 183 | */ 184 | public function readOuterXml() 185 | { 186 | // Compatibility libxml 20620 (2.6.20) or later - LIBXML_VERSION / LIBXML_DOTTED_VERSION 187 | if (method_exists($this->reader, 'readOuterXml')) { 188 | return $this->reader->readOuterXml(); 189 | } 190 | 191 | if (0 === $this->reader->nodeType) { 192 | return ''; 193 | } 194 | 195 | $doc = new DOMDocument(); 196 | 197 | $doc->preserveWhiteSpace = false; 198 | $doc->formatOutput = true; 199 | 200 | $node = $this->expand($doc); 201 | 202 | return $doc->saveXML($node); 203 | } 204 | 205 | /** 206 | * XMLReader expand node and import it into a DOMNode with a DOMDocument 207 | * 208 | * This is for example useful for DOMDocument::saveXML() {@see readOuterXml} 209 | * or getting a SimpleXMLElement out of it {@see getSimpleXMLElement} 210 | * 211 | * @param DOMNode $baseNode 212 | * @throws BadMethodCallException 213 | * @return DOMNode 214 | */ 215 | public function expand(DOMNode $baseNode = null) 216 | { 217 | if (null === $baseNode) { 218 | $baseNode = new DomDocument(); 219 | } 220 | 221 | if ($baseNode instanceof DOMDocument) { 222 | $doc = $baseNode; 223 | } else { 224 | $doc = $baseNode->ownerDocument; 225 | if (null === $doc) { 226 | throw new InvalidArgumentException('BaseNode has no OwnerDocument.'); 227 | } 228 | } 229 | 230 | if (false === $node = $this->reader->expand($baseNode)) { 231 | throw new BadMethodCallException('Unable to expand node.'); 232 | } 233 | 234 | if ($node->ownerDocument !== $doc) { 235 | $node = $doc->importNode($node, true); 236 | } 237 | 238 | return $node; 239 | } 240 | 241 | /** 242 | * Decorated method 243 | * 244 | * @throws BadMethodCallException 245 | * @return string 246 | */ 247 | public function readString() 248 | { 249 | // Compatibility libxml 20620 (2.6.20) or later - LIBXML_VERSION / LIBXML_DOTTED_VERSION 250 | if (method_exists($this->reader, 'readString')) { 251 | return $this->reader->readString(); 252 | } 253 | 254 | if (0 === $this->reader->nodeType) { 255 | return ''; 256 | } 257 | 258 | if (false === $node = $this->reader->expand()) { 259 | throw new BadMethodCallException('Unable to expand node.'); 260 | } 261 | 262 | return $node->textContent; 263 | } 264 | 265 | /** 266 | * Return node-type as human-readable string (constant name) 267 | * 268 | * @param null $nodeType 269 | * 270 | * @return string 271 | */ 272 | public function getNodeTypeName($nodeType = null) 273 | { 274 | $strings = array( 275 | XMLReader::NONE => 'NONE', 276 | XMLReader::ELEMENT => 'ELEMENT', 277 | XMLReader::ATTRIBUTE => 'ATTRIBUTE', 278 | XMLREADER::TEXT => 'TEXT', 279 | XMLREADER::CDATA => 'CDATA', 280 | XMLReader::ENTITY_REF => 'ENTITY_REF', 281 | XMLReader::ENTITY => 'ENTITY', 282 | XMLReader::PI => 'PI', 283 | XMLReader::COMMENT => 'COMMENT', 284 | XMLReader::DOC => 'DOC', 285 | XMLReader::DOC_TYPE => 'DOC_TYPE', 286 | XMLReader::DOC_FRAGMENT => 'DOC_FRAGMENT', 287 | XMLReader::NOTATION => 'NOTATION', 288 | XMLReader::WHITESPACE => 'WHITESPACE', 289 | XMLReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE', 290 | XMLReader::END_ELEMENT => 'END_ELEMENT', 291 | XMLReader::END_ENTITY => 'END_ENTITY', 292 | XMLReader::XML_DECLARATION => 'XML_DECLARATION', 293 | ); 294 | 295 | if (null === $nodeType) { 296 | $nodeType = $this->nodeType; 297 | } 298 | 299 | return $strings[$nodeType]; 300 | } 301 | 302 | /** 303 | * decorate method calls 304 | * 305 | * @param string $name 306 | * @param array $args 307 | * 308 | * @return mixed 309 | */ 310 | public function __call($name, $args) 311 | { 312 | return call_user_func_array(array($this->reader, $name), $args); 313 | } 314 | 315 | /** 316 | * decorate property get 317 | * 318 | * @param string $name 319 | * 320 | * @return string 321 | */ 322 | public function __get($name) 323 | { 324 | return $this->reader->$name; 325 | } 326 | 327 | /** 328 | * @param string $name 329 | * @param mixed $value 330 | * @return void 331 | */ 332 | public function __set($name, $value) 333 | { 334 | throw new BadMethodCallException('XMLReader properties are read-only: ' . $name); 335 | } 336 | 337 | /** 338 | * @param string $name 339 | * @return bool 340 | */ 341 | public function __isset($name) 342 | { 343 | return isset($this->reader->$name); 344 | } 345 | 346 | /** 347 | * debug utility method 348 | * 349 | * @param XMLReader $reader 350 | * @param bool $return (optional) prints by default but can return string 351 | * @return string|void 352 | */ 353 | public static function dump(XMLReader $reader, $return = FALSE) 354 | { 355 | $node = new self($reader); 356 | 357 | $nodeType = $reader->nodeType; 358 | $nodeName = $node->getNodeTypeName(); 359 | 360 | $extra = ''; 361 | 362 | if ($reader->nodeType === XMLReader::ELEMENT) { 363 | $extra = ' <' . $reader->name . '> '; 364 | $extra .= sprintf("(isEmptyElement: %s)", $reader->isEmptyElement ? 'Yes' : 'No'); 365 | } 366 | 367 | if ($reader->nodeType === XMLReader::END_ELEMENT) { 368 | $extra = ' name . '>'; 369 | } 370 | 371 | if ($reader->nodeType === XMLReader::ATTRIBUTE) { 372 | $extra = sprintf(' %s = %s', $reader->name, XMLBuild::dumpString($reader->value)); 373 | } 374 | 375 | 376 | if ($reader->nodeType === XMLReader::CDATA 377 | || $reader->nodeType === XMLReader::TEXT 378 | || $reader->nodeType === XMLReader::WHITESPACE 379 | || $reader->nodeType === XMLReader::SIGNIFICANT_WHITESPACE 380 | ) { 381 | $extra = sprintf( ' %s', XMLBuild::dumpString($reader->value)); 382 | } 383 | 384 | $label = sprintf("(#%d) %s%s", $nodeType, $nodeName, $extra); 385 | 386 | if ($return) { 387 | return $label; 388 | } 389 | 390 | printf("%s%s\n", str_repeat(' ', $reader->depth), $label); 391 | } 392 | } 393 | -------------------------------------------------------------------------------- /src/XMLSequenceStream.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLSequenceStream 26 | * 27 | * @since 0.1.3 28 | */ 29 | class XMLSequenceStream 30 | { 31 | const DECL_POS_PATTERN = '(<\?xml\s+version\s*=\s*(["\'])(1\.\d+)\1(?:\s*\?>|\s+encoding\s*=\s*(["\'])(((?!\3).)*)\3))'; 32 | /** 33 | * @var resource 34 | */ 35 | public $context; 36 | 37 | /** 38 | * @var string 39 | */ 40 | private $file; 41 | 42 | /** 43 | * @var BufferedFileRead 44 | */ 45 | private $reader; 46 | 47 | /** 48 | * this wrapper keeps a multi-singleton based on the filename 49 | * for read buffers to allow multiple stream operations 50 | * after another. 51 | * 52 | * @var BufferedFileReaders 53 | */ 54 | public static $readers; 55 | 56 | /** 57 | * @var bool 58 | */ 59 | private $flagEof; 60 | 61 | private $declFound = 0; 62 | 63 | /** 64 | * clear reader buffers, close open files if any. 65 | */ 66 | public static function clean() 67 | { 68 | self::$readers && self::$readers->close(); 69 | } 70 | 71 | /** 72 | * @param string $path filename of the buffer to close, complete with wrapper prefix 73 | * 74 | * @return bool 75 | */ 76 | public static function closeBuffer($path) 77 | { 78 | if (!self::$readers) { 79 | return false; 80 | } 81 | 82 | $path = new XMLSequenceStreamPath($path); 83 | $file = $path->getFile(); 84 | 85 | return self::$readers->removeReaderForFile($file); 86 | } 87 | 88 | /** 89 | * @param $path 90 | * 91 | * @return bool 92 | */ 93 | public static function notAtEndOfSequence($path) 94 | { 95 | if (!self::$readers) { 96 | return true; 97 | } 98 | 99 | try { 100 | $path = new XMLSequenceStreamPath($path); 101 | } catch (UnexpectedValueException $e) { 102 | return true; 103 | } 104 | 105 | $file = $path->getFile(); 106 | 107 | return !self::$readers->isFileConsumed($file); 108 | } 109 | 110 | public function __construct() 111 | { 112 | # fputs(STDOUT, sprintf('')); 113 | self::$readers || self::$readers = new BufferedFileReaders(); 114 | } 115 | 116 | /** 117 | * @param string $path 118 | * @param string $mode 119 | * @param int $options 120 | * @param string $opened_path 121 | * 122 | * @return bool 123 | */ 124 | public function stream_open($path, $mode, $options, &$opened_path) 125 | { 126 | # fputs(STDOUT, sprintf('', var_export($path, 1), $options & STREAM_REPORT_ERRORS, $options & STREAM_USE_PATH)); 127 | $path = new XMLSequenceStreamPath($path); 128 | 129 | $file = $path->getFile(); 130 | $reader = self::$readers->getReaderForFile($file, $mode, null, $this->context); 131 | $this->file = $file; 132 | $this->reader = $reader; 133 | 134 | if (!$reader) { 135 | return false; 136 | } 137 | 138 | $reader->setReadAhead(256); 139 | if ($reader->feof() && !strlen($reader->buffer)) { 140 | $message = sprintf('Concatenated XML Stream: Resource %s at the end of stream', var_export($file, true)); 141 | trigger_error($message); 142 | return false; 143 | } 144 | 145 | return true; 146 | } 147 | 148 | public function stream_stat() 149 | { 150 | return false; 151 | } 152 | 153 | /** 154 | * @param string $path 155 | * @param int $flags 156 | * 157 | * @return bool 158 | */ 159 | public function url_stat($path, $flags) 160 | { 161 | # fputs(STDOUT, sprintf('', var_export($path, 1), $flags & STREAM_URL_STAT_LINK, $flags | STREAM_URL_STAT_QUIET)); 162 | 163 | return array(); 164 | } 165 | 166 | public function stream_read($count) 167 | { 168 | $reader = $this->reader; 169 | 170 | # fputs(STDOUT, sprintf('', $count, strlen($reader->buffer), $this->flagEof)); 171 | 172 | if ($this->flagEof) { 173 | return ''; 174 | } 175 | 176 | $bufferLen = $reader->append($count); 177 | # fputs(STDOUT, sprintf('', $bufferLen)); 178 | 179 | $pos = $this->declPos(); 180 | if (!$this->declFound && $pos !== false) { 181 | $this->declFound++; 182 | if ($pos !== 0) { 183 | throw new UnexpectedValueException(sprintf('First XML declaration expected at offset 0, found at %d', $pos)); 184 | } 185 | $pos = $this->declPos(5); 186 | } 187 | 188 | if ($pos === false) { 189 | $returnLen = min($bufferLen, $count); 190 | } else { 191 | $returnLen = min($pos, $count); 192 | if ($returnLen >= $pos) { 193 | $this->flagEof = true; 194 | } 195 | $this->declFound++; 196 | } 197 | 198 | $return = $reader->shift($returnLen); 199 | 200 | return $return; 201 | } 202 | 203 | private function declPos($offset = 0) 204 | { 205 | $result = preg_match(self::DECL_POS_PATTERN, $this->reader->buffer, $matches, PREG_OFFSET_CAPTURE, $offset); 206 | if ($result === FALSE) { 207 | throw new UnexpectedValueException('Regex failed.'); 208 | } 209 | 210 | return $result ? $matches[0][1] : false; 211 | } 212 | 213 | public function stream_eof() 214 | { 215 | return $this->flagEof; 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /src/XMLSequenceStreamPath.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLSequenceStreamPath 26 | * 27 | * @since 0.1.3 28 | */ 29 | class XMLSequenceStreamPath 30 | { 31 | /** 32 | * @var string 33 | */ 34 | private $path; 35 | 36 | public function __construct($path) { 37 | $this->path = $path; 38 | } 39 | 40 | public function getProtocol() { 41 | $parts = $this->parsePath($this->path); 42 | return $parts['scheme']; 43 | } 44 | 45 | public function getSpecific() { 46 | $parts = $this->parsePath($this->path); 47 | return $parts['specific']; 48 | } 49 | 50 | public function getFile() { 51 | $specific = $this->getSpecific(); 52 | $specific = str_replace(array('\\', '/./'), '/', $specific); 53 | return $specific; 54 | } 55 | 56 | private function parsePath($path) { 57 | 58 | $parts = array_combine(array('scheme', 'specific'), explode('://', $path, 2) + array(null, null)); 59 | 60 | if (null === $parts['specific']) { 61 | throw new UnexpectedValueException(sprintf("Path '%s' has no protocol", $path)); 62 | } 63 | 64 | return $parts; 65 | } 66 | 67 | public function __toString() { 68 | return $this->path; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/XMLWritingIteration.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * @author hakre 21 | * @license AGPL-3.0-or-later 22 | */ 23 | 24 | /** 25 | * Class XMLWritingIteration 26 | * 27 | * @since 0.1.2 28 | * 29 | * @method XMLReader current() 30 | * @mixin XMLReaderIteration 31 | */ 32 | class XMLWritingIteration extends IteratorIterator 33 | { 34 | /** 35 | * @var XMLWriter 36 | */ 37 | private $writer; 38 | 39 | /** 40 | * @var XMLReader 41 | */ 42 | private $reader; 43 | 44 | public function __construct(XMLWriter $writer, XMLReader $reader) { 45 | $this->writer = $writer; 46 | $this->reader = $reader; 47 | 48 | parent::__construct(new XMLReaderIteration($reader)); 49 | } 50 | 51 | public function write() { 52 | $reader = $this->reader; 53 | $writer = $this->writer; 54 | 55 | switch ($reader->nodeType) { 56 | case XMLReader::ELEMENT: 57 | $writer->startElement($reader->name); 58 | 59 | if ($reader->moveToFirstAttribute()) { 60 | do { 61 | $writer->writeAttribute($reader->name, $reader->value); 62 | } while ($reader->moveToNextAttribute()); 63 | $reader->moveToElement(); 64 | } 65 | 66 | if ($reader->isEmptyElement) { 67 | $writer->endElement(); 68 | } 69 | break; 70 | 71 | case XMLReader::END_ELEMENT: 72 | $writer->endElement(); 73 | break; 74 | 75 | case XMLReader::CDATA: 76 | $writer->writeCdata($reader->value); 77 | break; 78 | 79 | case XMLReader::COMMENT: 80 | $writer->writeComment($reader->value); 81 | break; 82 | 83 | case XMLReader::SIGNIFICANT_WHITESPACE: 84 | case XMLReader::TEXT: 85 | case XMLReader::WHITESPACE: 86 | $writer->text($reader->value); 87 | break; 88 | 89 | case XMLReader::PI: 90 | $writer->writePi($reader->name, $reader->value); 91 | break; 92 | 93 | default: 94 | trigger_error(sprintf( 95 | '%s::write(): Node-type not implemented: %s', 96 | __CLASS__, 97 | XMLReaderNode::dump($reader, true) 98 | ), E_USER_WARNING); 99 | } 100 | } 101 | } 102 | --------------------------------------------------------------------------------