From 2c9dffcb74dc5eb51294b793daa4317a82c6088a Mon Sep 17 00:00:00 2001 From: Joeri van Dooren Date: Wed, 29 Apr 2015 22:09:37 +0200 Subject: [PATCH] Implement add-prefix option to allow developers to choose the add-method prefix. --- lib/Command/AbstractConvert.php | 5 +++-- lib/Command/ConvertToPHP.php | 7 ++++++- lib/Command/ConvertToYaml.php | 3 ++- lib/Php/ClassGenerator.php | 22 +++++++++++++++++++- tests/PHP/PHPConversionTest.php | 37 ++++++++++++++++++++++++++++++++- 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/lib/Command/AbstractConvert.php b/lib/Command/AbstractConvert.php index 87aec31..aeac621 100644 --- a/lib/Command/AbstractConvert.php +++ b/lib/Command/AbstractConvert.php @@ -8,6 +8,7 @@ use Goetas\XML\XSDReader\SchemaReader; use Goetas\Xsd\XsdToPhp\AbstractConverter; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputInterface; use Goetas\Xsd\XsdToPhp\Naming\ShortNamingStrategy; use Goetas\Xsd\XsdToPhp\Naming\LongNamingStrategy; use Goetas\Xsd\XsdToPhp\Naming\NamingStrategy; @@ -117,10 +118,10 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $schemas[spl_object_hash($schema)] = $schema; } - $this->convert($converter, $schemas, $targets, $output); + $this->convert($converter, $schemas, $targets, $input, $output); return 0; } - protected abstract function convert(AbstractConverter $converter, array $schemas, array $targets, OutputInterface $output); + protected abstract function convert(AbstractConverter $converter, array $schemas, array $targets, InputInterface $input, OutputInterface $output); } diff --git a/lib/Command/ConvertToPHP.php b/lib/Command/ConvertToPHP.php index 91e9201..472ac1a 100644 --- a/lib/Command/ConvertToPHP.php +++ b/lib/Command/ConvertToPHP.php @@ -6,6 +6,8 @@ use Goetas\Xsd\XsdToPhp\Php\PathGenerator\Psr4PathGenerator; use Goetas\Xsd\XsdToPhp\AbstractConverter; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Zend\Code\Generator\FileGenerator; use Goetas\Xsd\XsdToPhp\Naming\NamingStrategy; @@ -21,6 +23,7 @@ protected function configure() parent::configure(); $this->setName('convert:php'); $this->setDescription('Convert XSD definitions into PHP classes'); + $this->addOption('add-prefix', null, InputOption::VALUE_REQUIRED, 'Set the prefix for the add-methods.', 'addTo'); } protected function getConverterter(NamingStrategy $naming) @@ -28,9 +31,11 @@ protected function getConverterter(NamingStrategy $naming) return new PhpConverter($naming); } - protected function convert(AbstractConverter $converter, array $schemas, array $targets, OutputInterface $output) + protected function convert(AbstractConverter $converter, array $schemas, array $targets, InputInterface $input, OutputInterface $output) { $generator = new ClassGenerator(); + $generator->setAddPrefix($input->getOption('add-prefix')); + $pathGenerator = new Psr4PathGenerator($targets); $progress = $this->getHelperSet()->get('progress'); diff --git a/lib/Command/ConvertToYaml.php b/lib/Command/ConvertToYaml.php index 9221020..fc03f60 100644 --- a/lib/Command/ConvertToYaml.php +++ b/lib/Command/ConvertToYaml.php @@ -5,6 +5,7 @@ use Goetas\Xsd\XsdToPhp\Jms\YamlConverter; use Symfony\Component\Yaml\Dumper; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputInterface; use Goetas\Xsd\XsdToPhp\AbstractConverter; use Goetas\Xsd\XsdToPhp\Naming\NamingStrategy; @@ -27,7 +28,7 @@ protected function getConverterter(NamingStrategy $naming) return new YamlConverter($naming); } - protected function convert(AbstractConverter $converter, array $schemas, array $targets, OutputInterface $output) + protected function convert(AbstractConverter $converter, array $schemas, array $targets, InputInterface $input, OutputInterface $output) { $items = $converter->convert($schemas); diff --git a/lib/Php/ClassGenerator.php b/lib/Php/ClassGenerator.php index 19c524e..9e0da88 100644 --- a/lib/Php/ClassGenerator.php +++ b/lib/Php/ClassGenerator.php @@ -16,6 +16,26 @@ class ClassGenerator { + /** + * @var string $addPrefix + */ + private $addPrefix = 'addTo'; + + /** + * @return string + */ + public function getAddPrefix() + { + return $this->addPrefix; + } + + /** + * @param string $addPrefix + */ + public function setAddPrefix($addPrefix) + { + $this->addPrefix = $addPrefix; + } private function handleBody(Generator\ClassGenerator $class, PHPClass $type) { @@ -308,7 +328,7 @@ private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $p ->getType())); $docblock->setTag($patramTag); - $method = new MethodGenerator("addTo".Inflector::classify($prop->getName())); + $method = new MethodGenerator($this->getAddPrefix().Inflector::classify($prop->getName())); $parameter = new ParameterGenerator($propName); $tt = $type->getArg()->getType(); diff --git a/tests/PHP/PHPConversionTest.php b/tests/PHP/PHPConversionTest.php index 08a4e64..7046bdf 100644 --- a/tests/PHP/PHPConversionTest.php +++ b/tests/PHP/PHPConversionTest.php @@ -12,14 +12,18 @@ class PHPConversionTest extends \PHPUnit_Framework_TestCase /** * * @param mixed $xml + * @param string $addPrefix + * * @return \Zend\Code\Generator\ClassGenerator[] */ - protected function getClasses($xml) + protected function getClasses($xml, $addPrefix = 'addTo') { $phpcreator = new PhpConverter(new ShortNamingStrategy()); $phpcreator->addNamespace('http://www.example.com', 'Example'); $generator = new ClassGenerator(); + $generator->setAddPrefix($addPrefix); + $reader = new SchemaReader(); if (! is_array($xml)) { @@ -244,4 +248,35 @@ public function testSimpleMulteplicity() $this->assertTrue($single->hasMethod('getId')); $this->assertTrue($single->hasMethod('setId')); } + + public function testAddPrefix() + { + $xml = ' + + + + + + + + + + + + + + + + '; + + $items = $this->getClasses($xml, 'add'); + + $this->assertCount(1, $items); + + $single = $items['Example\SingleType']; + $this->assertTrue($single->hasMethod('addA')); + $this->assertTrue($single->hasMethod('addB')); + + } } \ No newline at end of file