Skip to content

Commit

Permalink
Added XML namespace prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Oct 3, 2022
1 parent df731b2 commit b20aa0a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ xsd2php:
# "http://www.example.org/test/somefile.xsd": somefile.xsd
# known_namespace_locations: # optional
# "urn:veloconnect:catalog-1.1": xsd/catalog-1.1.xsd
# xml_namespaces:
# 'my-prefix': 'https://example.com/my-namespace'
# configs_jms: #optional
# xml_cdata: false # Disables CDATA
```
Expand Down Expand Up @@ -121,6 +123,9 @@ Here is an explanation on the meaning of each parameter:
* `xsd2php.known_namespace_locations` (optional) Specify schema location by namespace.
This can be used to read schemas which import namespaces but do not specify schemaLocation attributes.

* `xsd2php.xml_namespaces` (optional) Specify XML namespace prefixes. If a prefix is defined for at namespace, then the
prefix will *always* be used in the XML element name.

* `xsd2php.configs_jms.xml_cdata` (optional) Specify if CDATA should be used or not in serialization.

## Generate PHP classes and JMS metadata info
Expand Down
12 changes: 12 additions & 0 deletions src/AbstractConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ abstract class AbstractConverter
'http://www.w3.org/XML/1998/namespace' => '',
];

protected $xmlNamespaces = [
// namespace => prefix
];

/**
* @var \GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy
*/
Expand Down Expand Up @@ -202,6 +206,14 @@ public function addNamespace($ns, $phpNamespace)
return $this;
}

public function addXMLNamespace($prefix, $namespace)
{
$this->logger->info("Added XML namespace $prefix:$namespace");
$this->xmlNamespaces[$namespace] = $prefix;

return $this;
}

protected function cleanName($name)
{
return preg_replace('/<.*>/', '', $name);
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('xml_namespaces')
->cannotBeEmpty()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->end();

return $treeBuilder;
Expand Down
8 changes: 7 additions & 1 deletion src/DependencyInjection/Xsd2PhpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($config['namespaces'] as $xml => $php) {
$converter->addMethodCall('addNamespace', [$xml, self::sanitizePhp($php)]);
}
foreach ($config['xml_namespaces'] as $prefix => $namespace) {
$converter->addMethodCall('addXMLNamespace', [$prefix, $namespace]);
}
foreach ($config['aliases'] as $xml => $data) {
foreach ($data as $type => $php) {
$converter->addMethodCall('addAliasMapType', [$xml, $type, self::sanitizePhp($php)]);
}
}
}

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

Expand Down
10 changes: 10 additions & 0 deletions src/Jms/YamlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
$data = [];
$ns = $className;
$class[$ns] = &$data;

if (!empty($this->xmlNamespaces)) {
$data['xml_namespaces'] = array_flip($this->xmlNamespaces);
}

$data['xml_root_name'] = $element->getName();

if ($schema->getTargetNamespace()) {
Expand All @@ -168,6 +173,11 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
if (!$schema->getElementsQualification() && !($element instanceof Element && $element->isQualified())) {
$data['xml_root_name'] = 'ns-' . substr(sha1($data['xml_root_namespace']), 0, 8) . ':' . $element->getName();
}

// Force XML namespace prefix if prefix is defined.
if (isset($this->xmlNamespaces[$data['xml_root_namespace']])) {
$data['xml_root_name'] = $this->xmlNamespaces[$data['xml_root_namespace']] . ':' . $element->getName();
}
}
$this->classes[spl_object_hash($element)]['class'] = &$class;

Expand Down

0 comments on commit b20aa0a

Please sign in to comment.