Skip to content

Commit

Permalink
Merge pull request #63 from rtek/master
Browse files Browse the repository at this point in the history
Added zend-code ^3.0.3 compatibility
  • Loading branch information
goetas authored Apr 18, 2018
2 parents 889c7d1 + a895f3c commit 4ae6efb
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 68 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
"symfony/config": "^2.2|^3.0|^4.0",
"goetas-webservices/xsd-reader": "^0.2|^0.3.1",
"doctrine/inflector": "^1.0",
"zendframework/zend-code": "~2.3",
"zendframework/zend-code": "^3.0.3",

"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.0",
"jms/serializer": "^1.3",
"goetas-webservices/xsd2php-runtime": "^0.2.7"
"jms/serializer": "^1.9",
"goetas-webservices/xsd2php-runtime": "^0.2.7",
"ext-xmldiff": "*"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 17 additions & 17 deletions src/AbstractConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public function __construct(NamingStrategy $namingStrategy, LoggerInterface $log
$this->logger = $logger ?: new NullLogger();

$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYearMonth", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonthDay", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonth", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYear", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NMTOKEN", function (Type $type) {
return "string";
Expand Down Expand Up @@ -107,43 +107,43 @@ public function __construct(NamingStrategy $namingStrategy, LoggerInterface $log
return "string";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "integer", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "int", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedInt", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "negativeInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonPositiveInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "long", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedLong", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "short", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "boolean", function (Type $type) {
return "boolean";
return "bool";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) {
return "integer";
return "int";
});
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "language", function (Type $type) {
return "string";
Expand Down
3 changes: 2 additions & 1 deletion src/Jms/YamlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
use GoetasWebservices\Xsd\XsdToPhp\AbstractConverter;
use GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy;
use GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClass;

class YamlConverter extends AbstractConverter
{
Expand Down Expand Up @@ -409,7 +410,7 @@ private function typeHasValue(Type $type, $parentClass, $name)
* @param PHPClass $class
* @param Schema $schema
* @param Element $element
* @param boolean $arrayize
* @param bool $arrayize
* @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPProperty
*/
private function visitElement(&$class, Schema $schema, ElementItem $element, $arrayize = true)
Expand Down
33 changes: 9 additions & 24 deletions src/Php/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,13 @@ private function handleBody(Generator\ClassGenerator $class, PHPClass $type)
return true;
}

private function isNativeType(PHPClass $class)
{
return !$class->getNamespace() && in_array($class->getName(), [
'string',
'int',
'float',
'integer',
'boolean',
'array',
'mixed',
'callable'
]);
}

private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true)
{
$type = $prop->getType();

$docblock = new DocBlockGenerator('Construct');
$docblock->setWordWrap(false);
$paramTag = new ParamTag("value", "mixed");
$paramTag = new ParamTag("value");
$paramTag->setTypes(($type ? $type->getPhpType() : "mixed"));

$docblock->setTag($paramTag);
Expand All @@ -76,7 +62,7 @@ private function handleValueMethod(Generator\ClassGenerator $generator, PHPPrope

$docblock = new DocBlockGenerator('Gets or sets the inner value');
$docblock->setWordWrap(false);
$paramTag = new ParamTag("value", "mixed");
$paramTag = new ParamTag("value");
if ($type && $type instanceof PHPClassOf) {
$paramTag->setTypes($type->getArg()->getType()->getPhpType() . "[]");
} elseif ($type) {
Expand Down Expand Up @@ -141,7 +127,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());

if ($type && $type instanceof PHPClassOf) {
$patramTag->setTypes($type->getArg()
Expand Down Expand Up @@ -193,12 +179,12 @@ private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $
$docblock->setLongDescription($prop->getDoc());
}

$patramTag = new ParamTag("index", "scalar");
$patramTag = new ParamTag("index", "int|string");
$docblock->setTag($patramTag);

$docblock->setTag(new ReturnTag("boolean"));
$docblock->setTag(new ReturnTag("bool"));

$paramIndex = new ParameterGenerator("index", "mixed");
$paramIndex = new ParameterGenerator("index");

$method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]);
$method->setDocBlock($docblock);
Expand All @@ -212,9 +198,9 @@ private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $
$docblock->setLongDescription($prop->getDoc());
}

$patramTag = new ParamTag("index", "scalar");
$patramTag = new ParamTag("index", "int|string");
$docblock->setTag($patramTag);
$paramIndex = new ParameterGenerator("index", "mixed");
$paramIndex = new ParameterGenerator("index");

$docblock->setTag(new ReturnTag("void"));

Expand Down Expand Up @@ -387,12 +373,11 @@ public function generate(PHPClass $type)
$this->handleValueMethod($class, $p, $extends);
} else {

$class->setExtendedClass($extends->getName());
$class->setExtendedClass($extends->getFullName());

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());
}
Expand Down
5 changes: 3 additions & 2 deletions src/Php/PathGenerator/Psr4PathGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public function getPath(ClassGenerator $php)
if (strpos(trim($php->getNamespaceName()) . "\\", $namespace) === 0) {
$d = strtr(substr($php->getNamespaceName(), strlen($namespace)), "\\", "/");
$dir = rtrim($dir, "/") . "/" . $d;
if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
throw new PathGeneratorException("Can't create the '$dir' directory");
if (!is_dir($dir) && !@mkdir($dir, 0777, true)) {
$error = error_get_last();
throw new PathGeneratorException("Can't create the '$dir' directory: '{$error['message']}'");
}

return rtrim($dir, "/") . "/" . $php->getName() . ".php";
Expand Down
4 changes: 2 additions & 2 deletions src/Php/PhpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private function findPHPName(Type $type)
/**
*
* @param Type $type
* @param boolean $force
* @param bool $force
* @param bool $skip
* @return PHPClass
* @throws Exception
Expand Down Expand Up @@ -399,7 +399,7 @@ private function visitAttribute(PHPClass $class, Schema $schema, AttributeItem $
* @param PHPClass $class
* @param Schema $schema
* @param Element $element
* @param boolean $arrayize
* @param bool $arrayize
* @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPProperty
*/
private function visitElement(PHPClass $class, Schema $schema, ElementSingle $element, $arrayize = true)
Expand Down
14 changes: 7 additions & 7 deletions src/Php/Structure/PHPClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function isNativeType()
'string',
'int',
'float',
'integer',
'boolean',
'bool',
'array',
'mixed',
'callable'
'callable',

'mixed' //todo this is not a php type but it's needed for now to allow mixed return tags
]);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ public function getProperties()
/**
*
* @param string $name
* @return boolean
* @return bool
*/
public function hasProperty($name)
{
Expand Down Expand Up @@ -245,7 +245,7 @@ public function addProperty(PHPProperty $property)

/**
*
* @var boolean
* @var bool
*/
protected $abstract;

Expand Down Expand Up @@ -282,7 +282,7 @@ public function getAbstract()

public function setAbstract($abstract)
{
$this->abstract = (boolean)$abstract;
$this->abstract = (bool)$abstract;
return $this;
}
}
4 changes: 4 additions & 0 deletions src/Writer/PHPClassWriter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GoetasWebservices\Xsd\XsdToPhp\Writer;

use GoetasWebservices\Xsd\XsdToPhp\Php\ClassGenerator;
use GoetasWebservices\Xsd\XsdToPhp\Php\PathGenerator\PathGenerator;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand All @@ -20,6 +21,9 @@ public function __construct(PathGenerator $pathGenerator, LoggerInterface $logge
$this->logger = $logger ?: new NullLogger();
}

/**
* @param ClassGenerator[] $items
*/
public function write(array $items)
{
foreach ($items as $item) {
Expand Down
11 changes: 8 additions & 3 deletions src/Writer/PHPWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public function __construct(PHPClassWriter $classWriter, ClassGenerator $generat
$this->logger = $logger ?: new NullLogger();
}

/**
* @param PHPClass[] $items
*/
public function write(array $items)
{
return $this->classWriter->write(array_filter(array_map(function (PHPClass $item) {
return $this->generator->generate($item);
}, $items)));
while($item = array_pop($items)) {
if($generator = $this->generator->generate($item)) {
$this->classWriter->write([$generator]);
}
}
}
}
2 changes: 1 addition & 1 deletion tests/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $targetNs, array $aliases = array(), $tmp = nu
$this->phpDir = "$tmp/php";
$this->jmsDir = "$tmp/jms";

$this->namingStrategy = new ShortNamingStrategy();
$this->namingStrategy = defined('PHP_WINDOWS_VERSION_BUILD') ? new VeryShortNamingStrategy() : new ShortNamingStrategy();

$this->loader = new ClassLoader();
foreach ($this->targetNs as $phpNs) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Converter/JMS/Xsd2JmsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function getPrimitiveTypeConversions()
return [
['xs:string', 'string'],
['xs:decimal', 'float'],
['xs:int', 'integer'],
['xs:integer', 'integer'],
['xs:int', 'int'],
['xs:integer', 'int'],
];
}
}
}
8 changes: 3 additions & 5 deletions tests/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ class Generator extends AbstractGenerator
public function generate(array $schemas)
{
$this->cleanDirectories();

list($php, $jms) = $this->getData($schemas);

$this->writeJMS($jms);
$this->writePHP($php);

$this->writeJMS($this->generateJMSFiles($schemas));
$this->writePHP($this->generatePHPFiles($schemas));
}

public function getData(array $schemas)
Expand Down
4 changes: 4 additions & 0 deletions tests/PHP/PHPConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function testMulteplicity()

$this->assertTrue($codegen->hasMethod('getId'));
$this->assertTrue($codegen->hasMethod('setId'));

$this->assertNull($codegen->getMethod('issetId')->getParameters()['index']->getType());
$this->assertNull($codegen->getMethod('issetId')->getParameters()['index']->getType());

}

public function testNestedMulteplicity()
Expand Down
Loading

0 comments on commit 4ae6efb

Please sign in to comment.