Skip to content

Commit 29234ed

Browse files
committed
1 parent 98b395a commit 29234ed

File tree

8 files changed

+208
-4
lines changed

8 files changed

+208
-4
lines changed

src/AbstractConverter.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ abstract public function convert(array $schemas);
3636

3737
protected $typeAliases = [];
3838

39+
protected $rootPrefixes = [];
40+
3941
protected $aliasCache = [];
4042

4143
public function addAliasMap($ns, $name, callable $handler)
@@ -56,7 +58,7 @@ public function getTypeAliases(): array
5658
return $this->typeAliases;
5759
}
5860

59-
public function getTypeAlias($type, ?Schema $schemapos = null)
61+
public function getTypeAlias($type, Schema $schemapos = null)
6062
{
6163
$schema = $schemapos ?: $type->getSchema();
6264

@@ -69,7 +71,27 @@ public function getTypeAlias($type, ?Schema $schemapos = null)
6971
}
7072
}
7173

72-
public function __construct(NamingStrategy $namingStrategy, ?LoggerInterface $logger = null)
74+
public function addRootPrefix($ns, $name, $prefix)
75+
{
76+
$this->logger->info("Added prefix $ns $prefix for $name");
77+
$this->rootPrefixes[$ns][$name] = $prefix;
78+
}
79+
80+
public function getRootPrefixes()
81+
{
82+
return $this->rootPrefixes;
83+
}
84+
85+
public function getRootPrefix($type, Schema $schemapos = null)
86+
{
87+
$schema = $schemapos ?: $type->getSchema();
88+
89+
if (isset($this->rootPrefixes[$schema->getTargetNamespace()][$type->getName()])) {
90+
return $this->rootPrefixes[$schema->getTargetNamespace()][$type->getName()];
91+
}
92+
}
93+
94+
public function __construct(NamingStrategy $namingStrategy, LoggerInterface $logger = null)
7395
{
7496
$this->namingStrategy = $namingStrategy;
7597
$this->logger = $logger ?: new NullLogger();

src/DependencyInjection/Configuration.php

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public function getConfigTreeBuilder(): TreeBuilder
7171
->end()
7272
->end()
7373
->end()
74+
->arrayNode('prefixes')->fixXmlConfig('prefix')
75+
->prototype('array')
76+
->prototype('scalar')
77+
->end()
78+
->end()
79+
->end()
7480
->end();
7581

7682
return $treeBuilder;

src/DependencyInjection/Xsd2PhpExtension.php

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function load(array $configs, ContainerBuilder $container)
4949
$converter->addMethodCall('addAliasMapType', [$xml, $type, self::sanitizePhp($php)]);
5050
}
5151
}
52+
foreach ($config['prefixes'] as $target => $data) {
53+
foreach ($data as $element => $prefix) {
54+
$converter->addMethodCall('addRootPrefix', [$target, $element, $prefix]);
55+
}
56+
}
5257
}
5358

5459
if ($config['configs_jms']) {

src/Jms/YamlConverter.php

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
178178
$visitedTypeClass = $this->visitType($type);
179179
}
180180

181+
if ($prefix = $this->getRootPrefix($element, $type->getSchema())) {
182+
$data['xml_root_prefix'] = $prefix;
183+
}
181184

182185
$data['extends'] = $visitedTypeClass;
183186
$this->classes[spl_object_hash($element)]['skip'] = in_array($element->getSchema()->getTargetNamespace(), $this->baseSchemas, true)

tests/Converter/AbstractXsdConverterTest.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function testAliases()
2727
$handlers = $this->converter->getTypeAliases();
2828

2929
$this->assertArrayHasKey('http://www.example.com', $handlers);
30-
$this->assertArrayHasKey('myType', $exmpleHandlers = $handlers['http://www.example.com']);
31-
$this->assertSame($f, $exmpleHandlers['myType']);
30+
$this->assertArrayHasKey('myType', $exampleHandlers = $handlers['http://www.example.com']);
31+
$this->assertSame($f, $exampleHandlers['myType']);
3232
}
3333

3434
public function testDefaultAliases()
@@ -52,4 +52,15 @@ public function testNamespaces()
5252
$this->assertArrayHasKey('http://www.example.com', $namespaces);
5353
$this->assertEquals('some\php\ns', $namespaces['http://www.example.com']);
5454
}
55+
56+
public function testPrefixes()
57+
{
58+
$this->converter->addRootPrefix('http://www.example.com', 'root', 'prefix');
59+
60+
$handlers = $this->converter->getRootPrefixes();
61+
62+
$this->assertArrayHasKey('http://www.example.com', $handlers);
63+
$this->assertArrayHasKey('root', $examplePrefixes = $handlers['http://www.example.com']);
64+
$this->assertSame('prefix', $examplePrefixes['root']);
65+
}
5566
}

tests/Issues/I182/I182Test.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* @package
4+
* @author David Pommer (conlabz GmbH) <[email protected]>
5+
*/
6+
7+
namespace GoetasWebservices\Xsd\XsdToPhp\Tests\Issues\I182;
8+
9+
use GoetasWebservices\XML\XSDReader\SchemaReader;
10+
use GoetasWebservices\Xsd\XsdToPhp\DependencyInjection\Xsd2PhpExtension;
11+
use GoetasWebservices\Xsd\XsdToPhp\Jms\YamlConverter;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\NullLogger;
14+
use Symfony\Component\Config\FileLocator;
15+
use Symfony\Component\Config\Loader\DelegatingLoader;
16+
use Symfony\Component\Config\Loader\LoaderResolver;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21+
22+
class I182Test extends TestCase
23+
{
24+
protected ContainerInterface $container;
25+
protected SchemaReader $reader;
26+
27+
protected YamlConverter $converter;
28+
29+
public function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
$this->container = new ContainerBuilder();
34+
$this->container->registerExtension(
35+
new Xsd2PhpExtension()
36+
);
37+
$this->container->set('logger', new NullLogger());
38+
39+
$this->reader = new SchemaReader();
40+
}
41+
42+
public function testXmlRootPrefix()
43+
{
44+
$this->loadConfigurations(__DIR__ . '/config.yml');
45+
46+
$schema = $this->reader->readFile(__DIR__ . '/data.xsd');
47+
48+
$converter = $this->container->get('goetas_webservices.xsd2php.converter.jms');
49+
50+
$actual = $converter->convert([$schema]);
51+
52+
$expected = [
53+
'Example\\Root\\RootAType' => [
54+
'Example\\Root\\RootAType' => [
55+
'properties' => [
56+
'child' => [
57+
'expose' => true,
58+
'access_type' => 'public_method',
59+
'serialized_name' => 'child',
60+
'accessor' => [
61+
'getter' => 'getChild',
62+
'setter' => 'setChild',
63+
],
64+
'xml_list' => [
65+
'inline' => true,
66+
'entry_name' => 'child',
67+
],
68+
'type' => 'array<Example\\ChildType>',
69+
],
70+
'childRoot' => [
71+
'expose' => true,
72+
'access_type' => 'public_method',
73+
'serialized_name' => 'childRoot',
74+
'xml_element' => [
75+
'namespace' => 'http://www.example.com',
76+
],
77+
'accessor' => [
78+
'getter' => 'getChildRoot',
79+
'setter' => 'setChildRoot',
80+
],
81+
'type' => 'Example\\ChildType',
82+
],
83+
],
84+
],
85+
],
86+
'Example\\Root' => [
87+
'Example\\Root' => [
88+
'xml_root_name' => 'ns-8ece61d2:root',
89+
'xml_root_namespace' => 'http://www.example.com',
90+
'xml_root_prefix' => 'prefix'
91+
],
92+
],
93+
'Example\\ChildType' => [
94+
'Example\\ChildType' => [
95+
'properties' => [
96+
'id' => [
97+
'expose' => true,
98+
'access_type' => 'public_method',
99+
'serialized_name' => 'id',
100+
'accessor' => [
101+
'getter' => 'getId',
102+
'setter' => 'setId',
103+
],
104+
'type' => 'string',
105+
],
106+
],
107+
],
108+
],
109+
];
110+
111+
$this->assertEquals($expected, $actual);
112+
}
113+
114+
private function loadConfigurations($configFile)
115+
{
116+
$locator = new FileLocator('.');
117+
$yaml = new YamlFileLoader($this->container, $locator);
118+
$xml = new XmlFileLoader($this->container, $locator);
119+
120+
$delegatingLoader = new DelegatingLoader(new LoaderResolver([$yaml, $xml]));
121+
$delegatingLoader->load($configFile);
122+
123+
$this->container->compile();
124+
}
125+
}

tests/Issues/I182/config.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
xsd2php:
2+
namespaces:
3+
'http://www.example.com': 'Example'
4+
destinations_php:
5+
'Example': example/php
6+
destinations_jms:
7+
'Example': example/jms
8+
prefixes:
9+
'http://www.example.com':
10+
'root': 'prefix'

tests/Issues/I182/data.xsd

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<xs:schema version="1.0"
3+
targetNamespace="http://www.example.com"
4+
xmlns:tns="http://www.example.com"
5+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
6+
elementFormDefault="unqualified">
7+
8+
<xs:complexType name="childType">
9+
<xs:sequence>
10+
<xs:element name="id" type="xs:string"/>
11+
</xs:sequence>
12+
</xs:complexType>
13+
14+
<xs:element name="root">
15+
<xs:complexType>
16+
<xs:sequence>
17+
<xs:element name="child" type="tns:childType" maxOccurs="unbounded"/>
18+
<xs:element form="qualified" name="childRoot" type="tns:childType"/>
19+
</xs:sequence>
20+
</xs:complexType>
21+
</xs:element>
22+
</xs:schema>

0 commit comments

Comments
 (0)