Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Command/AbstractConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
7 changes: 6 additions & 1 deletion lib/Command/ConvertToPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,16 +23,19 @@ 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)
{
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');

Expand Down
3 changes: 2 additions & 1 deletion lib/Command/ConvertToYaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
22 changes: 21 additions & 1 deletion lib/Php/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 36 additions & 1 deletion tests/PHP/PHPConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -244,4 +248,35 @@ public function testSimpleMulteplicity()
$this->assertTrue($single->hasMethod('getId'));
$this->assertTrue($single->hasMethod('setId'));
}

public function testAddPrefix()
{
$xml = '
<xs:schema targetNamespace="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="ArrayOfStrings">
<xs:all>
<xs:element name="string" type="xs:string" maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>

<xs:complexType name="Single">
<xs:all>
<xs:element name="a" type="ArrayOfStrings"/>
<xs:element name="b" type="ArrayOfStrings"/>
</xs:all>
</xs:complexType>

</xs:schema>';

$items = $this->getClasses($xml, 'add');

$this->assertCount(1, $items);

$single = $items['Example\SingleType'];
$this->assertTrue($single->hasMethod('addA'));
$this->assertTrue($single->hasMethod('addB'));

}
}