Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #168 add choice-in-sequence handling #176

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"

processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false">
<testsuites>
<testsuite name="Goetas XSD to PHP Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>

verbose="false"
>
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Goetas XSD to PHP Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>


112 changes: 59 additions & 53 deletions src/Php/PhpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(NamingStrategy $namingStrategy, LoggerInterface $log

private $classes = [];

public function convert(array $schemas)
public function convert(array $schemas): array
{
$visited = [];
$this->classes = [];
Expand All @@ -69,7 +69,7 @@ public function convert(array $schemas)
/**
* @return PHPClass[]
*/
private function getTypes()
private function getTypes(): array
{
uasort($this->classes, function ($a, $b) {
return strcmp($a['class']->getFullName(), $b['class']->getFullName());
Expand All @@ -84,7 +84,7 @@ private function getTypes()
return $ret;
}

private function navigate(Schema $schema, array &$visited)
private function navigate(Schema $schema, array &$visited): void
{
if (isset($visited[spl_object_hash($schema)])) {
return;
Expand All @@ -105,7 +105,7 @@ private function navigate(Schema $schema, array &$visited)
}
}

private function visitTypeBase(PHPClass $class, Type $type)
private function visitTypeBase(PHPClass $class, Type $type): void
{
$class->setAbstract($type->isAbstract());

Expand All @@ -119,46 +119,48 @@ private function visitTypeBase(PHPClass $class, Type $type)
$this->visitComplexType($class, $type);
}
}

/**
* Process xsd:complexType xsd:sequence xsd:element
*
* @param PHPClass $class
* @param Schema $schema
* @param Sequence $sequence
*/
private function visitSequence(PHPClass $class, Schema $schema, Sequence $sequence)
{
foreach ($sequence->getElements() as $childSequence) {
if ($childSequence instanceof Group) {
$this->visitGroup($class, $schema, $childSequence);
} else {
$property = $this->visitElement($class, $schema, $childSequence);
$class->addProperty($property);
}
}
}


/**
* Process xsd:complexType xsd:sequence xsd:element
*
* @param PHPClass $class
* @param Schema $schema
* @param Sequence $sequence
*/
private function visitSequence(PHPClass $class, Schema $schema, Sequence $sequence): void
{
foreach ($sequence->getElements() as $childSequence) {
if ($childSequence instanceof Group) {
goetas marked this conversation as resolved.
Show resolved Hide resolved
$this->visitGroup($class, $schema, $childSequence);
} elseif ($childSequence instanceof Choice) {
$this->visitChoice($class, $schema, $childSequence);
} else {
$property = $this->visitElement($class, $schema, $childSequence);
$class->addProperty($property);
}
}
}

/**
* Process xsd:complexType xsd:choice xsd:element
*
* @param PHPClass $class
* @param Schema $schema
* @param Choice $choice
*/
private function visitChoice(PHPClass $class, Schema $schema, Choice $choice)
private function visitChoice(PHPClass $class, Schema $schema, Choice $choice): void
{
foreach ($choice->getElements() as $choiceOption) {
if ($choiceOption instanceof Sequence) {
$this->visitSequence($class, $schema, $choiceOption);
} else {
$property = $this->visitElement($class, $schema, $choiceOption);
$class->addProperty($property);
}
if ($choiceOption instanceof Sequence) {
$this->visitSequence($class, $schema, $choiceOption);
} else {
$property = $this->visitElement($class, $schema, $choiceOption);
$class->addProperty($property);
}
}
}
private function visitGroup(PHPClass $class, Schema $schema, Group $group)

private function visitGroup(PHPClass $class, Schema $schema, Group $group): void
{
foreach ($group->getElements() as $childGroup) {
if ($childGroup instanceof Group) {
Expand All @@ -170,7 +172,7 @@ private function visitGroup(PHPClass $class, Schema $schema, Group $group)
}
}

private function visitAttributeGroup(PHPClass $class, Schema $schema, AttributeGroup $att)
private function visitAttributeGroup(PHPClass $class, Schema $schema, AttributeGroup $att): void
{
foreach ($att->getAttributes() as $childAttr) {
if ($childAttr instanceof AttributeGroup) {
Expand Down Expand Up @@ -223,7 +225,7 @@ public function visitElementDef(ElementDef $element)
return $this->classes[spl_object_hash($element)]['class'];
}

public function isSkip($class)
public function isSkip($class): bool
{
return !empty($this->skipByType[spl_object_hash($class)]);
}
Expand All @@ -238,12 +240,12 @@ private function findPHPName(Type $type)
substr($className, $pos + 1),
substr($className, 0, $pos),
];
} else {
return [
$className,
null,
];
}

return [
$className,
null,
];
}

$name = $this->getNamingStrategy()->getTypeName($type);
Expand Down Expand Up @@ -318,7 +320,7 @@ public function visitType(Type $type, $force = false)
/**
* @param string $name
*
* @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClass
* @return PHPClass
*/
private function visitTypeAnonymous(Type $type, $name, PHPClass $parentClass)
{
Expand All @@ -340,13 +342,13 @@ private function visitTypeAnonymous(Type $type, $name, PHPClass $parentClass)
return $this->classes[spl_object_hash($type)]['class'];
}

private function visitComplexType(PHPClass $class, ComplexType $type)
private function visitComplexType(PHPClass $class, ComplexType $type): void
{
$schema = $type->getSchema();
foreach ($type->getElements() as $element) {
if ($element instanceof Sequence) {
$this->visitSequence($class, $schema, $element);
} elseif ($element instanceof Choice) {
if ($element instanceof Sequence) {
$this->visitSequence($class, $schema, $element);
} elseif ($element instanceof Choice) {
$this->visitChoice($class, $schema, $element);
} elseif ($element instanceof Group) {
$this->visitGroup($class, $schema, $element);
Expand All @@ -357,7 +359,7 @@ private function visitComplexType(PHPClass $class, ComplexType $type)
}
}

private function visitSimpleType(PHPClass $class, SimpleType $type)
private function visitSimpleType(PHPClass $class, SimpleType $type): void
{
if ($restriction = $type->getRestriction()) {
$parent = $restriction->getBase();
Expand Down Expand Up @@ -387,7 +389,7 @@ private function visitSimpleType(PHPClass $class, SimpleType $type)
}
}

private function handleClassExtension(PHPClass $class, Type $type)
private function handleClassExtension(PHPClass $class, Type $type): void
{
if ($alias = $this->getTypeAlias($type)) {
$c = PHPClass::createFromFQCN($alias);
Expand All @@ -401,7 +403,7 @@ private function handleClassExtension(PHPClass $class, Type $type)
}
}

private function visitBaseComplexType(PHPClass $class, BaseComplexType $type)
private function visitBaseComplexType(PHPClass $class, BaseComplexType $type): void
{
$parent = $type->getParent();
if ($parent) {
Expand Down Expand Up @@ -449,7 +451,7 @@ private function visitAttribute(PHPClass $class, Schema $schema, AttributeItem $
* @param Element $element
* @param bool $arrayize
*
* @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPProperty
* @return PHPProperty
*/
private function visitElement(PHPClass $class, Schema $schema, ElementSingle $element, $arrayize = true)
{
Expand Down Expand Up @@ -484,7 +486,9 @@ private function visitElement(PHPClass $class, Schema $schema, ElementSingle $el
$property->setType(new PHPClassOf($arg));

return $property;
} elseif ($itemOfArray = $this->isArrayNestedElement($t)) {
}

if ($itemOfArray = $this->isArrayNestedElement($t)) {

if (!$t->getName()) {
if ($element instanceof ElementRef) {
Expand All @@ -502,7 +506,9 @@ private function visitElement(PHPClass $class, Schema $schema, ElementSingle $el
$property->setType(new PHPClassOf($elementProp));

return $property;
} elseif ($this->isArrayElement($element)) {
}

if ($this->isArrayElement($element)) {
$arg = new PHPArg($this->getNamingStrategy()->getPropertyName($element));

$arg->setType($this->findPHPElementClassName($class, $element));
Expand All @@ -528,9 +534,9 @@ private function findPHPClass(PHPClass $class, Item $node, $force = false)
}
if (!$node->getType()->getName()) {
return $this->visitTypeAnonymous($node->getType(), $node->getName(), $class);
} else {
return $this->visitType($node->getType(), $force);
}

return $this->visitType($node->getType(), $force);
}

private function typeHasValue(Type $type, PHPClass $parentClass, $name)
Expand Down
2 changes: 1 addition & 1 deletion tests/Issues/I138/I138Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class I138Test extends TestCase
{
public function testChioce()
public function testChoice()
{
$reader = new SchemaReader();
$schema = $reader->readFile(__DIR__ . '/data.xsd');
Expand Down
26 changes: 26 additions & 0 deletions tests/Issues/I168/I168Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace GoetasWebservices\Xsd\XsdToPhp\Tests\Issues\I168;

use GoetasWebservices\XML\XSDReader\SchemaReader;
use GoetasWebservices\Xsd\XsdToPhp\Naming\ShortNamingStrategy;
use GoetasWebservices\Xsd\XsdToPhp\Php\PhpConverter;
use PHPUnit\Framework\TestCase;

class I168Test extends TestCase
{
public function testChoice()
{
$reader = new SchemaReader();
$schema = $reader->readFile(__DIR__ . '/data.xsd');

$phpConv = new PhpConverter(new ShortNamingStrategy());
$phpConv->addNamespace('http://www.example.com/', 'Epa');

$items = $phpConv->convert([$schema]);

$this->assertTrue($items['Epa\ComplexType']->getProperty('key')->getNullable());
$this->assertTrue($items['Epa\ComplexType']->getProperty('stream')->getNullable());
$this->assertTrue($items['Epa\ComplexType']->getProperty('packet')->getNullable());
}
}
14 changes: 14 additions & 0 deletions tests/Issues/I168/data.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/" elementFormDefault="qualified">
<xs:complexType name="ComplexType">
<xs:choice>
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="stream" type="xs:string" minOccurs="0" nillable="true"/>
</xs:sequence>
<xs:sequence>
<xs:element name="packet" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:schema>