From 0468ebe22bc0e8f9ca47e7d004223b83a3d48022 Mon Sep 17 00:00:00 2001 From: Tobias Hoffmann Date: Fri, 29 May 2015 18:35:50 +0200 Subject: [PATCH 01/15] Added enumerations implemented by constants. --- lib/Php/ClassGenerator.php | 872 ++++++++++++++------------- lib/Php/PhpConverter.php | 972 +++++++++++++++++------------- lib/Php/Structure/PHPClass.php | 488 +++++++-------- lib/Php/Structure/PhpConstant.php | 47 ++ 4 files changed, 1307 insertions(+), 1072 deletions(-) create mode 100644 lib/Php/Structure/PhpConstant.php diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 19c524e..ac31e77 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -3,425 +3,469 @@ use Doctrine\Common\Inflector\Inflector; use Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass; -use Goetas\Xsd\XsdToPhp\Php\Structure\PHPProperty; use Goetas\Xsd\XsdToPhp\Php\Structure\PHPClassOf; +use Goetas\Xsd\XsdToPhp\Php\Structure\PhpConstant; +use Goetas\Xsd\XsdToPhp\Php\Structure\PHPProperty; use Zend\Code\Generator; -use Zend\Code\Generator\PropertyGenerator; -use Zend\Code\Generator\DocBlockGenerator; use Zend\Code\Generator\DocBlock\Tag\ParamTag; +use Zend\Code\Generator\DocBlock\Tag\PropertyTag; use Zend\Code\Generator\DocBlock\Tag\ReturnTag; +use Zend\Code\Generator\DocBlockGenerator; use Zend\Code\Generator\MethodGenerator; use Zend\Code\Generator\ParameterGenerator; -use Zend\Code\Generator\DocBlock\Tag\PropertyTag; +use Zend\Code\Generator\PropertyGenerator; + +class ClassGenerator { + + private function handleBody( Generator\ClassGenerator $class, PHPClass $type ) { + foreach ( $type->getConstants() as $const ) { + $this->handleConstant( $class, $const ); + } + + foreach ( $type->getProperties() as $prop ) { + if ( $prop->getName() !== '__value' ) { + $this->handleProperty( $class, $prop ); + } + } + foreach ( $type->getProperties() as $prop ) { + if ( $prop->getName() !== '__value' ) { + $this->handleMethod( $class, $prop, $type ); + } + } + + if ( count( $type->getProperties() ) === 1 && $type->hasProperty( '__value' ) ) { + if ( ! count( $type->getConstants() ) ) { + return false; + } + $class->removeMethod( '__construct' ) + ->removeMethod( '__toString' ) + ->removeMethod( 'value' ) + ; + } + + return true; + } + + private function isNativeType( PHPClass $class ) { + return ! $class->getNamespace() && in_array( + $class->getName(), + [ + 'string', + 'int', + 'float', + 'integer', + 'boolean', + 'array', + 'mixed', + 'callable', + ] + ); + } + + private function getPhpType( PHPClass $class ) { + if ( ! $class->getNamespace() ) { + if ( $this->isNativeType( $class ) ) { + return $class->getName(); + } + + return "\\" . $class->getName(); + } + + return "\\" . $class->getFullName(); + } + + private function handleValueMethod( + Generator\ClassGenerator $generator, + PHPProperty $prop, + PHPClass $class, + $all = true + ) { + $type = $prop->getType(); + + $docblock = new DocBlockGenerator( 'Construct' ); + $paramTag = new ParamTag( "value", "mixed" ); + $paramTag->setTypes( ( $type ? $this->getPhpType( $type ) : "mixed" ) ); + + $docblock->setTag( $paramTag ); + + $param = new ParameterGenerator( "value" ); + if ( $type && ! $this->isNativeType( $type ) ) { + $param->setType( $this->getPhpType( $type ) ); + } + $method = new MethodGenerator( + "__construct", [ + $param, + ] + ); + $method->setDocBlock( $docblock ); + $method->setBody( "\$this->value(\$value);" ); + + $generator->addMethodFromGenerator( $method ); + + $docblock = new DocBlockGenerator( 'Gets or sets the inner value' ); + $paramTag = new ParamTag( "value", "mixed" ); + if ( $type && $type instanceof PHPClassOf ) { + $paramTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + } elseif ( $type ) { + $paramTag->setTypes( $this->getPhpType( $prop->getType() ) ); + } + $docblock->setTag( $paramTag ); + + $returnTag = new ReturnTag( "mixed" ); + + if ( $type && $type instanceof PHPClassOf ) { + $returnTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + } elseif ( $type ) { + $returnTag->setTypes( $this->getPhpType( $type ) ); + } + $docblock->setTag( $returnTag ); + + $param = new ParameterGenerator( "value" ); + $param->setDefaultValue( null ); + + if ( $type && ! $this->isNativeType( $type ) ) { + $param->setType( $this->getPhpType( $type ) ); + } + $method = new MethodGenerator( "value", [ ] ); + $method->setDocBlock( $docblock ); + + $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; + $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; + $methodBody .= "}" . PHP_EOL; + $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; + $method->setBody( $methodBody ); + + $generator->addMethodFromGenerator( $method ); + + $docblock = new DocBlockGenerator( 'Gets a string value' ); + $docblock->setTag( new ReturnTag( "string" ) ); + $method = new MethodGenerator( "__toString" ); + $method->setDocBlock( $docblock ); + $method->setBody( "return strval(\$this->" . $prop->getName() . ");" ); + $generator->addMethodFromGenerator( $method ); + } + + private function handleSetter( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + $methodBody = ''; + $docblock = new DocBlockGenerator(); + + $docblock->setShortDescription( "Sets a new " . $prop->getName() ); + + if ( $prop->getDoc() ) { + $docblock->setLongDescription( $prop->getDoc() ); + } + + $patramTag = new ParamTag( $prop->getName() ); + $docblock->setTag( $patramTag ); + + $return = new ReturnTag( "self" ); + $docblock->setTag( $return ); + + $type = $prop->getType(); + + $method = new MethodGenerator( "set" . Inflector::classify( $prop->getName() ) ); + + $parameter = new ParameterGenerator( $prop->getName(), "mixed" ); + + if ( $type && $type instanceof PHPClassOf ) { + $patramTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + $parameter->setType( "array" ); + + if ( $p = $this->isOneType( + $type->getArg() + ->getType() + ) + ) { + if ( ( $t = $p->getType() ) ) { + $patramTag->setTypes( $this->getPhpType( $t ) ); + } + } + } elseif ( $type ) { + if ( $this->isNativeType( $type ) ) { + $patramTag->setTypes( $this->getPhpType( $type ) ); + } elseif ( $p = $this->isOneType( $type ) ) { + if ( ( $t = $p->getType() ) && ! $this->isNativeType( $t ) ) { + $patramTag->setTypes( $this->getPhpType( $t ) ); + $parameter->setType( $this->getPhpType( $t ) ); + } elseif ( $t && ! $this->isNativeType( $t ) ) { + $patramTag->setTypes( $this->getPhpType( $t ) ); + $parameter->setType( $this->getPhpType( $t ) ); + } elseif ( $t ) { + $patramTag->setTypes( $this->getPhpType( $t ) ); + } + } else { + $patramTag->setTypes( $this->getPhpType( $type ) ); + $parameter->setType( $this->getPhpType( $type ) ); + } + } + + $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; + $methodBody .= "return \$this;"; + $method->setBody( $methodBody ); + $method->setDocBlock( $docblock ); + $method->setParameter( $parameter ); + + $generator->addMethodFromGenerator( $method ); + } + + private function handleGetter( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + + if ( $prop->getType() instanceof PHPClassOf ) { + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription( "isset " . $prop->getName() ); + if ( $prop->getDoc() ) { + $docblock->setLongDescription( $prop->getDoc() ); + } + + $patramTag = new ParamTag( "index", "scalar" ); + $docblock->setTag( $patramTag ); + + $docblock->setTag( new ReturnTag( "boolean" ) ); + + $paramIndex = new ParameterGenerator( "index", "mixed" ); + + $method = new MethodGenerator( "isset" . Inflector::classify( $prop->getName() ), [ $paramIndex ] ); + $method->setDocBlock( $docblock ); + $method->setBody( "return isset(\$this->" . $prop->getName() . "[\$index]);" ); + $generator->addMethodFromGenerator( $method ); + + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription( "unset " . $prop->getName() ); + if ( $prop->getDoc() ) { + $docblock->setLongDescription( $prop->getDoc() ); + } + + $patramTag = new ParamTag( "index", "scalar" ); + $docblock->setTag( $patramTag ); + $paramIndex = new ParameterGenerator( "index", "mixed" ); + + $docblock->setTag( new ReturnTag( "void" ) ); + + + $method = new MethodGenerator( "unset" . Inflector::classify( $prop->getName() ), [ $paramIndex ] ); + $method->setDocBlock( $docblock ); + $method->setBody( "unset(\$this->" . $prop->getName() . "[\$index]);" ); + $generator->addMethodFromGenerator( $method ); + } + // //// + + $docblock = new DocBlockGenerator(); + + $docblock->setShortDescription( "Gets as " . $prop->getName() ); + + if ( $prop->getDoc() ) { + $docblock->setLongDescription( $prop->getDoc() ); + } + + $tag = new ReturnTag( "mixed" ); + $type = $prop->getType(); + if ( $type && $type instanceof PHPClassOf ) { + $tt = $type->getArg() + ->getType(); + $tag->setTypes( $this->getPhpType( $tt ) . "[]" ); + if ( $p = $this->isOneType( $tt ) ) { + if ( ( $t = $p->getType() ) ) { + $tag->setTypes( $this->getPhpType( $t ) . "[]" ); + } + } + } elseif ( $type ) { + + if ( $p = $this->isOneType( $type ) ) { + if ( $t = $p->getType() ) { + $tag->setTypes( $this->getPhpType( $t ) ); + } + } else { + $tag->setTypes( $this->getPhpType( $type ) ); + } + } + + $docblock->setTag( $tag ); + + $method = new MethodGenerator( "get" . Inflector::classify( $prop->getName() ) ); + $method->setDocBlock( $docblock ); + $method->setBody( "return \$this->" . $prop->getName() . ";" ); + + $generator->addMethodFromGenerator( $method ); + } + + private function isOneType( PHPClass $type, $onlyParent = false ) { + if ( $onlyParent ) { + $e = $type->getExtends(); + if ( $e ) { + if ( $e->hasProperty( '__value' ) ) { + return $e->getProperty( '__value' ); + } + } + } else { + if ( $type->hasPropertyInHierarchy( '__value' ) && count( $type->getPropertiesInHierarchy() ) === 1 ) { + return $type->getPropertyInHierarchy( "__value" ); + } + } + } + + private function handleAdder( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + $type = $prop->getType(); + $propName = $type->getArg() + ->getName(); + + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription( "Adds as $propName" ); + + if ( $prop->getDoc() ) { + $docblock->setLongDescription( $prop->getDoc() ); + } + + $return = new ReturnTag(); + $return->setTypes( "self" ); + $docblock->setTag( $return ); + + $patramTag = new ParamTag( + $propName, $this->getPhpType( + $type->getArg() + ->getType() + ) + ); + $docblock->setTag( $patramTag ); + + $method = new MethodGenerator( "addTo" . Inflector::classify( $prop->getName() ) ); + + $parameter = new ParameterGenerator( $propName ); + $tt = $type->getArg() + ->getType(); + + if ( ! $this->isNativeType( $tt ) ) { + + if ( $p = $this->isOneType( $tt ) ) { + if ( ( $t = $p->getType() ) ) { + $patramTag->setTypes( $this->getPhpType( $t ) ); + + if ( ! $this->isNativeType( $t ) ) { + $parameter->setType( $this->getPhpType( $t ) ); + } + } + } elseif ( ! $this->isNativeType( $tt ) ) { + $parameter->setType( $this->getPhpType( $tt ) ); + } + } + + $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; + $methodBody .= "return \$this;"; + $method->setBody( $methodBody ); + $method->setDocBlock( $docblock ); + $method->setParameter( $parameter ); + + $generator->addMethodFromGenerator( $method ); + } + + private function handleMethod( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + if ( $prop->getType() instanceof PHPClassOf ) { + $this->handleAdder( $generator, $prop, $class ); + } + + $this->handleGetter( $generator, $prop, $class ); + $this->handleSetter( $generator, $prop, $class ); + } + + private function handleProperty( Generator\ClassGenerator $class, PHPProperty $prop ) { + $generatedProp = new PropertyGenerator( $prop->getName() ); + $generatedProp->setVisibility( PropertyGenerator::VISIBILITY_PRIVATE ); + + $class->addPropertyFromGenerator( $generatedProp ); + + if ( $prop->getType() && ( ! $prop->getType() + ->getNamespace() && $prop->getType() + ->getName() == "array" ) + ) { + // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); + } + + $docBlock = new DocBlockGenerator(); + $generatedProp->setDocBlock( $docBlock ); + + if ( $prop->getDoc() ) { + $docBlock->setLongDescription( $prop->getDoc() ); + } + $tag = new PropertyTag( $prop->getName(), 'mixed' ); + + $type = $prop->getType(); + + if ( $type && $type instanceof PHPClassOf ) { + $tt = $type->getArg() + ->getType(); + $tag->setTypes( $this->getPhpType( $tt ) . "[]" ); + if ( $p = $this->isOneType( $tt ) ) { + if ( ( $t = $p->getType() ) ) { + $tag->setTypes( $this->getPhpType( $t ) . "[]" ); + } + } + } elseif ( $type ) { + + if ( $this->isNativeType( $type ) ) { + $tag->setTypes( $this->getPhpType( $type ) ); + } elseif ( ( $p = $this->isOneType( $type ) ) && ( $t = $p->getType() ) ) { + $tag->setTypes( $this->getPhpType( $t ) ); + } else { + $tag->setTypes( $this->getPhpType( $prop->getType() ) ); + } + } + $docBlock->setTag( $tag ); + } + + private function handleConstant( Generator\ClassGenerator $class, PhpConstant $const ) { + $class->addConstant( $const->getName(), $const->getValue() ); + } + + public function generate( Generator\ClassGenerator $class, PHPClass $type ) { + $docblock = new DocBlockGenerator( "Class representing " . $type->getName() ); + if ( $type->getDoc() ) { + $docblock->setLongDescription( $type->getDoc() ); + } + $class->setNamespaceName( $type->getNamespace() ); + $class->setName( $type->getName() ); + $class->setDocblock( $docblock ); + + if ( $extends = $type->getExtends() ) { + + if ( $p = $this->isOneType( $extends ) ) { + $this->handleProperty( $class, $p ); + $this->handleValueMethod( $class, $p, $extends ); + } else { + + $class->setExtendedClass( $extends->getName() ); + + if ( $extends->getNamespace() != $type->getNamespace() ) { + if ( $extends->getName() == $type->getName() ) { + $class->addUse( + $type->getExtends() + ->getFullName(), + $extends->getName() . "Base" + ); + $class->setExtendedClass( $extends->getName() . "Base" ); + } else { + $class->addUse( $extends->getFullName() ); + } + } + } + } + + if ( $this->handleBody( $class, $type ) ) { + return true; + } + } -class ClassGenerator -{ - - private function handleBody(Generator\ClassGenerator $class, PHPClass $type) - { - foreach ($type->getProperties() as $prop) { - if ($prop->getName() !== '__value') { - $this->handleProperty($class, $prop); - } - } - foreach ($type->getProperties() as $prop) { - if ($prop->getName() !== '__value') { - $this->handleMethod($class, $prop, $type); - } - } - - if (count($type->getProperties()) === 1 && $type->hasProperty('__value')) { - return false; - } - - return true; - } - - private function isNativeType(PHPClass $class) - { - return ! $class->getNamespace() && in_array($class->getName(), [ - 'string', - 'int', - 'float', - 'integer', - 'boolean', - 'array', - 'mixed', - 'callable' - ]); - } - - private function getPhpType(PHPClass $class) - { - if (! $class->getNamespace()) { - if ($this->isNativeType($class)) { - return $class->getName(); - } - return "\\" . $class->getName(); - } - return "\\" . $class->getFullName(); - } - - private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true) - { - $type = $prop->getType(); - - $docblock = new DocBlockGenerator('Construct'); - $paramTag = new ParamTag("value", "mixed"); - $paramTag->setTypes(($type ? $this->getPhpType($type) : "mixed")); - - $docblock->setTag($paramTag); - - $param = new ParameterGenerator("value"); - if ($type && ! $this->isNativeType($type)) { - $param->setType($this->getPhpType($type)); - } - $method = new MethodGenerator("__construct", [ - $param - ]); - $method->setDocBlock($docblock); - $method->setBody("\$this->value(\$value);"); - - $generator->addMethodFromGenerator($method); - - $docblock = new DocBlockGenerator('Gets or sets the inner value'); - $paramTag = new ParamTag("value", "mixed"); - if ($type && $type instanceof PHPClassOf) { - $paramTag->setTypes($this->getPhpType($type->getArg() - ->getType()) . "[]"); - } elseif ($type) { - $paramTag->setTypes($this->getPhpType($prop->getType())); - } - $docblock->setTag($paramTag); - - $returnTag = new ReturnTag("mixed"); - - if ($type && $type instanceof PHPClassOf) { - $returnTag->setTypes($this->getPhpType($type->getArg() - ->getType()) . "[]"); - } elseif ($type) { - $returnTag->setTypes($this->getPhpType($type)); - } - $docblock->setTag($returnTag); - - $param = new ParameterGenerator("value"); - $param->setDefaultValue(null); - - if ($type && ! $this->isNativeType($type)) { - $param->setType($this->getPhpType($type)); - } - $method = new MethodGenerator("value", []); - $method->setDocBlock($docblock); - - $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; - $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; - $methodBody .= "}" . PHP_EOL; - $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; - $method->setBody($methodBody); - - $generator->addMethodFromGenerator($method); - - $docblock = new DocBlockGenerator('Gets a string value'); - $docblock->setTag(new ReturnTag("string")); - $method = new MethodGenerator("__toString"); - $method->setDocBlock($docblock); - $method->setBody("return strval(\$this->" . $prop->getName() . ");"); - $generator->addMethodFromGenerator($method); - } - - private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) - { - $methodBody = ''; - $docblock = new DocBlockGenerator(); - - $docblock->setShortDescription("Sets a new " . $prop->getName()); - - if ($prop->getDoc()) { - $docblock->setLongDescription($prop->getDoc()); - } - - $patramTag = new ParamTag($prop->getName()); - $docblock->setTag($patramTag); - - $return = new ReturnTag("self"); - $docblock->setTag($return); - - $type = $prop->getType(); - - $method = new MethodGenerator("set" . Inflector::classify($prop->getName())); - - $parameter = new ParameterGenerator($prop->getName(), "mixed"); - - if ($type && $type instanceof PHPClassOf) { - $patramTag->setTypes($this->getPhpType($type->getArg() - ->getType()) . "[]"); - $parameter->setType("array"); - - if ($p = $this->isOneType($type->getArg() - ->getType())) { - if (($t = $p->getType())) { - $patramTag->setTypes($this->getPhpType($t)); - } - } - } elseif ($type) { - if ($this->isNativeType($type)) { - $patramTag->setTypes($this->getPhpType($type)); - } elseif ($p = $this->isOneType($type)) { - if (($t = $p->getType()) && ! $this->isNativeType($t)) { - $patramTag->setTypes($this->getPhpType($t)); - $parameter->setType($this->getPhpType($t)); - } elseif ($t && ! $this->isNativeType($t)) { - $patramTag->setTypes($this->getPhpType($t)); - $parameter->setType($this->getPhpType($t)); - } elseif ($t) { - $patramTag->setTypes($this->getPhpType($t)); - } - } else { - $patramTag->setTypes($this->getPhpType($type)); - $parameter->setType($this->getPhpType($type)); - } - } - - $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; - $methodBody .= "return \$this;"; - $method->setBody($methodBody); - $method->setDocBlock($docblock); - $method->setParameter($parameter); - - $generator->addMethodFromGenerator($method); - } - - private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) - { - - if ($prop->getType() instanceof PHPClassOf){ - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription("isset " . $prop->getName()); - if ($prop->getDoc()) { - $docblock->setLongDescription($prop->getDoc()); - } - - $patramTag = new ParamTag("index", "scalar"); - $docblock->setTag($patramTag); - - $docblock->setTag(new ReturnTag("boolean")); - - $paramIndex = new ParameterGenerator("index", "mixed"); - - $method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]); - $method->setDocBlock($docblock); - $method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);"); - $generator->addMethodFromGenerator($method); - - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription("unset " . $prop->getName()); - if ($prop->getDoc()) { - $docblock->setLongDescription($prop->getDoc()); - } - - $patramTag = new ParamTag("index", "scalar"); - $docblock->setTag($patramTag); - $paramIndex = new ParameterGenerator("index", "mixed"); - - $docblock->setTag(new ReturnTag("void")); - - - - $method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]); - $method->setDocBlock($docblock); - $method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);"); - $generator->addMethodFromGenerator($method); - } - // //// - - $docblock = new DocBlockGenerator(); - - $docblock->setShortDescription("Gets as " . $prop->getName()); - - if ($prop->getDoc()) { - $docblock->setLongDescription($prop->getDoc()); - } - - $tag = new ReturnTag("mixed"); - $type = $prop->getType(); - if ($type && $type instanceof PHPClassOf) { - $tt = $type->getArg()->getType(); - $tag->setTypes($this->getPhpType($tt) . "[]"); - if ($p = $this->isOneType($tt)) { - if (($t = $p->getType())) { - $tag->setTypes($this->getPhpType($t) . "[]"); - } - } - } elseif ($type) { - - if ($p = $this->isOneType($type)) { - if ($t = $p->getType()) { - $tag->setTypes($this->getPhpType($t)); - } - } else { - $tag->setTypes($this->getPhpType($type)); - } - } - - $docblock->setTag($tag); - - $method = new MethodGenerator("get" . Inflector::classify($prop->getName())); - $method->setDocBlock($docblock); - $method->setBody("return \$this->" . $prop->getName() . ";"); - - $generator->addMethodFromGenerator($method); - } - - private function isOneType(PHPClass $type, $onlyParent = false) - { - if ($onlyParent) { - $e = $type->getExtends(); - if ($e) { - if ($e->hasProperty('__value')) { - return $e->getProperty('__value'); - } - } - } else { - if ($type->hasPropertyInHierarchy('__value') && count($type->getPropertiesInHierarchy()) === 1) { - return $type->getPropertyInHierarchy("__value"); - } - } - } - - private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) - { - $type = $prop->getType(); - $propName = $type->getArg()->getName(); - - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription("Adds as $propName"); - - if ($prop->getDoc()) { - $docblock->setLongDescription($prop->getDoc()); - } - - $return = new ReturnTag(); - $return->setTypes("self"); - $docblock->setTag($return); - - $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg() - ->getType())); - $docblock->setTag($patramTag); - - $method = new MethodGenerator("addTo".Inflector::classify($prop->getName())); - - $parameter = new ParameterGenerator($propName); - $tt = $type->getArg()->getType(); - - if (! $this->isNativeType($tt)) { - - if ($p = $this->isOneType($tt)) { - if (($t = $p->getType())) { - $patramTag->setTypes($this->getPhpType($t)); - - if (! $this->isNativeType($t)) { - $parameter->setType($this->getPhpType($t)); - } - } - } elseif (! $this->isNativeType($tt)) { - $parameter->setType($this->getPhpType($tt)); - } - } - - $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; - $methodBody .= "return \$this;"; - $method->setBody($methodBody); - $method->setDocBlock($docblock); - $method->setParameter($parameter); - - $generator->addMethodFromGenerator($method); - } - - private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) - { - if ($prop->getType() instanceof PHPClassOf) { - $this->handleAdder($generator, $prop, $class); - } - - $this->handleGetter($generator, $prop, $class); - $this->handleSetter($generator, $prop, $class); - } - - private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop) - { - $generatedProp = new PropertyGenerator($prop->getName()); - $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE); - - $class->addPropertyFromGenerator($generatedProp); - - if ($prop->getType() && (! $prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) { - // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); - } - - $docBlock = new DocBlockGenerator(); - $generatedProp->setDocBlock($docBlock); - - if ($prop->getDoc()) { - $docBlock->setLongDescription($prop->getDoc()); - } - $tag = new PropertyTag($prop->getName(), 'mixed'); - - $type = $prop->getType(); - - if ($type && $type instanceof PHPClassOf) { - $tt = $type->getArg()->getType(); - $tag->setTypes($this->getPhpType($tt) . "[]"); - if ($p = $this->isOneType($tt)) { - if (($t = $p->getType())) { - $tag->setTypes($this->getPhpType($t) . "[]"); - } - } - } elseif ($type) { - - if ($this->isNativeType($type)) { - $tag->setTypes($this->getPhpType($type)); - } elseif (($p = $this->isOneType($type)) && ($t = $p->getType())) { - $tag->setTypes($this->getPhpType($t)); - } else { - $tag->setTypes($this->getPhpType($prop->getType())); - } - } - $docBlock->setTag($tag); - } - - public function generate(Generator\ClassGenerator $class, PHPClass $type) - { - $docblock = new DocBlockGenerator("Class representing " . $type->getName()); - if ($type->getDoc()) { - $docblock->setLongDescription($type->getDoc()); - } - $class->setNamespaceName($type->getNamespace()); - $class->setName($type->getName()); - $class->setDocblock($docblock); - - if ($extends = $type->getExtends()) { - - if ($p = $this->isOneType($extends)) { - $this->handleProperty($class, $p); - $this->handleValueMethod($class, $p, $extends); - } else { - - $class->setExtendedClass($extends->getName()); - - if ($extends->getNamespace() != $type->getNamespace()) { - if ($extends->getName() == $type->getName()) { - $class->addUse($type->getExtends() - ->getFullName(), $extends->getName() . "Base"); - $class->setExtendedClass($extends->getName() . "Base"); - } else { - $class->addUse($extends->getFullName()); - } - } - } - } - - if ($this->handleBody($class, $type)) { - return true; - } - } } diff --git a/lib/Php/PhpConverter.php b/lib/Php/PhpConverter.php index 1b13ec4..4ff5b91 100644 --- a/lib/Php/PhpConverter.php +++ b/lib/Php/PhpConverter.php @@ -1,434 +1,562 @@ addAliasMap("http://www.w3.org/2001/XMLSchema", "dateTime", function (Type $type) - { - return "DateTime"; - }); - $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "time", function (Type $type) - { - return "DateTime"; - }); - $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "date", function (Type $type) - { - return "DateTime"; - }); - $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "anySimpleType", function (Type $type) - { - return "mixed"; - }); - $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "anyType", function (Type $type) - { - return "mixed"; - }); - } - - private $classes = []; - - public function convert(array $schemas) - { - $visited = array(); - $this->classes = array(); - foreach ($schemas as $schema) { - $this->navigate($schema, $visited); - } - return $this->getTypes(); - } - - /** - * - * @return PHPClass[] - */ - private function getTypes() - { - uasort($this->classes, function ($a, $b) - { - return strcmp($a["class"]->getFullName(), $b["class"]->getFullName()); - }); - $ret = array(); - foreach ($this->classes as $classData) { - if (! isset($classData["skip"]) || ! $classData["skip"]) { - $ret[$classData["class"]->getFullName()] = $classData["class"]; - } - } - - return $ret; - } - - private function navigate(Schema $schema, array &$visited) - { - if (isset($visited[spl_object_hash($schema)])) { - return; - } - $visited[spl_object_hash($schema)] = true; - - foreach ($schema->getTypes() as $type) { - $this->visitType($type); - } - foreach ($schema->getElements() as $element) { - $this->visitElementDef($element); - } - - foreach ($schema->getSchemas() as $schildSchema) { - if (! in_array($schildSchema->getTargetNamespace(), $this->baseSchemas, true)) { - $this->navigate($schildSchema, $visited); - } - } - } - - private function visitTypeBase(PHPClass $class, Type $type) - { - $class->setAbstract($type->isAbstract()); - - if ($type instanceof SimpleType) { - $this->visitSimpleType($class, $type); - } - if ($type instanceof BaseComplexType) { - $this->visitBaseComplexType($class, $type); - } - if ($type instanceof ComplexType) { - $this->visitComplexType($class, $type); - } - } - - private function visitGroup(PHPClass $class, Schema $schema, Group $group) - { - foreach ($group->getElements() as $childGroup) { - if ($childGroup instanceof Group) { - $this->visitGroup($class, $schema, $childGroup); - } else { - $property = $this->visitElement($class, $schema, $childGroup); - $class->addProperty($property); - } - } - } - - private function visitAttributeGroup(PHPClass $class, Schema $schema, AttributeGroup $att) - { - foreach ($att->getAttributes() as $childAttr) { - if ($childAttr instanceof AttributeGroup) { - $this->visitAttributeGroup($class, $schema, $childAttr); - } else { - $property = $this->visitAttribute($class, $schema, $childAttr); - $class->addProperty($property); - } - } - } - - private function visitElementDef(ElementDef $element) - { - if (! isset($this->classes[spl_object_hash($element)])) { - $schema = $element->getSchema(); - - $class = new PHPClass(); - $class->setDoc($element->getDoc()); - $class->setName($this->getNamingStrategy()->getItemName($element)); - $class->setDoc($element->getDoc()); - - if (! isset($this->namespaces[$schema->getTargetNamespace()])) { - throw new Exception(sprintf("Can't find a PHP namespace to '%s' namespace", $schema->getTargetNamespace())); - } - $class->setNamespace($this->namespaces[$schema->getTargetNamespace()]); - - $this->classes[spl_object_hash($element)]["class"] = $class; - - if (! $element->getType()->getName()) { - $this->visitTypeBase($class, $element->getType()); - } else { - $this->handleClassExtension($class, $element->getType()); - } - } - return $this->classes[spl_object_hash($element)]["class"]; - } - - private function findPHPName(Type $type) - { - $schema = $type->getSchema(); - - if ($className = $this->getTypeAlias($type)) { - - if (($pos = strrpos($className, '\\')) !== false) { - return [ - substr($className, $pos + 1), - substr($className, 0, $pos) - ]; - } else { - return [ - $className, - null - ]; - } - } - - $name = $this->getNamingStrategy()->getTypeName($type); - - if (! isset($this->namespaces[$schema->getTargetNamespace()])) { - throw new Exception(sprintf("Can't find a PHP namespace to '%s' namespace", $schema->getTargetNamespace())); - } - $ns = $this->namespaces[$schema->getTargetNamespace()]; - return [ - $name, - $ns - ]; - } - - /** - * - * @param Type $type - * @param boolean $force - * @return \Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass - */ - private function visitType(Type $type, $force = false) - { - if (! isset($this->classes[spl_object_hash($type)])) { - - $this->classes[spl_object_hash($type)]["class"] = $class = new PHPClass(); - - if ($alias = $this->getTypeAlias($type)) { - $class->setName($alias); - $this->classes[spl_object_hash($type)]["skip"] = true; - return $class; - } - - list ($name, $ns) = $this->findPHPName($type); - $class->setName($name); - $class->setNamespace($ns); - - $class->setDoc($type->getDoc() . PHP_EOL . "XSD Type: " . ($type->getName() ? : 'anonymous')); - - $this->visitTypeBase($class, $type); - - if ($type instanceof SimpleType){ - $this->classes[spl_object_hash($type)]["skip"] = true; - return $class; - } - if (($this->isArrayType($type) || $this->isArrayNestedElement($type)) && !$force) { - $this->classes[spl_object_hash($type)]["skip"] = true; - return $class; - } - - $this->classes[spl_object_hash($type)]["skip"] = !!$this->getTypeAlias($type); - }elseif ($force) { - if (!($type instanceof SimpleType) && !$this->getTypeAlias($type)){ - $this->classes[spl_object_hash($type)]["skip"] = false; - } - } - return $this->classes[spl_object_hash($type)]["class"]; - } - - /** - * @param Type $type - * @param string $name - * @param PHPClass $parentClass - * @return \Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass - */ - private function visitTypeAnonymous(Type $type, $name, PHPClass $parentClass) - { - if (! isset($this->classes[spl_object_hash($type)])) { - $this->classes[spl_object_hash($type)]["class"] = $class = new PHPClass(); - $class->setName($this->getNamingStrategy()->getAnonymousTypeName($type, $name)); - - $class->setNamespace($parentClass->getNamespace() . "\\" . $parentClass->getName()); - $class->setDoc($type->getDoc()); - - $this->visitTypeBase($class, $type); - - if ($type instanceof SimpleType){ - $this->classes[spl_object_hash($type)]["skip"] = true; - } - } - return $this->classes[spl_object_hash($type)]["class"]; - } - - private function visitComplexType(PHPClass $class, ComplexType $type) - { - $schema = $type->getSchema(); - foreach ($type->getElements() as $element) { - if ($element instanceof Group) { - $this->visitGroup($class, $schema, $element); - } else { - $property = $this->visitElement($class, $schema, $element); - $class->addProperty($property); - } - } - } - - private function visitSimpleType(PHPClass $class, SimpleType $type) - { - if ($restriction = $type->getRestriction()) { - $parent = $restriction->getBase(); - - if ($parent instanceof Type) { - $this->handleClassExtension($class, $parent); - } - - foreach ($restriction->getChecks() as $typeCheck => $checks) { - foreach ($checks as $check) { - $class->addCheck('__value', $typeCheck, $check); - } - } - } elseif ($unions = $type->getUnions()) { - $types = array(); - foreach ($unions as $i => $unon) { - if (! $unon->getName()) { - $types[] = $this->visitTypeAnonymous($unon, $type->getName() . $i, $class); - } else { - $types[] = $this->visitType($unon); - } - } - - if ($candidato = reset($types)) { - $class->setExtends($candidato); - } - } - } - - private function handleClassExtension(PHPClass $class, Type $type) - { - - if ($alias = $this->getTypeAlias($type)) { - $c = PHPClass::createFromFQCN($alias); - $val = new PHPProperty('__value'); - $val->setType($c); - $c->addProperty($val); - $class->setExtends($c); - } else { - $extension = $this->visitType($type, true); - $class->setExtends($extension); - } - } - - private function visitBaseComplexType(PHPClass $class, BaseComplexType $type) - { - $parent = $type->getParent(); - if ($parent) { - $parentType = $parent->getBase(); - if ($parentType instanceof Type) { - $this->handleClassExtension($class, $parentType); - } - } - $schema = $type->getSchema(); - - foreach ($type->getAttributes() as $attr) { - if ($attr instanceof AttributeGroup) { - $this->visitAttributeGroup($class, $schema, $attr); - } else { - $property = $this->visitAttribute($class, $schema, $attr); - $class->addProperty($property); - } - } - } - - private function visitAttribute(PHPClass $class, Schema $schema, AttributeItem $attribute, $arrayize = true) - { - $property = new PHPProperty(); - $property->setName(Inflector::camelize($attribute->getName())); - - if ($arrayize && $itemOfArray = $this->isArrayType($attribute->getType())) { - if ($attribute->getType()->getName()) { - $arg = new PHPArg(Inflector::camelize($attribute->getName())); - $arg->setType($this->visitType($itemOfArray)); - $property->setType(new PHPClassOf($arg)); - } else { - $property->setType($this->visitTypeAnonymous($attribute->getType(), $attribute->getName(), $class)); - } - } else { - $property->setType($this->findPHPClass($class, $attribute, true)); - } - - $property->setDoc($attribute->getDoc()); - return $property; - } - - /** - * - * @param PHPClass $class - * @param Schema $schema - * @param Element $element - * @param boolean $arrayize - * @return \Goetas\Xsd\XsdToPhp\Structure\PHPProperty - */ - private function visitElement(PHPClass $class, Schema $schema, ElementSingle $element, $arrayize = true) - { - $property = new PHPProperty(); - $property->setName(Inflector::camelize($element->getName())); - $property->setDoc($element->getDoc()); - - $t = $element->getType(); - - if ($arrayize) { - if ($itemOfArray = $this->isArrayType($t)) { - if(!$itemOfArray->getName()){ - $classType = $this->visitTypeAnonymous($itemOfArray, $element->getName(), $class); - }else{ - $classType = $this->visitType($itemOfArray); - } - - $arg = new PHPArg(Inflector::camelize($element->getName())); - $arg->setType($classType); - $property->setType(new PHPClassOf($arg)); - return $property; - }elseif ($itemOfArray = $this->isArrayNestedElement($t)) { - if(!$t->getName()){ - $classType = $this->visitTypeAnonymous($t, $element->getName(), $class); - }else{ - $classType = $this->visitType($t); - } - $elementProp = $this->visitElement($classType, $schema, $itemOfArray, false); - $property->setType(new PHPClassOf($elementProp)); - return $property; - } elseif ($this->isArrayElement($element)) { - $arg = new PHPArg(Inflector::camelize($element->getName())); - $arg->setType($this->findPHPClass($class, $element)); - $arg->setDefault('array()'); - $property->setType(new PHPClassOf($arg)); - return $property; - } - } - - $property->setType($this->findPHPClass($class, $element, true)); - return $property; - } - - private function findPHPClass(PHPClass $class, Item $node, $force = false) - { - - if ($node instanceof ElementRef) { - return $this->visitElementDef($node->getReferencedElement()); - } - - if (! $node->getType()->getName()) { - return $this->visitTypeAnonymous($node->getType(), $node->getName(), $class); - } else { - return $this->visitType($node->getType(), $force); - } - } +class PhpConverter extends AbstractConverter { + + public function __construct( NamingStrategy $namingStrategy ) { + parent::__construct( $namingStrategy ); + + $this->addAliasMap( + "http://www.w3.org/2001/XMLSchema", + "dateTime", + function ( Type $type ) { + return "DateTime"; + } + ); + $this->addAliasMap( + "http://www.w3.org/2001/XMLSchema", + "time", + function ( Type $type ) { + return "DateTime"; + } + ); + $this->addAliasMap( + "http://www.w3.org/2001/XMLSchema", + "date", + function ( Type $type ) { + return "DateTime"; + } + ); + $this->addAliasMap( + "http://www.w3.org/2001/XMLSchema", + "anySimpleType", + function ( Type $type ) { + return "mixed"; + } + ); + $this->addAliasMap( + "http://www.w3.org/2001/XMLSchema", + "anyType", + function ( Type $type ) { + return "mixed"; + } + ); + } + + private $classes = [ ]; + + public function convert( array $schemas ) { + $visited = [ ]; + $this->classes = [ ]; + foreach ( $schemas as $schema ) { + $this->navigate( $schema, $visited ); + } + + return $this->getTypes(); + } + + /** + * + * @return PHPClass[] + */ + private function getTypes() { + uasort( + $this->classes, + function ( $a, $b ) { + return strcmp( $a[ "class" ]->getFullName(), $b[ "class" ]->getFullName() ); + } + ); + $ret = [ ]; + foreach ( $this->classes as $classData ) { + if ( ! isset( $classData[ "skip" ] ) || ! $classData[ "skip" ] ) { + $ret[ $classData[ "class" ]->getFullName() ] = $classData[ "class" ]; + } + } + + return $ret; + } + + private function navigate( Schema $schema, array &$visited ) { + if ( isset( $visited[ spl_object_hash( $schema ) ] ) ) { + return; + } + $visited[ spl_object_hash( $schema ) ] = true; + + foreach ( $schema->getTypes() as $type ) { + $this->visitType( $type ); + } + foreach ( $schema->getElements() as $element ) { + $this->visitElementDef( $element ); + } + + foreach ( $schema->getSchemas() as $schildSchema ) { + if ( ! in_array( $schildSchema->getTargetNamespace(), $this->baseSchemas, true ) ) { + $this->navigate( $schildSchema, $visited ); + } + } + } + + private function visitTypeBase( PHPClass $class, Type $type ) { + $class->setAbstract( $type->isAbstract() ); + + if ( $type instanceof SimpleType ) { + $this->visitSimpleType( $class, $type ); + } + if ( $type instanceof BaseComplexType ) { + $this->visitBaseComplexType( $class, $type ); + } + if ( $type instanceof ComplexType ) { + $this->visitComplexType( $class, $type ); + } + } + + private function visitGroup( PHPClass $class, Schema $schema, Group $group ) { + foreach ( $group->getElements() as $childGroup ) { + if ( $childGroup instanceof Group ) { + $this->visitGroup( $class, $schema, $childGroup ); + } else { + $property = $this->visitElement( $class, $schema, $childGroup ); + $class->addProperty( $property ); + } + } + } + + private function visitAttributeGroup( PHPClass $class, Schema $schema, AttributeGroup $att ) { + foreach ( $att->getAttributes() as $childAttr ) { + if ( $childAttr instanceof AttributeGroup ) { + $this->visitAttributeGroup( $class, $schema, $childAttr ); + } else { + $property = $this->visitAttribute( $class, $schema, $childAttr ); + $class->addProperty( $property ); + } + } + } + + private function visitElementDef( ElementDef $element ) { + if ( ! isset( $this->classes[ spl_object_hash( $element ) ] ) ) { + $schema = $element->getSchema(); + + $class = new PHPClass(); + $class->setDoc( $element->getDoc() ); + $class->setName( + $this->getNamingStrategy() + ->getItemName( $element ) + ); + $class->setDoc( $element->getDoc() ); + + if ( ! isset( $this->namespaces[ $schema->getTargetNamespace() ] ) ) { + throw new Exception( + sprintf( "Can't find a PHP namespace to '%s' namespace", $schema->getTargetNamespace() ) + ); + } + $class->setNamespace( $this->namespaces[ $schema->getTargetNamespace() ] ); + + $this->classes[ spl_object_hash( $element ) ][ "class" ] = $class; + + if ( ! $element->getType() + ->getName() + ) { + $this->visitTypeBase( $class, $element->getType() ); + } else { + $this->handleClassExtension( $class, $element->getType() ); + } + } + + return $this->classes[ spl_object_hash( $element ) ][ "class" ]; + } + + private function findPHPName( Type $type ) { + $schema = $type->getSchema(); + + if ( $className = $this->getTypeAlias( $type ) ) { + + if ( ( $pos = strrpos( $className, '\\' ) ) !== false ) { + return [ + substr( $className, $pos + 1 ), + substr( $className, 0, $pos ), + ]; + } else { + return [ + $className, + null, + ]; + } + } + + $name = $this->getNamingStrategy() + ->getTypeName( $type ); + + if ( ! isset( $this->namespaces[ $schema->getTargetNamespace() ] ) ) { + throw new Exception( + sprintf( "Can't find a PHP namespace to '%s' namespace", $schema->getTargetNamespace() ) + ); + } + $ns = $this->namespaces[ $schema->getTargetNamespace() ]; + + return [ + $name, + $ns, + ]; + } + + /** + * + * @param Type $type + * @param boolean $force + * + * @return \Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass + */ + private function visitType( Type $type, $force = false ) { + if ( ! isset( $this->classes[ spl_object_hash( $type ) ] ) ) { + + $this->classes[ spl_object_hash( $type ) ][ "class" ] = $class = new PHPClass(); + + if ( $alias = $this->getTypeAlias( $type ) ) { + $class->setName( $alias ); + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = true; + + return $class; + } + + list ( $name, $ns ) = $this->findPHPName( $type ); + $class->setName( $name ); + $class->setNamespace( $ns ); + + $class->setDoc( $type->getDoc() . PHP_EOL . "XSD Type: " . ( $type->getName() ?: 'anonymous' ) ); + + $this->visitTypeBase( $class, $type ); + + if ( ( $restriction = $type->getRestriction() ) && $restriction->getChecksByType( 'enumeration' ) ) { + return $class; + } + + if ( $type instanceof SimpleType ) { + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = true; + + return $class; + } + if ( ( $this->isArrayType( $type ) || $this->isArrayNestedElement( $type ) ) && ! $force ) { + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = true; + + return $class; + } + + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = ! ! $this->getTypeAlias( $type ); + } elseif ( $force ) { + if ( ! ( $type instanceof SimpleType ) && ! $this->getTypeAlias( $type ) ) { + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = false; + } + } + + return $this->classes[ spl_object_hash( $type ) ][ "class" ]; + } + + /** + * @param Type $type + * @param string $name + * @param PHPClass $parentClass + * + * @return \Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass + */ + private function visitTypeAnonymous( Type $type, $name, PHPClass $parentClass ) { + if ( ! isset( $this->classes[ spl_object_hash( $type ) ] ) ) { + $this->classes[ spl_object_hash( $type ) ][ "class" ] = $class = new PHPClass(); + $class->setName( + $this->getNamingStrategy() + ->getAnonymousTypeName( $type, $name ) + ); + + $class->setNamespace( $parentClass->getNamespace() . "\\" . $parentClass->getName() ); + $class->setDoc( $type->getDoc() ); + + $this->visitTypeBase( $class, $type ); + + if ( $type instanceof SimpleType ) { + $this->classes[ spl_object_hash( $type ) ][ "skip" ] = true; + } + } + + return $this->classes[ spl_object_hash( $type ) ][ "class" ]; + } + + private function visitComplexType( PHPClass $class, ComplexType $type ) { + $schema = $type->getSchema(); + foreach ( $type->getElements() as $element ) { + if ( $element instanceof Group ) { + $this->visitGroup( $class, $schema, $element ); + } else { + $property = $this->visitElement( $class, $schema, $element ); + $class->addProperty( $property ); + } + } + } + + private function visitSimpleType( PHPClass $class, SimpleType $type ) { + if ( $restriction = $type->getRestriction() ) { + $parent = $restriction->getBase(); + + + if ( $parent instanceof Type ) { + $this->handleClassExtension( $class, $parent ); + } + + foreach ( $restriction->getChecks() as $typeCheck => $checks ) { + foreach ( $checks as $check ) { + if ( $typeCheck === 'enumeration' ) { + $class->addConstant( + $this->createConstantNameFromValue( $check[ 'value' ] ), + $check[ 'value' ] + ); + } else { + $class->addCheck( '__value', $typeCheck, $check ); + } + } + } + } elseif ( $unions = $type->getUnions() ) { + $types = [ ]; + foreach ( $unions as $i => $unon ) { + if ( ! $unon->getName() ) { + $types[] = $this->visitTypeAnonymous( $unon, $type->getName() . $i, $class ); + } else { + $types[] = $this->visitType( $unon ); + } + } + + if ( $candidato = reset( $types ) ) { + $class->setExtends( $candidato ); + } + } + } + + private function handleClassExtension( PHPClass $class, Type $type ) { + + if ( $alias = $this->getTypeAlias( $type ) ) { + $c = PHPClass::createFromFQCN( $alias ); + $val = new PHPProperty( '__value' ); + $val->setType( $c ); + $c->addProperty( $val ); + $class->setExtends( $c ); + } else { + $extension = $this->visitType( $type, true ); + $class->setExtends( $extension ); + } + } + + private function visitBaseComplexType( PHPClass $class, BaseComplexType $type ) { + $parent = $type->getParent(); + if ( $parent ) { + $parentType = $parent->getBase(); + if ( $parentType instanceof Type ) { + $this->handleClassExtension( $class, $parentType ); + } + } + $schema = $type->getSchema(); + + foreach ( $type->getAttributes() as $attr ) { + if ( $attr instanceof AttributeGroup ) { + $this->visitAttributeGroup( $class, $schema, $attr ); + } else { + $property = $this->visitAttribute( $class, $schema, $attr ); + $class->addProperty( $property ); + } + } + } + + private function visitAttribute( PHPClass $class, Schema $schema, AttributeItem $attribute, $arrayize = true ) { + $property = new PHPProperty(); + $property->setName( Inflector::camelize( $attribute->getName() ) ); + + if ( $arrayize && $itemOfArray = $this->isArrayType( $attribute->getType() ) ) { + if ( $attribute->getType() + ->getName() + ) { + $arg = new PHPArg( Inflector::camelize( $attribute->getName() ) ); + $arg->setType( $this->visitType( $itemOfArray ) ); + $property->setType( new PHPClassOf( $arg ) ); + } else { + $property->setType( $this->visitTypeAnonymous( $attribute->getType(), $attribute->getName(), $class ) ); + } + } else { + $property->setType( $this->findPHPClass( $class, $attribute, true ) ); + } + + $property->setDoc( $attribute->getDoc() ); + + return $property; + } + + /** + * + * @param PHPClass $class + * @param Schema $schema + * @param Element $element + * @param boolean $arrayize + * + * @return \Goetas\Xsd\XsdToPhp\Structure\PHPProperty + */ + private function visitElement( PHPClass $class, Schema $schema, ElementSingle $element, $arrayize = true ) { + $property = new PHPProperty(); + $property->setName( Inflector::camelize( $element->getName() ) ); + $property->setDoc( $element->getDoc() ); + + $t = $element->getType(); + + if ( $arrayize ) { + if ( $itemOfArray = $this->isArrayType( $t ) ) { + if ( ! $itemOfArray->getName() ) { + $classType = $this->visitTypeAnonymous( $itemOfArray, $element->getName(), $class ); + } else { + $classType = $this->visitType( $itemOfArray ); + } + + $arg = new PHPArg( Inflector::camelize( $element->getName() ) ); + $arg->setType( $classType ); + $property->setType( new PHPClassOf( $arg ) ); + + return $property; + } elseif ( $itemOfArray = $this->isArrayNestedElement( $t ) ) { + if ( ! $t->getName() ) { + $classType = $this->visitTypeAnonymous( $t, $element->getName(), $class ); + } else { + $classType = $this->visitType( $t ); + } + $elementProp = $this->visitElement( $classType, $schema, $itemOfArray, false ); + $property->setType( new PHPClassOf( $elementProp ) ); + + return $property; + } elseif ( $this->isArrayElement( $element ) ) { + $arg = new PHPArg( Inflector::camelize( $element->getName() ) ); + $arg->setType( $this->findPHPClass( $class, $element ) ); + $arg->setDefault( 'array()' ); + $property->setType( new PHPClassOf( $arg ) ); + + return $property; + } + } + + $property->setType( $this->findPHPClass( $class, $element, true ) ); + + return $property; + } + + private function findPHPClass( PHPClass $class, Item $node, $force = false ) { + + if ( $node instanceof ElementRef ) { + return $this->visitElementDef( $node->getReferencedElement() ); + } + + if ( ! $node->getType() + ->getName() + ) { + return $this->visitTypeAnonymous( $node->getType(), $node->getName(), $class ); + } else { + return $this->visitType( $node->getType(), $force ); + } + } + + /** + * @param $constantValue + * + * @return string + */ + public function createConstantNameFromValue( $constantValue ) { + static $keywords = [ + 'abstract', + 'and', + 'array', + 'as', + 'break', + 'callable', + 'case', + 'catch', + 'class', + 'clone', + 'const', + 'continue', + 'declare', + 'default', + 'die', + 'do', + 'echo', + 'else', + 'elseif', + 'empty', + 'enddeclare', + 'endfor', + 'endforeach', + 'endif', + 'endswitch', + 'endwhile', + 'eval', + 'exit', + 'extends', + 'final', + 'for', + 'foreach', + 'function', + 'global', + 'goto', + 'if', + 'implements', + 'include', + 'include_once', + 'instanceof', + 'insteadof', + 'interface', + 'isset', + 'list', + 'namespace', + 'new', + 'or', + 'print', + 'private', + 'protected', + 'public', + 'require', + 'require_once', + 'return', + 'static', + 'switch', + 'throw', + 'trait', + 'try', + 'unset', + 'use', + 'var', + 'while', + 'xor', + ]; + + $slugify = new Slugify(); + $constantName = $slugify->slugify( $constantValue, '_' ); + if ( in_array( $constantName, $keywords ) ) { + $constantName = $constantName . '_'; + } + $constantName = strtoupper( $constantName ); + + return $constantName; + } } diff --git a/lib/Php/Structure/PHPClass.php b/lib/Php/Structure/PHPClass.php index 7511f8a..2df4d3b 100644 --- a/lib/Php/Structure/PHPClass.php +++ b/lib/Php/Structure/PHPClass.php @@ -1,240 +1,256 @@ name = $name; - $this->namespace = $namespace; - } - - public function getName() - { - return $this->name; - } - - public function setName($name) - { - $this->name = $name; - return $this; - } - - public function getNamespace() - { - return $this->namespace; - } - - public function setNamespace($namespace) - { - $this->namespace = $namespace; - return $this; - } - - public function getDoc() - { - return $this->doc; - } - - public function setDoc($doc) - { - $this->doc = $doc; - return $this; - } - - public function __toString() - { - return $this->getFullName(); - } - - public function getFullName() - { - return "{$this->namespace}\\{$this->name}"; - } - - protected $checks = array(); - - /** - * - * @var PHPConstant[] - */ - protected $constants = array(); - - /** - * - * @var PHPProperty[] - */ - protected $properties = array(); - - /** - * - * @param - * $property - * @return array - */ - public function getChecks($property) - { - return isset($this->checks[$property]) ? $this->checks[$property] : array(); - } - - /** - * - * @param - * $property - * @param - * $check - * @param - * $value - * @return $this - */ - public function addCheck($property, $check, $value) - { - $this->checks[$property][$check][] = $value; - return $this; - } - - /** - * - * @return PHPProperty[] - */ - public function getProperties() - { - return $this->properties; - } - - /** - * - * @param string $name - * @return boolean - */ - public function hasProperty($name) - { - return isset($this->properties[$name]); - } - - /** - * - * @param string $name - * @return bool - */ - public function hasPropertyInHierarchy($name) - { - if ($this->hasProperty($name)) { - return true; - } - if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) { - return true; - } - return false; - } - - /** - * - * @param string $name - * @return PHPProperty - */ - public function getPropertyInHierarchy($name) - { - if ($this->hasProperty($name)) { - return $this->getProperty($name); - } - if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) { - return $this->getExtends()->getPropertyInHierarchy($name); - } - return null; - } - - /** - * - * @param string $name - * @return PHPProperty - */ - public function getPropertiesInHierarchy() - { - $ps = $this->getProperties(); - - if (($this instanceof PHPClass) && $this->getExtends()) { - $ps = array_merge($ps, $this->getExtends()->getPropertiesInHierarchy()); - } - - return $ps; - } - - /** - * - * @param string $name - * @return PHPProperty - */ - public function getProperty($name) - { - return $this->properties[$name]; - } - - /** - * - * @param PHPProperty $property - * @return $this - */ - public function addProperty(PHPProperty $property) - { - $this->properties[$property->getName()] = $property; - return $this; - } - - /** - * - * @var boolean - */ - protected $abstract; - - /** - * - * @var PHPClass - */ - protected $extends; - - /** - * - * @return PHPClass - */ - public function getExtends() - { - return $this->extends; - } - - /** - * - * @param PHPClass $extends - * @return PHPClass - */ - public function setExtends(PHPClass $extends) - { - $this->extends = $extends; - return $this; - } - - public function getAbstract() - { - return $this->abstract; - } - - public function setAbstract($abstract) - { - $this->abstract = (boolean) $abstract; - return $this; - } +class PHPClass { + + protected $name; + + protected $namespace; + + protected $doc; + + public static function createFromFQCN( $className ) { + if ( ( $pos = strrpos( $className, '\\' ) ) !== false ) { + return new self( substr( $className, $pos + 1 ), substr( $className, 0, $pos ) ); + } else { + return new self( $className ); + } + } + + public function __construct( $name = null, $namespace = null ) { + $this->name = $name; + $this->namespace = $namespace; + } + + public function getName() { + return $this->name; + } + + public function setName( $name ) { + $this->name = $name; + + return $this; + } + + public function getNamespace() { + return $this->namespace; + } + + public function setNamespace( $namespace ) { + $this->namespace = $namespace; + + return $this; + } + + public function getDoc() { + return $this->doc; + } + + public function setDoc( $doc ) { + $this->doc = $doc; + + return $this; + } + + public function __toString() { + return $this->getFullName(); + } + + public function getFullName() { + return "{$this->namespace}\\{$this->name}"; + } + + protected $checks = [ ]; + + /** + * + * @var PHPConstant[] + */ + protected $constants = [ ]; + + /** + * + * @var PHPProperty[] + */ + protected $properties = [ ]; + + /** + * + * @param + * $property + * + * @return array + */ + public function getChecks( $property ) { + return isset( $this->checks[ $property ] ) ? $this->checks[ $property ] : [ ]; + } + + /** + * + * @param + * $property + * @param + * $check + * @param + * $value + * + * @return $this + */ + public function addCheck( $property, $check, $value ) { + $this->checks[ $property ][ $check ][] = $value; + + return $this; + } + + /** + * + * @return PHPProperty[] + */ + public function getProperties() { + return $this->properties; + } + + /** + * + * @param string $name + * + * @return boolean + */ + public function hasProperty( $name ) { + return isset( $this->properties[ $name ] ); + } + + /** + * + * @param string $name + * + * @return bool + */ + public function hasPropertyInHierarchy( $name ) { + if ( $this->hasProperty( $name ) ) { + return true; + } + if ( ( $this instanceof PHPClass ) && $this->getExtends() && $this->getExtends() + ->hasPropertyInHierarchy( $name ) + ) { + return true; + } + + return false; + } + + /** + * + * @param string $name + * + * @return PHPProperty + */ + public function getPropertyInHierarchy( $name ) { + if ( $this->hasProperty( $name ) ) { + return $this->getProperty( $name ); + } + if ( ( $this instanceof PHPClass ) && $this->getExtends() && $this->getExtends() + ->hasPropertyInHierarchy( $name ) + ) { + return $this->getExtends() + ->getPropertyInHierarchy( $name ); + } + + return null; + } + + /** + * + * @param string $name + * + * @return PHPProperty + */ + public function getPropertiesInHierarchy() { + $ps = $this->getProperties(); + + if ( ( $this instanceof PHPClass ) && $this->getExtends() ) { + $ps = array_merge( + $ps, + $this->getExtends() + ->getPropertiesInHierarchy() + ); + } + + return $ps; + } + + /** + * + * @param string $name + * + * @return PHPProperty + */ + public function getProperty( $name ) { + return $this->properties[ $name ]; + } + + /** + * + * @param PHPProperty $property + * + * @return $this + */ + public function addProperty( PHPProperty $property ) { + $this->properties[ $property->getName() ] = $property; + + return $this; + } + + /** + * + * @var boolean + */ + protected $abstract; + + /** + * + * @var PHPClass + */ + protected $extends; + + /** + * + * @return PHPClass + */ + public function getExtends() { + return $this->extends; + } + + /** + * + * @param PHPClass $extends + * + * @return PHPClass + */ + public function setExtends( PHPClass $extends ) { + $this->extends = $extends; + + return $this; + } + + public function getAbstract() { + return $this->abstract; + } + + public function setAbstract( $abstract ) { + $this->abstract = (boolean) $abstract; + + return $this; + } + + public function addConstant( $constantName, $constantValue ) { + $this->constants[] = new PhpConstant( $constantName, $constantValue ); + } + + /** + * @return PHPConstant[] + */ + public function getConstants() { + return $this->constants; + } } diff --git a/lib/Php/Structure/PhpConstant.php b/lib/Php/Structure/PhpConstant.php new file mode 100644 index 0000000..8c9ee43 --- /dev/null +++ b/lib/Php/Structure/PhpConstant.php @@ -0,0 +1,47 @@ +name = $name; + $this->value = $value; + } + + /** + * @return string + */ + public function getName() { + return $this->name; + } + + /** + * @param string $name + */ + public function setName( $name ) { + $this->name = $name; + } + + /** + * @return string + */ + public function getValue() { + return $this->value; + } + + /** + * @param string $value + */ + public function setValue( $value ) { + $this->value = $value; + } +} From 1f419e89e14b0db89682b1513cf0b624b22f2cb6 Mon Sep 17 00:00:00 2001 From: Tobias Hoffmann Date: Tue, 2 Jun 2015 16:34:26 +0200 Subject: [PATCH 02/15] Setter methods now accept null values. --- lib/Php/ClassGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index ac31e77..670007f 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -175,7 +175,7 @@ private function handleSetter( Generator\ClassGenerator $generator, PHPProperty $method = new MethodGenerator( "set" . Inflector::classify( $prop->getName() ) ); - $parameter = new ParameterGenerator( $prop->getName(), "mixed" ); + $parameter = new ParameterGenerator( $prop->getName(), "mixed", new Generator\ValueGenerator(null, Generator\ValueGenerator::TYPE_NULL) ); if ( $type && $type instanceof PHPClassOf ) { $patramTag->setTypes( From 15a7ea6ebfed07a3d9471562511228192efc586e Mon Sep 17 00:00:00 2001 From: Tobias Eckardt Date: Thu, 31 Mar 2016 16:50:55 +0200 Subject: [PATCH 03/15] Versions > 5.4 are possible --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 73d13bf..d01719a 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "license":"LGPL", "require":{ - "php":"~5.4", + "php":">=5.4", "symfony/console":"~2.1", "symfony/yaml":"~2.1", "goetas/xsd-reader":"~2.0@dev", From dea2f23ddd2439ac5714f3b3a0e04acaf4c44a0a Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 17:59:18 +0000 Subject: [PATCH 04/15] fixing bad merge. adding missing package --- composer.json | 3 ++- lib/Php/Structure/PHPClass.php | 3 --- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 393732c..737f1b7 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "symfony/yaml": "^2.1|^3.0", "goetas-webservices/xsd-reader": "^0.1.2", "doctrine/inflector": "^1.0", - "zendframework/zend-code": "~2.3" + "zendframework/zend-code": "~2.3", + "cocur/slugify": "^2.3" }, "require-dev": { "phpunit/phpunit": "^4.8|^5.0", diff --git a/lib/Php/Structure/PHPClass.php b/lib/Php/Structure/PHPClass.php index 65e4eb7..a78bfc8 100644 --- a/lib/Php/Structure/PHPClass.php +++ b/lib/Php/Structure/PHPClass.php @@ -229,9 +229,6 @@ public function setExtends(PHPClass $extends) return $this; } - return $ps; - } - public function setAbstract($abstract) { $this->abstract = (boolean)$abstract; From 87386835d9bcd1a0ca17546a0b8346aaa5268e6a Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:26:30 -0700 Subject: [PATCH 05/15] allowing generation of numeric constants. adding a EMPTY_STRING constant as an option as well --- lib/Php/PhpConverter.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Php/PhpConverter.php b/lib/Php/PhpConverter.php index 8f7f54d..868bc5d 100644 --- a/lib/Php/PhpConverter.php +++ b/lib/Php/PhpConverter.php @@ -516,6 +516,15 @@ public function createConstantNameFromValue( $constantValue ) { if ( in_array( $constantName, $keywords ) ) { $constantName = $constantName . '_'; } + + if (is_numeric($constantName)) { + $constantName = 'NUM_' . $constantName; + } + + if ($constantName === '') { + $constantName = 'EMPTY_STRING'; + } + $constantName = strtoupper( $constantName ); return $constantName; From 0a782868798f31778a455ad0ee95b57e12ccdd7c Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:28:47 -0700 Subject: [PATCH 06/15] reformat spacing --- lib/Php/PhpConverter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Php/PhpConverter.php b/lib/Php/PhpConverter.php index 868bc5d..a656744 100644 --- a/lib/Php/PhpConverter.php +++ b/lib/Php/PhpConverter.php @@ -517,8 +517,8 @@ public function createConstantNameFromValue( $constantValue ) { $constantName = $constantName . '_'; } - if (is_numeric($constantName)) { - $constantName = 'NUM_' . $constantName; + if (is_numeric($constantName)) { + $constantName = 'NUM_' . $constantName; } if ($constantName === '') { From 4ad0c63443e0ec935e0beb365f69cd39923c7a89 Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:30:35 -0700 Subject: [PATCH 07/15] adding missing function from bad merge --- lib/Php/Structure/PHPClass.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Php/Structure/PHPClass.php b/lib/Php/Structure/PHPClass.php index a78bfc8..676fc34 100644 --- a/lib/Php/Structure/PHPClass.php +++ b/lib/Php/Structure/PHPClass.php @@ -229,6 +229,11 @@ public function setExtends(PHPClass $extends) return $this; } + public function getAbstract() + { + return $this->abstract; + } + public function setAbstract($abstract) { $this->abstract = (boolean)$abstract; From 58ec5e2ad457b643df14aec51ca1c405eb2532eb Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:33:46 -0700 Subject: [PATCH 08/15] trying to match original package formatting a bit more --- lib/Php/ClassGenerator.php | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 670007f..15225b9 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -15,9 +15,11 @@ use Zend\Code\Generator\ParameterGenerator; use Zend\Code\Generator\PropertyGenerator; -class ClassGenerator { +class ClassGenerator +{ - private function handleBody( Generator\ClassGenerator $class, PHPClass $type ) { + private function handleBody(Generator\ClassGenerator $class, PHPClass $type ) + { foreach ( $type->getConstants() as $const ) { $this->handleConstant( $class, $const ); } @@ -46,7 +48,8 @@ private function handleBody( Generator\ClassGenerator $class, PHPClass $type ) { return true; } - private function isNativeType( PHPClass $class ) { + private function isNativeType(PHPClass $class ) + { return ! $class->getNamespace() && in_array( $class->getName(), [ @@ -62,7 +65,8 @@ private function isNativeType( PHPClass $class ) { ); } - private function getPhpType( PHPClass $class ) { + private function getPhpType(PHPClass $class ) + { if ( ! $class->getNamespace() ) { if ( $this->isNativeType( $class ) ) { return $class->getName(); @@ -155,7 +159,8 @@ private function handleValueMethod( $generator->addMethodFromGenerator( $method ); } - private function handleSetter( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + { $methodBody = ''; $docblock = new DocBlockGenerator(); @@ -223,7 +228,8 @@ private function handleSetter( Generator\ClassGenerator $generator, PHPProperty $generator->addMethodFromGenerator( $method ); } - private function handleGetter( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + { if ( $prop->getType() instanceof PHPClassOf ) { $docblock = new DocBlockGenerator(); @@ -303,7 +309,8 @@ private function handleGetter( Generator\ClassGenerator $generator, PHPProperty $generator->addMethodFromGenerator( $method ); } - private function isOneType( PHPClass $type, $onlyParent = false ) { + private function isOneType(PHPClass $type, $onlyParent = false ) + { if ( $onlyParent ) { $e = $type->getExtends(); if ( $e ) { @@ -318,7 +325,8 @@ private function isOneType( PHPClass $type, $onlyParent = false ) { } } - private function handleAdder( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + { $type = $prop->getType(); $propName = $type->getArg() ->getName(); @@ -372,7 +380,8 @@ private function handleAdder( Generator\ClassGenerator $generator, PHPProperty $ $generator->addMethodFromGenerator( $method ); } - private function handleMethod( Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) { + private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + { if ( $prop->getType() instanceof PHPClassOf ) { $this->handleAdder( $generator, $prop, $class ); } @@ -381,7 +390,8 @@ private function handleMethod( Generator\ClassGenerator $generator, PHPProperty $this->handleSetter( $generator, $prop, $class ); } - private function handleProperty( Generator\ClassGenerator $class, PHPProperty $prop ) { + private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop ) + { $generatedProp = new PropertyGenerator( $prop->getName() ); $generatedProp->setVisibility( PropertyGenerator::VISIBILITY_PRIVATE ); @@ -426,11 +436,13 @@ private function handleProperty( Generator\ClassGenerator $class, PHPProperty $p $docBlock->setTag( $tag ); } - private function handleConstant( Generator\ClassGenerator $class, PhpConstant $const ) { + private function handleConstant(Generator\ClassGenerator $class, PhpConstant $const ) + { $class->addConstant( $const->getName(), $const->getValue() ); } - public function generate( Generator\ClassGenerator $class, PHPClass $type ) { + public function generate(Generator\ClassGenerator $class, PHPClass $type ) + { $docblock = new DocBlockGenerator( "Class representing " . $type->getName() ); if ( $type->getDoc() ) { $docblock->setLongDescription( $type->getDoc() ); From d865be49dfa90b80f73c762fe0c5fce6a28f2b83 Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:35:12 -0700 Subject: [PATCH 09/15] reformatting to match --- lib/Php/ClassGenerator.php | 892 ++++++++++++++++++------------------- 1 file changed, 446 insertions(+), 446 deletions(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 15225b9..d123e3a 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -18,466 +18,466 @@ class ClassGenerator { - private function handleBody(Generator\ClassGenerator $class, PHPClass $type ) + private function handleBody(Generator\ClassGenerator $class, PHPClass $type) { - foreach ( $type->getConstants() as $const ) { - $this->handleConstant( $class, $const ); - } - - foreach ( $type->getProperties() as $prop ) { - if ( $prop->getName() !== '__value' ) { - $this->handleProperty( $class, $prop ); - } - } - foreach ( $type->getProperties() as $prop ) { - if ( $prop->getName() !== '__value' ) { - $this->handleMethod( $class, $prop, $type ); - } - } - - if ( count( $type->getProperties() ) === 1 && $type->hasProperty( '__value' ) ) { - if ( ! count( $type->getConstants() ) ) { - return false; - } - $class->removeMethod( '__construct' ) - ->removeMethod( '__toString' ) - ->removeMethod( 'value' ) - ; - } - - return true; - } - - private function isNativeType(PHPClass $class ) + foreach ($type->getConstants() as $const) { + $this->handleConstant($class, $const); + } + + foreach ($type->getProperties() as $prop) { + if ($prop->getName() !== '__value') { + $this->handleProperty($class, $prop); + } + } + foreach ($type->getProperties() as $prop) { + if ($prop->getName() !== '__value') { + $this->handleMethod($class, $prop, $type); + } + } + + if (count($type->getProperties()) === 1 && $type->hasProperty('__value')) { + if (!count($type->getConstants())) { + return false; + } + $class->removeMethod('__construct') + ->removeMethod('__toString') + ->removeMethod('value'); + } + + return true; + } + + private function isNativeType(PHPClass $class) { - return ! $class->getNamespace() && in_array( - $class->getName(), - [ - 'string', - 'int', - 'float', - 'integer', - 'boolean', - 'array', - 'mixed', - 'callable', - ] - ); - } - - private function getPhpType(PHPClass $class ) + return !$class->getNamespace() && in_array( + $class->getName(), + [ + 'string', + 'int', + 'float', + 'integer', + 'boolean', + 'array', + 'mixed', + 'callable', + ] + ); + } + + private function getPhpType(PHPClass $class) { - if ( ! $class->getNamespace() ) { - if ( $this->isNativeType( $class ) ) { - return $class->getName(); - } - - return "\\" . $class->getName(); - } - - return "\\" . $class->getFullName(); - } - - private function handleValueMethod( - Generator\ClassGenerator $generator, - PHPProperty $prop, - PHPClass $class, - $all = true - ) { - $type = $prop->getType(); - - $docblock = new DocBlockGenerator( 'Construct' ); - $paramTag = new ParamTag( "value", "mixed" ); - $paramTag->setTypes( ( $type ? $this->getPhpType( $type ) : "mixed" ) ); - - $docblock->setTag( $paramTag ); - - $param = new ParameterGenerator( "value" ); - if ( $type && ! $this->isNativeType( $type ) ) { - $param->setType( $this->getPhpType( $type ) ); - } - $method = new MethodGenerator( - "__construct", [ - $param, - ] - ); - $method->setDocBlock( $docblock ); - $method->setBody( "\$this->value(\$value);" ); - - $generator->addMethodFromGenerator( $method ); - - $docblock = new DocBlockGenerator( 'Gets or sets the inner value' ); - $paramTag = new ParamTag( "value", "mixed" ); - if ( $type && $type instanceof PHPClassOf ) { - $paramTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); - } elseif ( $type ) { - $paramTag->setTypes( $this->getPhpType( $prop->getType() ) ); - } - $docblock->setTag( $paramTag ); - - $returnTag = new ReturnTag( "mixed" ); - - if ( $type && $type instanceof PHPClassOf ) { - $returnTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); - } elseif ( $type ) { - $returnTag->setTypes( $this->getPhpType( $type ) ); - } - $docblock->setTag( $returnTag ); - - $param = new ParameterGenerator( "value" ); - $param->setDefaultValue( null ); - - if ( $type && ! $this->isNativeType( $type ) ) { - $param->setType( $this->getPhpType( $type ) ); - } - $method = new MethodGenerator( "value", [ ] ); - $method->setDocBlock( $docblock ); - - $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; - $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; - $methodBody .= "}" . PHP_EOL; - $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; - $method->setBody( $methodBody ); - - $generator->addMethodFromGenerator( $method ); - - $docblock = new DocBlockGenerator( 'Gets a string value' ); - $docblock->setTag( new ReturnTag( "string" ) ); - $method = new MethodGenerator( "__toString" ); - $method->setDocBlock( $docblock ); - $method->setBody( "return strval(\$this->" . $prop->getName() . ");" ); - $generator->addMethodFromGenerator( $method ); - } - - private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + if (!$class->getNamespace()) { + if ($this->isNativeType($class)) { + return $class->getName(); + } + + return "\\" . $class->getName(); + } + + return "\\" . $class->getFullName(); + } + + private function handleValueMethod( + Generator\ClassGenerator $generator, + PHPProperty $prop, + PHPClass $class, + $all = true + ) { - $methodBody = ''; - $docblock = new DocBlockGenerator(); - - $docblock->setShortDescription( "Sets a new " . $prop->getName() ); - - if ( $prop->getDoc() ) { - $docblock->setLongDescription( $prop->getDoc() ); - } - - $patramTag = new ParamTag( $prop->getName() ); - $docblock->setTag( $patramTag ); - - $return = new ReturnTag( "self" ); - $docblock->setTag( $return ); - - $type = $prop->getType(); - - $method = new MethodGenerator( "set" . Inflector::classify( $prop->getName() ) ); - - $parameter = new ParameterGenerator( $prop->getName(), "mixed", new Generator\ValueGenerator(null, Generator\ValueGenerator::TYPE_NULL) ); - - if ( $type && $type instanceof PHPClassOf ) { - $patramTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); - $parameter->setType( "array" ); - - if ( $p = $this->isOneType( - $type->getArg() - ->getType() - ) - ) { - if ( ( $t = $p->getType() ) ) { - $patramTag->setTypes( $this->getPhpType( $t ) ); - } - } - } elseif ( $type ) { - if ( $this->isNativeType( $type ) ) { - $patramTag->setTypes( $this->getPhpType( $type ) ); - } elseif ( $p = $this->isOneType( $type ) ) { - if ( ( $t = $p->getType() ) && ! $this->isNativeType( $t ) ) { - $patramTag->setTypes( $this->getPhpType( $t ) ); - $parameter->setType( $this->getPhpType( $t ) ); - } elseif ( $t && ! $this->isNativeType( $t ) ) { - $patramTag->setTypes( $this->getPhpType( $t ) ); - $parameter->setType( $this->getPhpType( $t ) ); - } elseif ( $t ) { - $patramTag->setTypes( $this->getPhpType( $t ) ); - } - } else { - $patramTag->setTypes( $this->getPhpType( $type ) ); - $parameter->setType( $this->getPhpType( $type ) ); - } - } - - $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; - $methodBody .= "return \$this;"; - $method->setBody( $methodBody ); - $method->setDocBlock( $docblock ); - $method->setParameter( $parameter ); - - $generator->addMethodFromGenerator( $method ); - } - - private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + $type = $prop->getType(); + + $docblock = new DocBlockGenerator('Construct'); + $paramTag = new ParamTag("value", "mixed"); + $paramTag->setTypes(($type ? $this->getPhpType($type) : "mixed")); + + $docblock->setTag($paramTag); + + $param = new ParameterGenerator("value"); + if ($type && !$this->isNativeType($type)) { + $param->setType($this->getPhpType($type)); + } + $method = new MethodGenerator( + "__construct", [ + $param, + ] + ); + $method->setDocBlock($docblock); + $method->setBody("\$this->value(\$value);"); + + $generator->addMethodFromGenerator($method); + + $docblock = new DocBlockGenerator('Gets or sets the inner value'); + $paramTag = new ParamTag("value", "mixed"); + if ($type && $type instanceof PHPClassOf) { + $paramTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + } elseif ($type) { + $paramTag->setTypes($this->getPhpType($prop->getType())); + } + $docblock->setTag($paramTag); + + $returnTag = new ReturnTag("mixed"); + + if ($type && $type instanceof PHPClassOf) { + $returnTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + } elseif ($type) { + $returnTag->setTypes($this->getPhpType($type)); + } + $docblock->setTag($returnTag); + + $param = new ParameterGenerator("value"); + $param->setDefaultValue(null); + + if ($type && !$this->isNativeType($type)) { + $param->setType($this->getPhpType($type)); + } + $method = new MethodGenerator("value", []); + $method->setDocBlock($docblock); + + $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; + $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; + $methodBody .= "}" . PHP_EOL; + $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; + $method->setBody($methodBody); + + $generator->addMethodFromGenerator($method); + + $docblock = new DocBlockGenerator('Gets a string value'); + $docblock->setTag(new ReturnTag("string")); + $method = new MethodGenerator("__toString"); + $method->setDocBlock($docblock); + $method->setBody("return strval(\$this->" . $prop->getName() . ");"); + $generator->addMethodFromGenerator($method); + } + + private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) + { + $methodBody = ''; + $docblock = new DocBlockGenerator(); + + $docblock->setShortDescription("Sets a new " . $prop->getName()); + + if ($prop->getDoc()) { + $docblock->setLongDescription($prop->getDoc()); + } + + $patramTag = new ParamTag($prop->getName()); + $docblock->setTag($patramTag); + + $return = new ReturnTag("self"); + $docblock->setTag($return); + + $type = $prop->getType(); + + $method = new MethodGenerator("set" . Inflector::classify($prop->getName())); + + $parameter = new ParameterGenerator($prop->getName(), "mixed", new Generator\ValueGenerator(null, Generator\ValueGenerator::TYPE_NULL)); + + if ($type && $type instanceof PHPClassOf) { + $patramTag->setTypes( + $this->getPhpType( + $type->getArg() + ->getType() + ) . "[]" + ); + $parameter->setType("array"); + + if ($p = $this->isOneType( + $type->getArg() + ->getType() + ) + ) { + if (($t = $p->getType())) { + $patramTag->setTypes($this->getPhpType($t)); + } + } + } elseif ($type) { + if ($this->isNativeType($type)) { + $patramTag->setTypes($this->getPhpType($type)); + } elseif ($p = $this->isOneType($type)) { + if (($t = $p->getType()) && !$this->isNativeType($t)) { + $patramTag->setTypes($this->getPhpType($t)); + $parameter->setType($this->getPhpType($t)); + } elseif ($t && !$this->isNativeType($t)) { + $patramTag->setTypes($this->getPhpType($t)); + $parameter->setType($this->getPhpType($t)); + } elseif ($t) { + $patramTag->setTypes($this->getPhpType($t)); + } + } else { + $patramTag->setTypes($this->getPhpType($type)); + $parameter->setType($this->getPhpType($type)); + } + } + + $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; + $methodBody .= "return \$this;"; + $method->setBody($methodBody); + $method->setDocBlock($docblock); + $method->setParameter($parameter); + + $generator->addMethodFromGenerator($method); + } + + private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) { - if ( $prop->getType() instanceof PHPClassOf ) { - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription( "isset " . $prop->getName() ); - if ( $prop->getDoc() ) { - $docblock->setLongDescription( $prop->getDoc() ); - } - - $patramTag = new ParamTag( "index", "scalar" ); - $docblock->setTag( $patramTag ); - - $docblock->setTag( new ReturnTag( "boolean" ) ); - - $paramIndex = new ParameterGenerator( "index", "mixed" ); - - $method = new MethodGenerator( "isset" . Inflector::classify( $prop->getName() ), [ $paramIndex ] ); - $method->setDocBlock( $docblock ); - $method->setBody( "return isset(\$this->" . $prop->getName() . "[\$index]);" ); - $generator->addMethodFromGenerator( $method ); - - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription( "unset " . $prop->getName() ); - if ( $prop->getDoc() ) { - $docblock->setLongDescription( $prop->getDoc() ); - } - - $patramTag = new ParamTag( "index", "scalar" ); - $docblock->setTag( $patramTag ); - $paramIndex = new ParameterGenerator( "index", "mixed" ); - - $docblock->setTag( new ReturnTag( "void" ) ); - - - $method = new MethodGenerator( "unset" . Inflector::classify( $prop->getName() ), [ $paramIndex ] ); - $method->setDocBlock( $docblock ); - $method->setBody( "unset(\$this->" . $prop->getName() . "[\$index]);" ); - $generator->addMethodFromGenerator( $method ); - } - // //// - - $docblock = new DocBlockGenerator(); - - $docblock->setShortDescription( "Gets as " . $prop->getName() ); - - if ( $prop->getDoc() ) { - $docblock->setLongDescription( $prop->getDoc() ); - } - - $tag = new ReturnTag( "mixed" ); - $type = $prop->getType(); - if ( $type && $type instanceof PHPClassOf ) { - $tt = $type->getArg() - ->getType(); - $tag->setTypes( $this->getPhpType( $tt ) . "[]" ); - if ( $p = $this->isOneType( $tt ) ) { - if ( ( $t = $p->getType() ) ) { - $tag->setTypes( $this->getPhpType( $t ) . "[]" ); - } - } - } elseif ( $type ) { - - if ( $p = $this->isOneType( $type ) ) { - if ( $t = $p->getType() ) { - $tag->setTypes( $this->getPhpType( $t ) ); - } - } else { - $tag->setTypes( $this->getPhpType( $type ) ); - } - } - - $docblock->setTag( $tag ); - - $method = new MethodGenerator( "get" . Inflector::classify( $prop->getName() ) ); - $method->setDocBlock( $docblock ); - $method->setBody( "return \$this->" . $prop->getName() . ";" ); - - $generator->addMethodFromGenerator( $method ); - } - - private function isOneType(PHPClass $type, $onlyParent = false ) + if ($prop->getType() instanceof PHPClassOf) { + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription("isset " . $prop->getName()); + if ($prop->getDoc()) { + $docblock->setLongDescription($prop->getDoc()); + } + + $patramTag = new ParamTag("index", "scalar"); + $docblock->setTag($patramTag); + + $docblock->setTag(new ReturnTag("boolean")); + + $paramIndex = new ParameterGenerator("index", "mixed"); + + $method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]); + $method->setDocBlock($docblock); + $method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);"); + $generator->addMethodFromGenerator($method); + + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription("unset " . $prop->getName()); + if ($prop->getDoc()) { + $docblock->setLongDescription($prop->getDoc()); + } + + $patramTag = new ParamTag("index", "scalar"); + $docblock->setTag($patramTag); + $paramIndex = new ParameterGenerator("index", "mixed"); + + $docblock->setTag(new ReturnTag("void")); + + + $method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]); + $method->setDocBlock($docblock); + $method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);"); + $generator->addMethodFromGenerator($method); + } + // //// + + $docblock = new DocBlockGenerator(); + + $docblock->setShortDescription("Gets as " . $prop->getName()); + + if ($prop->getDoc()) { + $docblock->setLongDescription($prop->getDoc()); + } + + $tag = new ReturnTag("mixed"); + $type = $prop->getType(); + if ($type && $type instanceof PHPClassOf) { + $tt = $type->getArg() + ->getType(); + $tag->setTypes($this->getPhpType($tt) . "[]"); + if ($p = $this->isOneType($tt)) { + if (($t = $p->getType())) { + $tag->setTypes($this->getPhpType($t) . "[]"); + } + } + } elseif ($type) { + + if ($p = $this->isOneType($type)) { + if ($t = $p->getType()) { + $tag->setTypes($this->getPhpType($t)); + } + } else { + $tag->setTypes($this->getPhpType($type)); + } + } + + $docblock->setTag($tag); + + $method = new MethodGenerator("get" . Inflector::classify($prop->getName())); + $method->setDocBlock($docblock); + $method->setBody("return \$this->" . $prop->getName() . ";"); + + $generator->addMethodFromGenerator($method); + } + + private function isOneType(PHPClass $type, $onlyParent = false) { - if ( $onlyParent ) { - $e = $type->getExtends(); - if ( $e ) { - if ( $e->hasProperty( '__value' ) ) { - return $e->getProperty( '__value' ); - } - } - } else { - if ( $type->hasPropertyInHierarchy( '__value' ) && count( $type->getPropertiesInHierarchy() ) === 1 ) { - return $type->getPropertyInHierarchy( "__value" ); - } - } - } - - private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + if ($onlyParent) { + $e = $type->getExtends(); + if ($e) { + if ($e->hasProperty('__value')) { + return $e->getProperty('__value'); + } + } + } else { + if ($type->hasPropertyInHierarchy('__value') && count($type->getPropertiesInHierarchy()) === 1) { + return $type->getPropertyInHierarchy("__value"); + } + } + } + + private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) { - $type = $prop->getType(); - $propName = $type->getArg() - ->getName(); - - $docblock = new DocBlockGenerator(); - $docblock->setShortDescription( "Adds as $propName" ); - - if ( $prop->getDoc() ) { - $docblock->setLongDescription( $prop->getDoc() ); - } - - $return = new ReturnTag(); - $return->setTypes( "self" ); - $docblock->setTag( $return ); - - $patramTag = new ParamTag( - $propName, $this->getPhpType( - $type->getArg() - ->getType() - ) - ); - $docblock->setTag( $patramTag ); - - $method = new MethodGenerator( "addTo" . Inflector::classify( $prop->getName() ) ); - - $parameter = new ParameterGenerator( $propName ); - $tt = $type->getArg() - ->getType(); - - if ( ! $this->isNativeType( $tt ) ) { - - if ( $p = $this->isOneType( $tt ) ) { - if ( ( $t = $p->getType() ) ) { - $patramTag->setTypes( $this->getPhpType( $t ) ); - - if ( ! $this->isNativeType( $t ) ) { - $parameter->setType( $this->getPhpType( $t ) ); - } - } - } elseif ( ! $this->isNativeType( $tt ) ) { - $parameter->setType( $this->getPhpType( $tt ) ); - } - } - - $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; - $methodBody .= "return \$this;"; - $method->setBody( $methodBody ); - $method->setDocBlock( $docblock ); - $method->setParameter( $parameter ); - - $generator->addMethodFromGenerator( $method ); - } - - private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class ) + $type = $prop->getType(); + $propName = $type->getArg() + ->getName(); + + $docblock = new DocBlockGenerator(); + $docblock->setShortDescription("Adds as $propName"); + + if ($prop->getDoc()) { + $docblock->setLongDescription($prop->getDoc()); + } + + $return = new ReturnTag(); + $return->setTypes("self"); + $docblock->setTag($return); + + $patramTag = new ParamTag( + $propName, $this->getPhpType( + $type->getArg() + ->getType() + ) + ); + $docblock->setTag($patramTag); + + $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName())); + + $parameter = new ParameterGenerator($propName); + $tt = $type->getArg() + ->getType(); + + if (!$this->isNativeType($tt)) { + + if ($p = $this->isOneType($tt)) { + if (($t = $p->getType())) { + $patramTag->setTypes($this->getPhpType($t)); + + if (!$this->isNativeType($t)) { + $parameter->setType($this->getPhpType($t)); + } + } + } elseif (!$this->isNativeType($tt)) { + $parameter->setType($this->getPhpType($tt)); + } + } + + $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; + $methodBody .= "return \$this;"; + $method->setBody($methodBody); + $method->setDocBlock($docblock); + $method->setParameter($parameter); + + $generator->addMethodFromGenerator($method); + } + + private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) { - if ( $prop->getType() instanceof PHPClassOf ) { - $this->handleAdder( $generator, $prop, $class ); - } + if ($prop->getType() instanceof PHPClassOf) { + $this->handleAdder($generator, $prop, $class); + } - $this->handleGetter( $generator, $prop, $class ); - $this->handleSetter( $generator, $prop, $class ); - } + $this->handleGetter($generator, $prop, $class); + $this->handleSetter($generator, $prop, $class); + } - private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop ) + private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop) { - $generatedProp = new PropertyGenerator( $prop->getName() ); - $generatedProp->setVisibility( PropertyGenerator::VISIBILITY_PRIVATE ); - - $class->addPropertyFromGenerator( $generatedProp ); - - if ( $prop->getType() && ( ! $prop->getType() - ->getNamespace() && $prop->getType() - ->getName() == "array" ) - ) { - // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); - } - - $docBlock = new DocBlockGenerator(); - $generatedProp->setDocBlock( $docBlock ); - - if ( $prop->getDoc() ) { - $docBlock->setLongDescription( $prop->getDoc() ); - } - $tag = new PropertyTag( $prop->getName(), 'mixed' ); - - $type = $prop->getType(); - - if ( $type && $type instanceof PHPClassOf ) { - $tt = $type->getArg() - ->getType(); - $tag->setTypes( $this->getPhpType( $tt ) . "[]" ); - if ( $p = $this->isOneType( $tt ) ) { - if ( ( $t = $p->getType() ) ) { - $tag->setTypes( $this->getPhpType( $t ) . "[]" ); - } - } - } elseif ( $type ) { - - if ( $this->isNativeType( $type ) ) { - $tag->setTypes( $this->getPhpType( $type ) ); - } elseif ( ( $p = $this->isOneType( $type ) ) && ( $t = $p->getType() ) ) { - $tag->setTypes( $this->getPhpType( $t ) ); - } else { - $tag->setTypes( $this->getPhpType( $prop->getType() ) ); - } - } - $docBlock->setTag( $tag ); - } - - private function handleConstant(Generator\ClassGenerator $class, PhpConstant $const ) + $generatedProp = new PropertyGenerator($prop->getName()); + $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE); + + $class->addPropertyFromGenerator($generatedProp); + + if ($prop->getType() && (!$prop->getType() + ->getNamespace() && $prop->getType() + ->getName() == "array") + ) { + // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); + } + + $docBlock = new DocBlockGenerator(); + $generatedProp->setDocBlock($docBlock); + + if ($prop->getDoc()) { + $docBlock->setLongDescription($prop->getDoc()); + } + $tag = new PropertyTag($prop->getName(), 'mixed'); + + $type = $prop->getType(); + + if ($type && $type instanceof PHPClassOf) { + $tt = $type->getArg() + ->getType(); + $tag->setTypes($this->getPhpType($tt) . "[]"); + if ($p = $this->isOneType($tt)) { + if (($t = $p->getType())) { + $tag->setTypes($this->getPhpType($t) . "[]"); + } + } + } elseif ($type) { + + if ($this->isNativeType($type)) { + $tag->setTypes($this->getPhpType($type)); + } elseif (($p = $this->isOneType($type)) && ($t = $p->getType())) { + $tag->setTypes($this->getPhpType($t)); + } else { + $tag->setTypes($this->getPhpType($prop->getType())); + } + } + $docBlock->setTag($tag); + } + + private function handleConstant(Generator\ClassGenerator $class, PhpConstant $const) { - $class->addConstant( $const->getName(), $const->getValue() ); - } + $class->addConstant($const->getName(), $const->getValue()); + } - public function generate(Generator\ClassGenerator $class, PHPClass $type ) + public function generate(Generator\ClassGenerator $class, PHPClass $type) { - $docblock = new DocBlockGenerator( "Class representing " . $type->getName() ); - if ( $type->getDoc() ) { - $docblock->setLongDescription( $type->getDoc() ); - } - $class->setNamespaceName( $type->getNamespace() ); - $class->setName( $type->getName() ); - $class->setDocblock( $docblock ); - - if ( $extends = $type->getExtends() ) { - - if ( $p = $this->isOneType( $extends ) ) { - $this->handleProperty( $class, $p ); - $this->handleValueMethod( $class, $p, $extends ); - } else { - - $class->setExtendedClass( $extends->getName() ); - - if ( $extends->getNamespace() != $type->getNamespace() ) { - if ( $extends->getName() == $type->getName() ) { - $class->addUse( - $type->getExtends() - ->getFullName(), - $extends->getName() . "Base" - ); - $class->setExtendedClass( $extends->getName() . "Base" ); - } else { - $class->addUse( $extends->getFullName() ); - } - } - } - } - - if ( $this->handleBody( $class, $type ) ) { - return true; - } - } + $docblock = new DocBlockGenerator("Class representing " . $type->getName()); + if ($type->getDoc()) { + $docblock->setLongDescription($type->getDoc()); + } + $class->setNamespaceName($type->getNamespace()); + $class->setName($type->getName()); + $class->setDocblock($docblock); + + if ($extends = $type->getExtends()) { + + if ($p = $this->isOneType($extends)) { + $this->handleProperty($class, $p); + $this->handleValueMethod($class, $p, $extends); + } else { + + $class->setExtendedClass($extends->getName()); + + if ($extends->getNamespace() != $type->getNamespace()) { + if ($extends->getName() == $type->getName()) { + $class->addUse( + $type->getExtends() + ->getFullName(), + $extends->getName() . "Base" + ); + $class->setExtendedClass($extends->getName() . "Base"); + } else { + $class->addUse($extends->getFullName()); + } + } + } + } + + if ($this->handleBody($class, $type)) { + return true; + } + } } From 253a2885ce1e598d962dcc9f67836cc83abd57c2 Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:39:27 -0700 Subject: [PATCH 10/15] more reformatting --- lib/Php/ClassGenerator.php | 75 +++++++++++--------------------------- 1 file changed, 21 insertions(+), 54 deletions(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index d123e3a..8d63213 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -77,12 +77,7 @@ private function getPhpType(PHPClass $class) return "\\" . $class->getFullName(); } - private function handleValueMethod( - Generator\ClassGenerator $generator, - PHPProperty $prop, - PHPClass $class, - $all = true - ) + private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true) { $type = $prop->getType(); @@ -96,11 +91,9 @@ private function handleValueMethod( if ($type && !$this->isNativeType($type)) { $param->setType($this->getPhpType($type)); } - $method = new MethodGenerator( - "__construct", [ - $param, - ] - ); + $method = new MethodGenerator("__construct", [ + $param, + ]); $method->setDocBlock($docblock); $method->setBody("\$this->value(\$value);"); @@ -109,12 +102,8 @@ private function handleValueMethod( $docblock = new DocBlockGenerator('Gets or sets the inner value'); $paramTag = new ParamTag("value", "mixed"); if ($type && $type instanceof PHPClassOf) { - $paramTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); + $paramTag->setTypes($this->getPhpType($type->getArg() + ->getType()) . "[]"); } elseif ($type) { $paramTag->setTypes($this->getPhpType($prop->getType())); } @@ -123,12 +112,8 @@ private function handleValueMethod( $returnTag = new ReturnTag("mixed"); if ($type && $type instanceof PHPClassOf) { - $returnTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); + $returnTag->setTypes($this->getPhpType($type->getArg() + ->getType()) . "[]"); } elseif ($type) { $returnTag->setTypes($this->getPhpType($type)); } @@ -183,17 +168,12 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ $parameter = new ParameterGenerator($prop->getName(), "mixed", new Generator\ValueGenerator(null, Generator\ValueGenerator::TYPE_NULL)); if ($type && $type instanceof PHPClassOf) { - $patramTag->setTypes( - $this->getPhpType( - $type->getArg() - ->getType() - ) . "[]" - ); + $patramTag->setTypes($this->getPhpType($type->getArg() + ->getType()) . "[]"); $parameter->setType("array"); - if ($p = $this->isOneType( - $type->getArg() - ->getType() + if ($p = $this->isOneType($type->getArg() + ->getType() ) ) { if (($t = $p->getType())) { @@ -281,8 +261,7 @@ private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $ $tag = new ReturnTag("mixed"); $type = $prop->getType(); if ($type && $type instanceof PHPClassOf) { - $tt = $type->getArg() - ->getType(); + $tt = $type->getArg()->getType(); $tag->setTypes($this->getPhpType($tt) . "[]"); if ($p = $this->isOneType($tt)) { if (($t = $p->getType())) { @@ -328,8 +307,7 @@ private function isOneType(PHPClass $type, $onlyParent = false) private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) { $type = $prop->getType(); - $propName = $type->getArg() - ->getName(); + $propName = $type->getArg()->getName(); $docblock = new DocBlockGenerator(); $docblock->setShortDescription("Adds as $propName"); @@ -342,19 +320,14 @@ private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $p $return->setTypes("self"); $docblock->setTag($return); - $patramTag = new ParamTag( - $propName, $this->getPhpType( - $type->getArg() - ->getType() - ) - ); + $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg() + ->getType())); $docblock->setTag($patramTag); $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName())); $parameter = new ParameterGenerator($propName); - $tt = $type->getArg() - ->getType(); + $tt = $type->getArg()->getType(); if (!$this->isNativeType($tt)) { @@ -397,10 +370,7 @@ private function handleProperty(Generator\ClassGenerator $class, PHPProperty $pr $class->addPropertyFromGenerator($generatedProp); - if ($prop->getType() && (!$prop->getType() - ->getNamespace() && $prop->getType() - ->getName() == "array") - ) { + if ($prop->getType() && (!$prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) { // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); } @@ -415,8 +385,7 @@ private function handleProperty(Generator\ClassGenerator $class, PHPProperty $pr $type = $prop->getType(); if ($type && $type instanceof PHPClassOf) { - $tt = $type->getArg() - ->getType(); + $tt = $type->getArg()->getType(); $tag->setTypes($this->getPhpType($tt) . "[]"); if ($p = $this->isOneType($tt)) { if (($t = $p->getType())) { @@ -462,10 +431,8 @@ public function generate(Generator\ClassGenerator $class, PHPClass $type) if ($extends->getNamespace() != $type->getNamespace()) { if ($extends->getName() == $type->getName()) { - $class->addUse( - $type->getExtends() - ->getFullName(), - $extends->getName() . "Base" + $class->addUse($type->getExtends() + ->getFullName(), $extends->getName() . "Base" ); $class->setExtendedClass($extends->getName() . "Base"); } else { From 8f57d66295b086c12760ccd798a8719106ab42df Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:41:26 -0700 Subject: [PATCH 11/15] should be the last reformat --- lib/Php/ClassGenerator.php | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 8d63213..1166abe 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -49,19 +49,16 @@ private function handleBody(Generator\ClassGenerator $class, PHPClass $type) private function isNativeType(PHPClass $class) { - return !$class->getNamespace() && in_array( - $class->getName(), - [ - 'string', - 'int', - 'float', - 'integer', - 'boolean', - 'array', - 'mixed', - 'callable', - ] - ); + return !$class->getNamespace() && in_array($class->getName(), [ + 'string', + 'int', + 'float', + 'integer', + 'boolean', + 'array', + 'mixed', + 'callable', + ]); } private function getPhpType(PHPClass $class) @@ -92,7 +89,7 @@ private function handleValueMethod(Generator\ClassGenerator $generator, PHPPrope $param->setType($this->getPhpType($type)); } $method = new MethodGenerator("__construct", [ - $param, + $param ]); $method->setDocBlock($docblock); $method->setBody("\$this->value(\$value);"); @@ -173,8 +170,7 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ $parameter->setType("array"); if ($p = $this->isOneType($type->getArg() - ->getType() - ) + ->getType()) ) { if (($t = $p->getType())) { $patramTag->setTypes($this->getPhpType($t)); @@ -432,8 +428,7 @@ public function generate(Generator\ClassGenerator $class, PHPClass $type) if ($extends->getNamespace() != $type->getNamespace()) { if ($extends->getName() == $type->getName()) { $class->addUse($type->getExtends() - ->getFullName(), $extends->getName() . "Base" - ); + ->getFullName(), $extends->getName() . "Base"); $class->setExtendedClass($extends->getName() . "Base"); } else { $class->addUse($extends->getFullName()); From 33b8c3ce5f6ee79b5f9464da7725e82af5f9fe0d Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:42:35 -0700 Subject: [PATCH 12/15] ok. I'm done reformatting now ;) --- lib/Php/ClassGenerator.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 1166abe..f809deb 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -57,7 +57,7 @@ private function isNativeType(PHPClass $class) 'boolean', 'array', 'mixed', - 'callable', + 'callable' ]); } @@ -67,10 +67,8 @@ private function getPhpType(PHPClass $class) if ($this->isNativeType($class)) { return $class->getName(); } - return "\\" . $class->getName(); } - return "\\" . $class->getFullName(); } @@ -441,5 +439,4 @@ public function generate(Generator\ClassGenerator $class, PHPClass $type) return true; } } - } From 5e5ab71ea7c868b8e573a3519e03a0fcb3cc8279 Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 11:58:57 -0700 Subject: [PATCH 13/15] Reformatting a little bit more --- lib/Php/Structure/PHPClass.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Php/Structure/PHPClass.php b/lib/Php/Structure/PHPClass.php index 676fc34..808148a 100644 --- a/lib/Php/Structure/PHPClass.php +++ b/lib/Php/Structure/PHPClass.php @@ -240,14 +240,16 @@ public function setAbstract($abstract) return $this; } - public function addConstant( $constantName, $constantValue ) { - $this->constants[] = new PhpConstant( $constantName, $constantValue ); + public function addConstant($constantName, $constantValue) + { + $this->constants[] = new PhpConstant($constantName, $constantValue); } /** * @return PHPConstant[] */ - public function getConstants() { + public function getConstants() + { return $this->constants; } } From 2ba5d3d6bac09aafcc35d40bbd021a246b39e7f8 Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Fri, 23 Sep 2016 12:47:25 -0700 Subject: [PATCH 14/15] make enumeration elements not use CDATA --- lib/Jms/YamlConverter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Jms/YamlConverter.php b/lib/Jms/YamlConverter.php index 0c82da0..94c3db1 100644 --- a/lib/Jms/YamlConverter.php +++ b/lib/Jms/YamlConverter.php @@ -263,6 +263,11 @@ private function visitSimpleType(&$class, &$data, SimpleType $type, $name) if ($parent instanceof Type) { $this->handleClassExtension($class, $data, $parent, $name); } + + $checks = $restriction->getChecks(); + if (count($checks) && isset($checks['enumeration'])) { + $data['properties']['__value']['xml_element'] = ['cdata' => false]; + } } elseif ($unions = $type->getUnions()) { foreach ($unions as $i => $unon) { $this->handleClassExtension($class, $data, $unon, $name . $i); From ac762b59b8bdcd03347e17ef9b5867368fe36d8a Mon Sep 17 00:00:00 2001 From: Ryder Ross Date: Thu, 20 Oct 2016 21:26:44 -0700 Subject: [PATCH 15/15] If constant starts with a number, prefix with an underscore --- lib/Php/PhpConverter.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Php/PhpConverter.php b/lib/Php/PhpConverter.php index a656744..80245a7 100644 --- a/lib/Php/PhpConverter.php +++ b/lib/Php/PhpConverter.php @@ -521,6 +521,10 @@ public function createConstantNameFromValue( $constantValue ) { $constantName = 'NUM_' . $constantName; } + if (is_numeric(substr($constantName, 0, 1))) { + $constantName = '_' . $constantName; + } + if ($constantName === '') { $constantName = 'EMPTY_STRING'; } @@ -529,5 +533,5 @@ public function createConstantNameFromValue( $constantValue ) { return $constantName; } - + }