| Total Complexity | 151 |
| Total Lines | 1266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SchemaReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class SchemaReader |
||
| 39 | { |
||
| 40 | |||
| 41 | const XSD_NS = "http://www.w3.org/2001/XMLSchema"; |
||
| 42 | |||
| 43 | const XML_NS = "http://www.w3.org/XML/1998/namespace"; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Schema[] |
||
| 47 | */ |
||
| 48 | private $loadedFiles = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | private $knownLocationSchemas = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | private static $globalSchemaInfo = array( |
||
| 59 | self::XML_NS => 'http://www.w3.org/2001/xml.xsd', |
||
| 60 | self::XSD_NS => 'http://www.w3.org/2001/XMLSchema.xsd' |
||
| 61 | ); |
||
| 62 | |||
| 63 | public function __construct() |
||
| 64 | { |
||
| 65 | $this->addKnownSchemaLocation('http://www.w3.org/2001/xml.xsd', __DIR__ . '/Resources/xml.xsd'); |
||
| 66 | $this->addKnownSchemaLocation('http://www.w3.org/2001/XMLSchema.xsd', __DIR__ . '/Resources/XMLSchema.xsd'); |
||
| 67 | $this->addKnownSchemaLocation('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', __DIR__ . '/Resources/oasis-200401-wss-wssecurity-secext-1.0.xsd'); |
||
| 68 | $this->addKnownSchemaLocation('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', __DIR__ . '/Resources/oasis-200401-wss-wssecurity-utility-1.0.xsd'); |
||
| 69 | $this->addKnownSchemaLocation('https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd', __DIR__ . '/Resources/xmldsig-core-schema.xsd'); |
||
| 70 | $this->addKnownSchemaLocation('http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd', __DIR__ . '/Resources/xmldsig-core-schema.xsd'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $remote |
||
| 75 | * @param string $local |
||
| 76 | */ |
||
| 77 | public function addKnownSchemaLocation($remote, $local) |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return Closure |
||
| 84 | */ |
||
| 85 | private function loadAttributeGroup(Schema $schema, DOMElement $node) |
||
| 86 | { |
||
| 87 | $attGroup = new AttributeGroup($schema, $node->getAttribute("name")); |
||
| 88 | $attGroup->setDoc($this->getDocumentation($node)); |
||
| 89 | $schema->addAttributeGroup($attGroup); |
||
| 90 | |||
| 91 | return function () use ($schema, $node, $attGroup) { |
||
| 92 | foreach ($node->childNodes as $childNode) { |
||
| 93 | switch ($childNode->localName) { |
||
| 94 | case 'attribute': |
||
| 95 | $attribute = $this->getAttributeFromAttributeOrRef( |
||
| 96 | $childNode, |
||
| 97 | $schema, |
||
| 98 | $node |
||
| 99 | ); |
||
| 100 | $attGroup->addAttribute($attribute); |
||
| 101 | break; |
||
| 102 | case 'attributeGroup': |
||
| 103 | AttributeGroup::findSomethingLikeThis( |
||
| 104 | $this, |
||
| 105 | $schema, |
||
| 106 | $node, |
||
| 107 | $childNode, |
||
| 108 | $attGroup |
||
| 109 | ); |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | }; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return AttributeItem |
||
| 118 | */ |
||
| 119 | private function getAttributeFromAttributeOrRef( |
||
| 120 | DOMElement $childNode, |
||
| 121 | Schema $schema, |
||
| 122 | DOMElement $node |
||
| 123 | ) { |
||
| 124 | if ($childNode->hasAttribute("ref")) { |
||
| 125 | /** |
||
| 126 | * @var AttributeItem $attribute |
||
| 127 | */ |
||
| 128 | $attribute = $this->findSomething('findAttribute', $schema, $node, $childNode->getAttribute("ref")); |
||
| 129 | } else { |
||
| 130 | /** |
||
| 131 | * @var Attribute $attribute |
||
| 132 | */ |
||
| 133 | $attribute = $this->loadAttribute($schema, $childNode); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $attribute; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return Attribute |
||
| 141 | */ |
||
| 142 | private function loadAttribute(Schema $schema, DOMElement $node) |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return Closure |
||
| 162 | */ |
||
| 163 | private function loadAttributeOrElementDef( |
||
| 164 | Schema $schema, |
||
| 165 | DOMElement $node, |
||
| 166 | bool $attributeDef |
||
| 167 | ) { |
||
| 168 | $name = $node->getAttribute('name'); |
||
| 169 | if ($attributeDef) { |
||
| 170 | $attribute = new AttributeDef($schema, $name); |
||
| 171 | $schema->addAttribute($attribute); |
||
| 172 | } else { |
||
| 173 | $attribute = new ElementDef($schema, $name); |
||
| 174 | $schema->addElement($attribute); |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | return function () use ($attribute, $node) { |
||
| 179 | $this->fillItem($attribute, $node); |
||
| 180 | }; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return Closure |
||
| 185 | */ |
||
| 186 | private function loadAttributeDef(Schema $schema, DOMElement $node) |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param DOMElement $node |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | private function getDocumentation(DOMElement $node) |
||
| 209 | } |
||
| 210 | |||
| 211 | private function setSchemaThingsFromNode( |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param mixed $schema |
||
| 230 | * |
||
| 231 | * @return Closure|null |
||
| 232 | */ |
||
| 233 | private function maybeCallMethod( |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * |
||
| 252 | * @param Schema $schema |
||
| 253 | * @param DOMElement $node |
||
| 254 | * @param Schema $parent |
||
| 255 | * @return Closure[] |
||
| 256 | */ |
||
| 257 | private function schemaNode(Schema $schema, DOMElement $node, Schema $parent = null) |
||
| 258 | { |
||
| 259 | $this->setSchemaThingsFromNode($schema, $node, $parent); |
||
| 260 | $functions = array(); |
||
| 261 | |||
| 262 | static $methods = [ |
||
| 263 | 'include' => 'loadImport', |
||
| 264 | 'import' => 'loadImport', |
||
| 265 | 'element' => 'loadElementDef', |
||
| 266 | 'attribute' => 'loadAttributeDef', |
||
| 267 | 'attributeGroup' => 'loadAttributeGroup', |
||
| 268 | 'group' => 'loadGroup', |
||
| 269 | 'complexType' => 'loadComplexType', |
||
| 270 | 'simpleType' => 'loadSimpleType', |
||
| 271 | ]; |
||
| 272 | |||
| 273 | foreach ($node->childNodes as $childNode) { |
||
| 274 | $callback = $this->maybeCallMethod( |
||
| 275 | $methods, |
||
| 276 | (string) $childNode->localName, |
||
| 277 | $childNode, |
||
| 278 | $schema, |
||
| 279 | $childNode |
||
| 280 | ); |
||
| 281 | |||
| 282 | if ($callback instanceof Closure) { |
||
| 283 | $functions[] = $callback; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | return $functions; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return Element |
||
| 292 | */ |
||
| 293 | private function loadElement(Schema $schema, DOMElement $node) |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return GroupRef |
||
| 321 | */ |
||
| 322 | private function loadGroupRef(Group $referenced, DOMElement $node) |
||
| 323 | { |
||
| 324 | $ref = new GroupRef($referenced); |
||
| 325 | $ref->setDoc($this->getDocumentation($node)); |
||
| 326 | |||
| 327 | static::maybeSetMax($ref, $node); |
||
| 328 | static::maybeSetMin($ref, $node); |
||
| 329 | |||
| 330 | return $ref; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return ElementRef |
||
| 335 | */ |
||
| 336 | private function loadElementRef(ElementDef $referenced, DOMElement $node) |
||
| 337 | { |
||
| 338 | $ref = new ElementRef($referenced); |
||
| 339 | $this->setDoc($ref, $node); |
||
| 340 | |||
| 341 | static::maybeSetMax($ref, $node); |
||
| 342 | static::maybeSetMin($ref, $node); |
||
| 343 | if ($node->hasAttribute("nillable")) { |
||
| 344 | $ref->setNil($node->getAttribute("nillable") == "true"); |
||
| 345 | } |
||
| 346 | if ($node->hasAttribute("form")) { |
||
| 347 | $ref->setQualified($node->getAttribute("form") == "qualified"); |
||
| 348 | } |
||
| 349 | |||
| 350 | return $ref; |
||
| 351 | } |
||
| 352 | |||
| 353 | private function setDoc(Item $ref, DOMElement $node) |
||
| 354 | { |
||
| 355 | $ref->setDoc($this->getDocumentation($node)); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return InterfaceSetMinMax |
||
| 360 | */ |
||
| 361 | private static function maybeSetMax(InterfaceSetMinMax $ref, DOMElement $node) |
||
| 362 | { |
||
| 363 | if ( |
||
| 364 | $node->hasAttribute("maxOccurs") |
||
| 365 | ) { |
||
| 366 | $ref->setMax($node->getAttribute("maxOccurs") == "unbounded" ? -1 : (int)$node->getAttribute("maxOccurs")); |
||
| 367 | } |
||
| 368 | |||
| 369 | return $ref; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return InterfaceSetMinMax |
||
| 374 | */ |
||
| 375 | private static function maybeSetMin(InterfaceSetMinMax $ref, DOMElement $node) |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return AttributeRef |
||
| 386 | */ |
||
| 387 | private function loadAttributeRef(AttributeDef $referencedAttribiute, DOMElement $node) |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param int|null $max |
||
| 406 | */ |
||
| 407 | private function loadSequence(ElementContainer $elementContainer, DOMElement $node, $max = null) |
||
| 424 | ); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param int|null $max |
||
| 431 | */ |
||
| 432 | private function loadSequenceChildNode( |
||
| 433 | ElementContainer $elementContainer, |
||
| 434 | DOMElement $node, |
||
| 435 | DOMElement $childNode, |
||
| 436 | $max |
||
| 437 | ) { |
||
| 438 | $loadSeq = function () use ($elementContainer, $childNode, $max) { |
||
| 439 | $this->loadSequence($elementContainer, $childNode, $max); |
||
| 440 | }; |
||
| 441 | $methods = [ |
||
| 442 | 'choice' => $loadSeq, |
||
| 443 | 'sequence' => $loadSeq, |
||
| 444 | 'all' => $loadSeq, |
||
| 445 | 'element' => function () use ( |
||
| 446 | $elementContainer, |
||
| 447 | $node, |
||
| 448 | $childNode, |
||
| 449 | $max |
||
| 450 | ) { |
||
| 451 | if ($childNode->hasAttribute("ref")) { |
||
| 452 | /** |
||
| 453 | * @var ElementDef $referencedElement |
||
| 454 | */ |
||
| 455 | $referencedElement = $this->findSomething('findElement', $elementContainer->getSchema(), $node, $childNode->getAttribute("ref")); |
||
| 456 | $element = $this->loadElementRef($referencedElement, $childNode); |
||
| 457 | } else { |
||
| 458 | $element = $this->loadElement($elementContainer->getSchema(), $childNode); |
||
| 459 | } |
||
| 460 | if (is_int($max) && (bool) $max) { |
||
| 461 | $element->setMax($max); |
||
| 462 | } |
||
| 463 | $elementContainer->addElement($element); |
||
| 464 | }, |
||
| 465 | 'group' => function () use ( |
||
| 466 | $elementContainer, |
||
| 467 | $node, |
||
| 468 | $childNode |
||
| 469 | ) { |
||
| 470 | $this->addGroupAsElement( |
||
| 471 | $elementContainer->getSchema(), |
||
| 472 | $node, |
||
| 473 | $childNode, |
||
| 474 | $elementContainer |
||
| 475 | ); |
||
| 476 | }, |
||
| 477 | ]; |
||
| 478 | |||
| 479 | if (isset($methods[$childNode->localName])) { |
||
| 480 | $method = $methods[$childNode->localName]; |
||
| 481 | $method(); |
||
| 482 | } |
||
| 483 | } |
||
| 484 | |||
| 485 | private function addGroupAsElement( |
||
| 486 | Schema $schema, |
||
| 487 | DOMElement $node, |
||
| 488 | DOMElement $childNode, |
||
| 489 | ElementContainer $elementContainer |
||
| 490 | ) { |
||
| 491 | /** |
||
| 492 | * @var Group $referencedGroup |
||
| 493 | */ |
||
| 494 | $referencedGroup = $this->findSomething( |
||
| 495 | 'findGroup', |
||
| 496 | $schema, |
||
| 497 | $node, |
||
| 498 | $childNode->getAttribute("ref") |
||
| 499 | ); |
||
| 500 | |||
| 501 | $group = $this->loadGroupRef($referencedGroup, $childNode); |
||
| 502 | $elementContainer->addElement($group); |
||
| 503 | } |
||
| 504 | |||
| 505 | private function maybeLoadSequenceFromElementContainer( |
||
| 506 | BaseComplexType $type, |
||
| 507 | DOMElement $childNode |
||
| 508 | ) { |
||
| 509 | if (! ($type instanceof ElementContainer)) { |
||
| 510 | throw new RuntimeException( |
||
| 511 | '$type passed to ' . |
||
| 512 | __FUNCTION__ . |
||
| 513 | 'expected to be an instance of ' . |
||
| 514 | ElementContainer::class . |
||
| 515 | ' when child node localName is "group", ' . |
||
| 516 | get_class($type) . |
||
| 517 | ' given.' |
||
| 518 | ); |
||
| 519 | } |
||
| 520 | $this->loadSequence($type, $childNode); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return Closure |
||
| 525 | */ |
||
| 526 | private function loadGroup(Schema $schema, DOMElement $node) |
||
| 527 | { |
||
| 528 | $group = new Group($schema, $node->getAttribute("name")); |
||
| 529 | $group->setDoc($this->getDocumentation($node)); |
||
| 530 | |||
| 531 | if ($node->hasAttribute("maxOccurs")) { |
||
| 532 | /** |
||
| 533 | * @var GroupRef $group |
||
| 534 | */ |
||
| 535 | $group = static::maybeSetMax(new GroupRef($group), $node); |
||
| 536 | } |
||
| 537 | if ($node->hasAttribute("minOccurs")) { |
||
| 538 | /** |
||
| 539 | * @var GroupRef $group |
||
| 540 | */ |
||
| 541 | $group = static::maybeSetMin( |
||
| 542 | $group instanceof GroupRef ? $group : new GroupRef($group), |
||
| 543 | $node |
||
| 544 | ); |
||
| 545 | } |
||
| 546 | |||
| 547 | $schema->addGroup($group); |
||
| 548 | |||
| 549 | static $methods = [ |
||
| 550 | 'sequence' => 'loadSequence', |
||
| 551 | 'choice' => 'loadSequence', |
||
| 552 | 'all' => 'loadSequence', |
||
| 553 | ]; |
||
| 554 | |||
| 555 | return function () use ($group, $node, $methods) { |
||
| 556 | foreach ($node->childNodes as $childNode) { |
||
| 557 | $this->maybeCallMethod( |
||
| 558 | $methods, |
||
| 559 | (string) $childNode->localName, |
||
| 560 | $childNode, |
||
| 561 | $group, |
||
| 562 | $childNode |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | }; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param Closure|null $callback |
||
| 570 | * |
||
| 571 | * @return Closure |
||
| 572 | */ |
||
| 573 | private function loadComplexType(Schema $schema, DOMElement $node, $callback = null) |
||
| 574 | { |
||
| 575 | $isSimple = false; |
||
| 576 | |||
| 577 | foreach ($node->childNodes as $childNode) { |
||
| 578 | if ($childNode->localName === "simpleContent") { |
||
| 579 | $isSimple = true; |
||
| 580 | break; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | $type = $isSimple ? new ComplexTypeSimpleContent($schema, $node->getAttribute("name")) : new ComplexType($schema, $node->getAttribute("name")); |
||
| 585 | |||
| 586 | $type->setDoc($this->getDocumentation($node)); |
||
| 587 | if ($node->getAttribute("name")) { |
||
| 588 | $schema->addType($type); |
||
| 589 | } |
||
| 590 | |||
| 591 | return $this->makeCallbackCallback( |
||
| 592 | $type, |
||
| 593 | $node, |
||
| 594 | function ( |
||
| 595 | DOMElement $node, |
||
| 596 | DOMElement $childNode |
||
| 597 | ) use( |
||
| 598 | $schema, |
||
| 599 | $type |
||
| 600 | ) { |
||
| 601 | $this->loadComplexTypeFromChildNode( |
||
| 602 | $type, |
||
| 603 | $node, |
||
| 604 | $childNode, |
||
| 605 | $schema |
||
| 606 | ); |
||
| 607 | }, |
||
| 608 | $callback |
||
| 609 | ); |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param Closure|null $callback |
||
| 614 | * |
||
| 615 | * @return Closure |
||
| 616 | */ |
||
| 617 | private function makeCallbackCallback( |
||
| 618 | Type $type, |
||
| 619 | DOMElement $node, |
||
| 620 | Closure $callbackCallback, |
||
| 621 | $callback = null |
||
| 622 | ) { |
||
| 623 | return function ( |
||
| 624 | ) use ( |
||
| 625 | $type, |
||
| 626 | $node, |
||
| 627 | $callbackCallback, |
||
| 628 | $callback |
||
| 629 | ) { |
||
| 630 | $this->runCallbackAgainstDOMNodeList( |
||
| 631 | $type, |
||
| 632 | $node, |
||
| 633 | $callbackCallback, |
||
| 634 | $callback |
||
| 635 | ); |
||
| 636 | }; |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @param Closure|null $callback |
||
| 641 | */ |
||
| 642 | private function runCallbackAgainstDOMNodeList( |
||
| 643 | Type $type, |
||
| 644 | DOMElement $node, |
||
| 645 | Closure $againstNodeList, |
||
| 646 | $callback = null |
||
| 647 | ) { |
||
| 648 | $this->fillTypeNode($type, $node, true); |
||
| 649 | |||
| 650 | foreach ($node->childNodes as $childNode) { |
||
| 651 | if ($childNode instanceof DOMElement) { |
||
| 652 | $againstNodeList( |
||
| 653 | $node, |
||
| 654 | $childNode |
||
| 655 | ); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | if ($callback) { |
||
| 660 | call_user_func($callback, $type); |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 664 | private function loadComplexTypeFromChildNode( |
||
| 665 | BaseComplexType $type, |
||
| 666 | DOMElement $node, |
||
| 667 | DOMElement $childNode, |
||
| 668 | Schema $schema |
||
| 669 | ) { |
||
| 670 | if ( |
||
| 671 | in_array( |
||
| 672 | $childNode->localName, |
||
| 673 | [ |
||
| 674 | 'sequence', |
||
| 675 | 'choice', |
||
| 676 | 'all', |
||
| 677 | ] |
||
| 678 | ) |
||
| 679 | ) { |
||
| 680 | $this->maybeLoadSequenceFromElementContainer( |
||
| 681 | $type, |
||
| 682 | $childNode |
||
| 683 | ); |
||
| 684 | } elseif ($childNode->localName === 'attribute') { |
||
| 685 | $attribute = $this->getAttributeFromAttributeOrRef( |
||
| 686 | $childNode, |
||
| 687 | $schema, |
||
| 688 | $node |
||
| 689 | ); |
||
| 690 | |||
| 691 | $type->addAttribute($attribute); |
||
| 692 | } elseif ( |
||
| 693 | $childNode->localName === 'group' && |
||
| 694 | $type instanceof ComplexType |
||
| 695 | ) { |
||
| 696 | $this->addGroupAsElement( |
||
| 697 | $schema, |
||
| 698 | $node, |
||
| 699 | $childNode, |
||
| 700 | $type |
||
| 701 | ); |
||
| 702 | } elseif ($childNode->localName === 'attributeGroup') { |
||
| 703 | AttributeGroup::findSomethingLikeThis( |
||
| 704 | $this, |
||
| 705 | $schema, |
||
| 706 | $node, |
||
| 707 | $childNode, |
||
| 708 | $type |
||
| 709 | ); |
||
| 710 | } |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @param Closure|null $callback |
||
| 715 | * |
||
| 716 | * @return Closure |
||
| 717 | */ |
||
| 718 | private function loadSimpleType(Schema $schema, DOMElement $node, $callback = null) |
||
| 719 | { |
||
| 720 | $type = new SimpleType($schema, $node->getAttribute("name")); |
||
| 721 | $type->setDoc($this->getDocumentation($node)); |
||
| 722 | if ($node->getAttribute("name")) { |
||
| 723 | $schema->addType($type); |
||
| 724 | } |
||
| 725 | |||
| 726 | static $methods = [ |
||
| 727 | 'union' => 'loadUnion', |
||
| 728 | 'list' => 'loadList', |
||
| 729 | ]; |
||
| 730 | |||
| 731 | return $this->makeCallbackCallback( |
||
| 732 | $type, |
||
| 733 | $node, |
||
| 734 | function ( |
||
| 735 | DOMElement $node, |
||
| 736 | DOMElement $childNode |
||
| 737 | ) use ( |
||
| 738 | $methods, |
||
| 739 | $type |
||
| 740 | ) { |
||
| 741 | $this->maybeCallMethod( |
||
| 742 | $methods, |
||
| 743 | $childNode->localName, |
||
| 744 | $childNode, |
||
| 745 | $type, |
||
| 746 | $childNode |
||
| 747 | ); |
||
| 748 | }, |
||
| 749 | $callback |
||
| 750 | ); |
||
| 751 | } |
||
| 752 | |||
| 753 | private function loadList(SimpleType $type, DOMElement $node) |
||
| 754 | { |
||
| 755 | if ($node->hasAttribute("itemType")) { |
||
| 756 | /** |
||
| 757 | * @var SimpleType $listType |
||
| 758 | */ |
||
| 759 | $listType = $this->findSomeType($type, $node, 'itemType'); |
||
| 760 | $type->setList($listType); |
||
| 761 | } else { |
||
| 762 | $addCallback = function (SimpleType $list) use ($type) { |
||
| 763 | $type->setList($list); |
||
| 764 | }; |
||
| 765 | |||
| 766 | $this->loadTypeWithCallbackOnChildNodes( |
||
| 767 | $type->getSchema(), |
||
| 768 | $node, |
||
| 769 | $addCallback |
||
| 770 | ); |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | private function loadTypeWithCallbackOnChildNodes( |
||
| 775 | Schema $schema, |
||
| 776 | DOMNode $node, |
||
| 777 | Closure $callback |
||
| 778 | ) { |
||
| 779 | foreach ($node->childNodes as $childNode) { |
||
| 780 | $this->loadTypeWithCallback($schema, $childNode, $callback); |
||
| 781 | } |
||
| 782 | } |
||
| 783 | |||
| 784 | private function loadTypeWithCallback( |
||
| 785 | Schema $schema, |
||
| 786 | DOMNode $childNode, |
||
| 787 | Closure $callback |
||
| 788 | ) { |
||
| 789 | if (! ($childNode instanceof DOMElement)) { |
||
| 790 | return; |
||
| 791 | } |
||
| 792 | $methods = [ |
||
| 793 | 'complexType' => 'loadComplexType', |
||
| 794 | 'simpleType' => 'loadSimpleType', |
||
| 795 | ]; |
||
| 796 | |||
| 797 | $func = $this->maybeCallMethod( |
||
| 798 | $methods, |
||
| 799 | $childNode->localName, |
||
| 800 | $childNode, |
||
| 801 | $schema, |
||
| 802 | $childNode, |
||
| 803 | $callback |
||
| 804 | ); |
||
| 805 | |||
| 806 | if ($func instanceof Closure) { |
||
| 807 | call_user_func($func); |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * @return SchemaItem |
||
| 813 | */ |
||
| 814 | private function findSomeType( |
||
| 815 | SchemaItem $fromThis, |
||
| 816 | DOMElement $node, |
||
| 817 | string $attributeName |
||
| 818 | ) { |
||
| 819 | return $this->findSomeTypeFromAttribute( |
||
| 820 | $fromThis, |
||
| 821 | $node, |
||
| 822 | $node->getAttribute($attributeName) |
||
| 823 | ); |
||
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @return SchemaItem |
||
| 828 | */ |
||
| 829 | private function findSomeTypeFromAttribute( |
||
| 830 | SchemaItem $fromThis, |
||
| 831 | DOMElement $node, |
||
| 832 | string $attributeName |
||
| 833 | ) { |
||
| 834 | /** |
||
| 835 | * @var SchemaItem $out |
||
| 836 | */ |
||
| 837 | $out = $this->findSomething( |
||
| 838 | 'findType', |
||
| 839 | $fromThis->getSchema(), |
||
| 840 | $node, |
||
| 841 | $attributeName |
||
| 842 | ); |
||
| 843 | |||
| 844 | return $out; |
||
| 845 | } |
||
| 846 | |||
| 847 | private function loadUnion(SimpleType $type, DOMElement $node) |
||
| 848 | { |
||
| 849 | if ($node->hasAttribute("memberTypes")) { |
||
| 850 | $types = preg_split('/\s+/', $node->getAttribute("memberTypes")); |
||
| 851 | foreach ($types as $typeName) { |
||
| 852 | /** |
||
| 853 | * @var SimpleType $unionType |
||
| 854 | */ |
||
| 855 | $unionType = $this->findSomeTypeFromAttribute( |
||
| 856 | $type, |
||
| 857 | $node, |
||
| 858 | $typeName |
||
| 859 | ); |
||
| 860 | $type->addUnion($unionType); |
||
| 861 | } |
||
| 862 | } |
||
| 863 | $addCallback = function (SimpleType $unType) use ($type) { |
||
| 864 | $type->addUnion($unType); |
||
| 865 | }; |
||
| 866 | |||
| 867 | $this->loadTypeWithCallbackOnChildNodes( |
||
| 868 | $type->getSchema(), |
||
| 869 | $node, |
||
| 870 | $addCallback |
||
| 871 | ); |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * @param bool $checkAbstract |
||
| 876 | */ |
||
| 877 | private function fillTypeNode(Type $type, DOMElement $node, $checkAbstract = false) |
||
| 878 | { |
||
| 879 | |||
| 880 | if ($checkAbstract) { |
||
| 881 | $type->setAbstract($node->getAttribute("abstract") === "true" || $node->getAttribute("abstract") === "1"); |
||
| 882 | } |
||
| 883 | |||
| 884 | static $methods = [ |
||
| 885 | 'restriction' => 'loadRestriction', |
||
| 886 | 'extension' => 'maybeLoadExtensionFromBaseComplexType', |
||
| 887 | 'simpleContent' => 'fillTypeNode', |
||
| 888 | 'complexContent' => 'fillTypeNode', |
||
| 889 | ]; |
||
| 890 | |||
| 891 | foreach ($node->childNodes as $childNode) { |
||
| 892 | $this->maybeCallMethod( |
||
| 893 | $methods, |
||
| 894 | (string) $childNode->localName, |
||
| 895 | $childNode, |
||
| 896 | $type, |
||
| 897 | $childNode |
||
| 898 | ); |
||
| 899 | } |
||
| 900 | } |
||
| 901 | |||
| 902 | private function loadExtension(BaseComplexType $type, DOMElement $node) |
||
| 903 | { |
||
| 904 | $extension = new Extension(); |
||
| 905 | $type->setExtension($extension); |
||
| 906 | |||
| 907 | if ($node->hasAttribute("base")) { |
||
| 908 | $this->findAndSetSomeBase( |
||
| 909 | $type, |
||
| 910 | $extension, |
||
| 911 | $node |
||
| 912 | ); |
||
| 913 | } |
||
| 914 | |||
| 915 | $seqFromElement = function (DOMElement $childNode) use ($type) { |
||
| 916 | $this->maybeLoadSequenceFromElementContainer( |
||
| 917 | $type, |
||
| 918 | $childNode |
||
| 919 | ); |
||
| 920 | }; |
||
| 921 | |||
| 922 | $methods = [ |
||
| 923 | 'sequence' => $seqFromElement, |
||
| 924 | 'choice' => $seqFromElement, |
||
| 925 | 'all' => $seqFromElement, |
||
| 926 | 'attribute' => function ( |
||
| 927 | DOMElement $childNode |
||
| 928 | ) use ( |
||
| 929 | $node, |
||
| 930 | $type |
||
| 931 | ) { |
||
| 932 | $attribute = $this->getAttributeFromAttributeOrRef( |
||
| 933 | $childNode, |
||
| 934 | $type->getSchema(), |
||
| 935 | $node |
||
| 936 | ); |
||
| 937 | $type->addAttribute($attribute); |
||
| 938 | }, |
||
| 939 | 'attributeGroup' => function ( |
||
| 940 | DOMElement $childNode |
||
| 941 | ) use ( |
||
| 942 | $node, |
||
| 943 | $type |
||
| 944 | ) { |
||
| 945 | AttributeGroup::findSomethingLikeThis( |
||
| 946 | $this, |
||
| 947 | $type->getSchema(), |
||
| 948 | $node, |
||
| 949 | $childNode, |
||
| 950 | $type |
||
| 951 | ); |
||
| 952 | }, |
||
| 953 | ]; |
||
| 954 | |||
| 955 | foreach ($node->childNodes as $childNode) { |
||
| 956 | if (isset($methods[$childNode->localName])) { |
||
| 957 | $method = $methods[$childNode->localName]; |
||
| 958 | $method($childNode); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | } |
||
| 962 | |||
| 963 | private function findAndSetSomeBase( |
||
| 964 | Type $type, |
||
| 965 | Base $setBaseOnThis, |
||
| 966 | DOMElement $node |
||
| 967 | ) { |
||
| 968 | /** |
||
| 969 | * @var Type $parent |
||
| 970 | */ |
||
| 971 | $parent = $this->findSomeType($type, $node, 'base'); |
||
| 972 | $setBaseOnThis->setBase($parent); |
||
| 973 | } |
||
| 974 | |||
| 975 | private function maybeLoadExtensionFromBaseComplexType( |
||
| 976 | Type $type, |
||
| 977 | DOMElement $childNode |
||
| 978 | ) { |
||
| 979 | if (! ($type instanceof BaseComplexType)) { |
||
| 980 | throw new RuntimeException( |
||
| 981 | 'Argument 1 passed to ' . |
||
| 982 | __METHOD__ . |
||
| 983 | ' needs to be an instance of ' . |
||
| 984 | BaseComplexType::class . |
||
| 985 | ' when passed onto ' . |
||
| 986 | static::class . |
||
| 987 | '::loadExtension(), ' . |
||
| 988 | get_class($type) . |
||
| 989 | ' given.' |
||
| 990 | ); |
||
| 991 | } |
||
| 992 | $this->loadExtension($type, $childNode); |
||
| 993 | } |
||
| 994 | |||
| 995 | private function loadRestriction(Type $type, DOMElement $node) |
||
| 996 | { |
||
| 997 | $restriction = new Restriction(); |
||
| 998 | $type->setRestriction($restriction); |
||
| 999 | if ($node->hasAttribute("base")) { |
||
| 1000 | $this->findAndSetSomeBase($type, $restriction, $node); |
||
| 1001 | } else { |
||
| 1002 | $addCallback = function (Type $restType) use ($restriction) { |
||
| 1003 | $restriction->setBase($restType); |
||
| 1004 | }; |
||
| 1005 | |||
| 1006 | $this->loadTypeWithCallbackOnChildNodes( |
||
| 1007 | $type->getSchema(), |
||
| 1008 | $node, |
||
| 1009 | $addCallback |
||
| 1010 | ); |
||
| 1011 | } |
||
| 1012 | foreach ($node->childNodes as $childNode) { |
||
| 1013 | if (in_array($childNode->localName, |
||
| 1014 | [ |
||
| 1015 | 'enumeration', |
||
| 1016 | 'pattern', |
||
| 1017 | 'length', |
||
| 1018 | 'minLength', |
||
| 1019 | 'maxLength', |
||
| 1020 | 'minInclusive', |
||
| 1021 | 'maxInclusive', |
||
| 1022 | 'minExclusive', |
||
| 1023 | 'maxExclusive', |
||
| 1024 | 'fractionDigits', |
||
| 1025 | 'totalDigits', |
||
| 1026 | 'whiteSpace' |
||
| 1027 | ], true)) { |
||
| 1028 | $restriction->addCheck($childNode->localName, |
||
| 1029 | [ |
||
| 1030 | 'value' => $childNode->getAttribute("value"), |
||
| 1031 | 'doc' => $this->getDocumentation($childNode) |
||
| 1032 | ]); |
||
| 1033 | } |
||
| 1034 | } |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * @param string $typeName |
||
| 1039 | * |
||
| 1040 | * @return mixed[] |
||
| 1041 | */ |
||
| 1042 | private static function splitParts(DOMElement $node, $typeName) |
||
| 1043 | { |
||
| 1044 | $prefix = null; |
||
| 1045 | $name = $typeName; |
||
| 1046 | if (strpos($typeName, ':') !== false) { |
||
| 1047 | list ($prefix, $name) = explode(':', $typeName); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | $namespace = $node->lookupNamespaceUri($prefix ?: ''); |
||
| 1051 | return array( |
||
| 1052 | $name, |
||
| 1053 | $namespace, |
||
| 1054 | $prefix |
||
| 1055 | ); |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * |
||
| 1060 | * @param string $finder |
||
| 1061 | * @param Schema $schema |
||
| 1062 | * @param DOMElement $node |
||
| 1063 | * @param string $typeName |
||
| 1064 | * @throws TypeException |
||
| 1065 | * @return ElementItem|Group|AttributeItem|AttributeGroup|Type |
||
| 1066 | */ |
||
| 1067 | public function findSomething($finder, Schema $schema, DOMElement $node, $typeName) |
||
| 1068 | { |
||
| 1069 | list ($name, $namespace) = self::splitParts($node, $typeName); |
||
| 1070 | |||
| 1071 | $namespace = $namespace ?: $schema->getTargetNamespace(); |
||
| 1072 | |||
| 1073 | try { |
||
| 1074 | return $schema->$finder($name, $namespace); |
||
| 1075 | } catch (TypeNotFoundException $e) { |
||
| 1076 | throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", strtolower(substr($finder, 4)), $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e); |
||
| 1077 | } |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * @return Closure |
||
| 1082 | */ |
||
| 1083 | private function loadElementDef(Schema $schema, DOMElement $node) |
||
| 1084 | { |
||
| 1085 | return $this->loadAttributeOrElementDef($schema, $node, false); |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | private function fillItem(Item $element, DOMElement $node) |
||
| 1089 | { |
||
| 1090 | $localType = null; |
||
| 1091 | foreach ($node->childNodes as $childNode) { |
||
| 1092 | switch ($childNode->localName) { |
||
| 1093 | case 'complexType': |
||
| 1094 | case 'simpleType': |
||
| 1095 | $localType = $childNode; |
||
| 1096 | break 2; |
||
| 1097 | } |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | if ($localType) { |
||
| 1101 | $addCallback = function (Type $type) use ($element) { |
||
| 1102 | $element->setType($type); |
||
| 1103 | }; |
||
| 1104 | $this->loadTypeWithCallback( |
||
| 1105 | $element->getSchema(), |
||
| 1106 | $localType, |
||
| 1107 | $addCallback |
||
| 1108 | ); |
||
| 1109 | } else { |
||
| 1110 | |||
| 1111 | if ($node->getAttribute("type")) { |
||
| 1112 | /** |
||
| 1113 | * @var Type $type |
||
| 1114 | */ |
||
| 1115 | $type = $this->findSomeType($element, $node, 'type'); |
||
| 1116 | } else { |
||
| 1117 | /** |
||
| 1118 | * @var Type $type |
||
| 1119 | */ |
||
| 1120 | $type = $this->findSomeTypeFromAttribute( |
||
| 1121 | $element, |
||
| 1122 | $node, |
||
| 1123 | ($node->lookupPrefix(self::XSD_NS) . ':anyType') |
||
| 1124 | ); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | $element->setType($type); |
||
| 1128 | } |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * @return Closure |
||
| 1133 | */ |
||
| 1134 | private function loadImport(Schema $schema, DOMElement $node) |
||
| 1135 | { |
||
| 1136 | $base = urldecode($node->ownerDocument->documentURI); |
||
| 1137 | $file = UrlUtils::resolveRelativeUrl($base, $node->getAttribute("schemaLocation")); |
||
| 1138 | if ($node->hasAttribute("namespace") |
||
| 1139 | && isset(self::$globalSchemaInfo[$node->getAttribute("namespace")]) |
||
| 1140 | && isset($this->loadedFiles[self::$globalSchemaInfo[$node->getAttribute("namespace")]]) |
||
| 1141 | ) { |
||
| 1142 | |||
| 1143 | $schema->addSchema($this->loadedFiles[self::$globalSchemaInfo[$node->getAttribute("namespace")]]); |
||
| 1144 | |||
| 1145 | return function () { |
||
| 1146 | }; |
||
| 1147 | } elseif ($node->hasAttribute("namespace") |
||
| 1148 | && isset($this->loadedFiles[$this->getNamespaceSpecificFileIndex($file, $node->getAttribute("namespace"))])) { |
||
| 1149 | $schema->addSchema($this->loadedFiles[$this->getNamespaceSpecificFileIndex($file, $node->getAttribute("namespace"))]); |
||
| 1150 | return function () { |
||
| 1151 | }; |
||
| 1152 | } elseif (isset($this->loadedFiles[$file])) { |
||
| 1153 | $schema->addSchema($this->loadedFiles[$file]); |
||
| 1154 | return function () { |
||
| 1155 | }; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | if (!$node->getAttribute("namespace")) { |
||
| 1159 | $this->loadedFiles[$file] = $newSchema = $schema; |
||
| 1160 | } else { |
||
| 1161 | $this->loadedFiles[$file] = $newSchema = new Schema(); |
||
| 1162 | $newSchema->addSchema($this->getGlobalSchema()); |
||
| 1163 | } |
||
| 1164 | |||
| 1165 | $xml = $this->getDOM(isset($this->knownLocationSchemas[$file]) ? $this->knownLocationSchemas[$file] : $file); |
||
| 1166 | |||
| 1167 | $callbacks = $this->schemaNode($newSchema, $xml->documentElement, $schema); |
||
| 1168 | |||
| 1169 | if ($node->getAttribute("namespace")) { |
||
| 1170 | $schema->addSchema($newSchema); |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | |||
| 1174 | return function () use ($callbacks) { |
||
| 1175 | foreach ($callbacks as $callback) { |
||
| 1176 | call_user_func($callback); |
||
| 1177 | } |
||
| 1178 | }; |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * @var Schema|null |
||
| 1183 | */ |
||
| 1184 | private $globalSchema; |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * |
||
| 1188 | * @return Schema |
||
| 1189 | */ |
||
| 1190 | public function getGlobalSchema() |
||
| 1191 | { |
||
| 1192 | if (!$this->globalSchema) { |
||
| 1193 | $callbacks = array(); |
||
| 1194 | $globalSchemas = array(); |
||
| 1195 | foreach (self::$globalSchemaInfo as $namespace => $uri) { |
||
| 1196 | $this->loadedFiles[$uri] = $globalSchemas [$namespace] = $schema = new Schema(); |
||
| 1197 | if ($namespace === self::XSD_NS) { |
||
| 1198 | $this->globalSchema = $schema; |
||
| 1199 | } |
||
| 1200 | $xml = $this->getDOM($this->knownLocationSchemas[$uri]); |
||
| 1201 | $callbacks = array_merge($callbacks, $this->schemaNode($schema, $xml->documentElement)); |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | $globalSchemas[self::XSD_NS]->addType(new SimpleType($globalSchemas[self::XSD_NS], "anySimpleType")); |
||
| 1205 | $globalSchemas[self::XSD_NS]->addType(new SimpleType($globalSchemas[self::XSD_NS], "anyType")); |
||
| 1206 | |||
| 1207 | $globalSchemas[self::XML_NS]->addSchema($globalSchemas[self::XSD_NS], self::XSD_NS); |
||
| 1208 | $globalSchemas[self::XSD_NS]->addSchema($globalSchemas[self::XML_NS], self::XML_NS); |
||
| 1209 | |||
| 1210 | foreach ($callbacks as $callback) { |
||
| 1211 | $callback(); |
||
| 1212 | } |
||
| 1213 | } |
||
| 1214 | |||
| 1215 | /** |
||
| 1216 | * @var Schema $out |
||
| 1217 | */ |
||
| 1218 | $out = $this->globalSchema; |
||
| 1219 | |||
| 1220 | return $out; |
||
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * @param DOMElement $node |
||
| 1225 | * @param string $file |
||
| 1226 | * |
||
| 1227 | * @return Schema |
||
| 1228 | */ |
||
| 1229 | public function readNode(DOMElement $node, $file = 'schema.xsd') |
||
| 1230 | { |
||
| 1231 | $fileKey = $node->hasAttribute('targetNamespace') ? $this->getNamespaceSpecificFileIndex($file, $node->getAttribute('targetNamespace')) : $file; |
||
| 1232 | $this->loadedFiles[$fileKey] = $rootSchema = new Schema(); |
||
| 1233 | |||
| 1234 | $rootSchema->addSchema($this->getGlobalSchema()); |
||
| 1235 | $callbacks = $this->schemaNode($rootSchema, $node); |
||
| 1236 | |||
| 1237 | foreach ($callbacks as $callback) { |
||
| 1238 | call_user_func($callback); |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | return $rootSchema; |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * It is possible that a single file contains multiple <xsd:schema/> nodes, for instance in a WSDL file. |
||
| 1246 | * |
||
| 1247 | * Each of these <xsd:schema/> nodes typically target a specific namespace. Append the target namespace to the |
||
| 1248 | * file to distinguish between multiple schemas in a single file. |
||
| 1249 | * |
||
| 1250 | * @param string $file |
||
| 1251 | * @param string $targetNamespace |
||
| 1252 | * |
||
| 1253 | * @return string |
||
| 1254 | */ |
||
| 1255 | private function getNamespaceSpecificFileIndex($file, $targetNamespace) |
||
| 1256 | { |
||
| 1257 | return $file . '#' . $targetNamespace; |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * @param string $content |
||
| 1262 | * @param string $file |
||
| 1263 | * |
||
| 1264 | * @return Schema |
||
| 1265 | * |
||
| 1266 | * @throws IOException |
||
| 1267 | */ |
||
| 1268 | public function readString($content, $file = 'schema.xsd') |
||
| 1269 | { |
||
| 1270 | $xml = new DOMDocument('1.0', 'UTF-8'); |
||
| 1271 | if (!$xml->loadXML($content)) { |
||
| 1272 | throw new IOException("Can't load the schema"); |
||
| 1273 | } |
||
| 1274 | $xml->documentURI = $file; |
||
| 1275 | |||
| 1276 | return $this->readNode($xml->documentElement, $file); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @param string $file |
||
| 1281 | * |
||
| 1282 | * @return Schema |
||
| 1283 | */ |
||
| 1284 | public function readFile($file) |
||
| 1288 | } |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * @param string $file |
||
| 1292 | * |
||
| 1293 | * @return DOMDocument |
||
| 1294 | * |
||
| 1295 | * @throws IOException |
||
| 1296 | */ |
||
| 1297 | private function getDOM($file) |
||
| 1298 | { |
||
| 1299 | $xml = new DOMDocument('1.0', 'UTF-8'); |
||
| 1300 | if (!$xml->load($file)) { |
||
| 1301 | throw new IOException("Can't load the file $file"); |
||
| 1302 | } |
||
| 1303 | return $xml; |
||
| 1304 | } |
||
| 1305 | } |
||
| 1306 |