-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GoetasWebservices\XML\XSDReader\Schema\Element\Any; | ||
|
||
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem; | ||
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax; | ||
use GoetasWebservices\XML\XSDReader\Schema\Element\MinMaxTrait; | ||
use GoetasWebservices\XML\XSDReader\Schema\Schema; | ||
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem; | ||
use GoetasWebservices\XML\XSDReader\Schema\SchemaItemTrait; | ||
|
||
/** | ||
* Represents https://www.w3schools.com/xml/el_any.asp. | ||
*/ | ||
class Any implements SchemaItem, ElementItem, InterfaceSetMinMax | ||
{ | ||
use MinMaxTrait; | ||
use SchemaItemTrait; | ||
|
||
protected ?string $namespace = null; | ||
protected ProcessContents $processContents = ProcessContents::Strict; | ||
protected ?string $id = null; | ||
|
||
public function __construct(Schema $schema) | ||
{ | ||
$this->schema = $schema; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'any'; | ||
} | ||
|
||
public function getNamespace(): ?string | ||
{ | ||
return $this->namespace; | ||
} | ||
|
||
public function setNamespace(?string $namespace): void | ||
{ | ||
$this->namespace = $namespace; | ||
} | ||
|
||
public function getProcessContents(): ProcessContents | ||
{ | ||
return $this->processContents; | ||
} | ||
|
||
public function setProcessContents(ProcessContents $processContents): void | ||
{ | ||
$this->processContents = $processContents; | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(?string $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace GoetasWebservices\XML\XSDReader\Schema\Element\Any; | ||
|
||
enum ProcessContents: string | ||
{ | ||
case Strict = 'strict'; | ||
case Lax = 'lax'; | ||
case Skip = 'skip'; | ||
|
||
public static function default(): self | ||
{ | ||
return self::Strict; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use GoetasWebservices\XML\XSDReader\Schema\Element\Any\Any; | ||
use GoetasWebservices\XML\XSDReader\Schema\Element\Any\ProcessContents; | ||
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; | ||
use GoetasWebservices\XML\XSDReader\Tests\BaseTest; | ||
|
||
class AnyTest extends BaseTest | ||
{ | ||
public function testBase(): void | ||
{ | ||
$schema = $this->reader->readString( | ||
' | ||
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<xs:element name="customerData"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:any /> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema>' | ||
); | ||
|
||
$myElement = $schema->findElement('customerData', 'http://www.example.com'); | ||
$myType = $myElement->getType(); | ||
|
||
self::assertInstanceOf(ComplexType::class, $myType); | ||
|
||
$elements = $myType->getElements(); | ||
|
||
self::assertCount(1, $elements); | ||
self::assertInstanceOf(Any::class, $any = $elements[0]); | ||
|
||
self::assertSame($schema, $any->getSchema()); | ||
self::assertSame('any', $any->getName()); | ||
self::assertNull($any->getNamespace()); | ||
self::assertSame(ProcessContents::Strict, $any->getProcessContents()); | ||
self::assertNull($any->getId()); | ||
self::assertSame(1, $any->getMin()); | ||
self::assertSame(1, $any->getMax()); | ||
self::assertSame('', $any->getDoc()); | ||
} | ||
|
||
public function testAllArgsSet(): void | ||
{ | ||
$schema = $this->reader->readString( | ||
' | ||
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<xs:element name="customerData"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:any id="hello" minOccurs="0" maxOccurs="2" processContents="lax" namespace="##any"> | ||
<xs:annotation> | ||
<xs:documentation>Any description</xs:documentation> | ||
</xs:annotation> | ||
</xs:any> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema>' | ||
); | ||
|
||
$myElement = $schema->findElement('customerData', 'http://www.example.com'); | ||
$myType = $myElement->getType(); | ||
|
||
self::assertInstanceOf(ComplexType::class, $myType); | ||
|
||
$elements = $myType->getElements(); | ||
|
||
self::assertCount(1, $elements); | ||
self::assertInstanceOf(Any::class, $any = $elements[0]); | ||
|
||
self::assertSame($schema, $any->getSchema()); | ||
self::assertSame('any', $any->getName()); | ||
self::assertSame('##any', $any->getNamespace()); | ||
self::assertSame(ProcessContents::Lax, $any->getProcessContents()); | ||
self::assertSame('hello', $any->getId()); | ||
self::assertSame(0, $any->getMin()); | ||
self::assertSame(2, $any->getMax()); | ||
self::assertSame('Any description', $any->getDoc()); | ||
} | ||
|
||
public function testCombinationWithOtherElements(): void | ||
{ | ||
$schema = $this->reader->readString( | ||
' | ||
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<xs:element name="customerData"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="firstName" type="xs:string" /> | ||
<xs:element name="lastName" type="xs:string" /> | ||
<xs:any id="hello" minOccurs="0" maxOccurs="2" processContents="lax" namespace="##any" /> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema>' | ||
); | ||
|
||
$myElement = $schema->findElement('customerData', 'http://www.example.com'); | ||
$myType = $myElement->getType(); | ||
|
||
self::assertInstanceOf(ComplexType::class, $myType); | ||
|
||
$elements = $myType->getElements(); | ||
|
||
self::assertCount(3, $elements); | ||
self::assertInstanceOf(Any::class, $any = $elements[2]); | ||
} | ||
} |