Skip to content

Commit

Permalink
Merge pull request #89 from riccardonar/master
Browse files Browse the repository at this point in the history
Add config to remove CDATA
  • Loading branch information
goetas authored Apr 19, 2019
2 parents d3bab26 + 5b4b5be commit 474f18d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function getConfigTreeBuilder()
->prototype('scalar')
->end()
->end()
->arrayNode('configs_jms')
->addDefaultsIfNotSet()
->children()
->booleanNode('xml_cdata')
->defaultTrue()
->end()
->end()
->end()
->arrayNode('destinations_php')->fixXmlConfig('destination')
->cannotBeEmpty()->isRequired()
->requiresAtLeastOneElement()
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Xsd2PhpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function load(array $configs, ContainerBuilder $container)
}
}

if ($config['configs_jms']) {
$converter = $container->getDefinition('goetas_webservices.xsd2php.converter.jms');
$converter->addMethodCall('setUseCdata', ['xml_cdata', $config['configs_jms']['xml_cdata']]);
}

$container->setParameter('goetas_webservices.xsd2php.config', $config);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Jms/YamlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class YamlConverter extends AbstractConverter
{

protected $useCdata = true;

public function __construct(NamingStrategy $namingStrategy)
{

Expand All @@ -40,6 +42,13 @@ public function __construct(NamingStrategy $namingStrategy)
});
}

public function setUseCdata($value)
{
$this->logger->info("Set useCdata $value");
$this->useCdata = $value;
return $this;
}

private $classes = [];

public function convert(array $schemas)
Expand Down Expand Up @@ -315,6 +324,9 @@ private function handleClassExtension(&$class, &$data, Type $type, $parentName)
$property["accessor"]["getter"] = "value";
$property["accessor"]["setter"] = "value";
$property["type"] = $alias;
if (!$this->useCdata) {
$property["xml_element"]["cdata"] = $this->useCdata;
}

$data["properties"]["__value"] = $property;

Expand All @@ -332,6 +344,9 @@ private function handleClassExtension(&$class, &$data, Type $type, $parentName)
$property["access_type"] = "public_method";
$property["accessor"]["getter"] = "value";
$property["accessor"]["setter"] = "value";
if (!$this->useCdata) {
$property["xml_element"]["cdata"] = $this->useCdata;
}

if ($valueProp = $this->typeHasValue($type, $class, $parentName)) {
$property["type"] = $valueProp;
Expand Down Expand Up @@ -420,6 +435,9 @@ private function visitElement(&$class, Schema $schema, ElementItem $element, $ar
$property["access_type"] = "public_method";
$property["serialized_name"] = $element->getName();

if (!$this->useCdata) {
$property["xml_element"]["cdata"] = $this->useCdata;
}
if ($element->getSchema()->getTargetNamespace() && ($schema->getElementsQualification() || ($element instanceof Element && $element->isQualified()))) {
$property["xml_element"]["namespace"] = $element->getSchema()->getTargetNamespace();
}
Expand Down
79 changes: 79 additions & 0 deletions tests/Converter/JMS/Xsd2JmsElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,85 @@
class Xsd2PhpElementTest extends Xsd2JmsBase
{

/**
* @dataProvider getPrimitiveTypeConversions
*/
public function testElementOfPrimitiveTypeWithCdata($xsType, $phpName)
{
$xml = '
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element-one" type="' . $xsType . '">
</xs:element>
</xs:schema>
';

$this->converter->setUseCdata(true);
$classes = $this->getClasses($xml);
$this->assertCount(1, $classes);

$this->assertEquals(
array(
'Example\ElementOne' => array(
'xml_root_name' => 'ns-8ece61d2:element-one',
'xml_root_namespace' => 'http://www.example.com',
'properties' => array(
'__value' => array(
'expose' => true,
'xml_value' => true,
'access_type' => 'public_method',
'accessor' => array(
'getter' => 'value',
'setter' => 'value'
),
'type' => $phpName
)
)
)
), $classes['Example\ElementOne']);
}

/**
* @dataProvider getPrimitiveTypeConversions
*/
public function testElementOfPrimitiveTypeWithoutCdata($xsType, $phpName)
{
$xml = '
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element-one" type="' . $xsType . '">
</xs:element>
</xs:schema>
';

$this->converter->setUseCdata(false);
$classes = $this->getClasses($xml);
$this->assertCount(1, $classes);

$this->assertEquals(
array(
'Example\ElementOne' => array(
'xml_root_name' => 'ns-8ece61d2:element-one',
'xml_root_namespace' => 'http://www.example.com',
'properties' => array(
'__value' => array(
'expose' => true,
'xml_value' => true,
'access_type' => 'public_method',
'accessor' => array(
'getter' => 'value',
'setter' => 'value'
),
'xml_element' => array(
'cdata' => false
),
'type' => $phpName
)
)
)
), $classes['Example\ElementOne']);
}

/**
* @dataProvider getPrimitiveTypeConversions
*/
Expand Down

0 comments on commit 474f18d

Please sign in to comment.