Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #177

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ Here is an explanation on the meaning of each parameter:

* `xsd2php.known_locations` (optional) override remote location with a local file.

* `xsd2php.strict_types` (optional) enable strict types for setter arguments.

* `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.

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"authors": [
{
"name": "Asmir Mustafic",
"email" : "goetas@gmail.com"
"email": "goetas@gmail.com"
}
],
"keywords": [
Expand All @@ -18,21 +18,21 @@
],
"license": "MIT",
"require": {
"php": ">=7.2|^8.0",
"symfony/console": "^2.7|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/dependency-injection": "^2.2|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/yaml": "^2.2|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/config": "^2.2|^3.0|^4.0|^5.0|^6.0|^7.0",
"php": "^8.0",
"symfony/console": "^6.0|^7.0",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/yaml": "^6.0|^7.0",
"symfony/config": "^6.0|^7.0",
"goetas-webservices/xsd-reader": "^0.3.7 | ^0.4.1",
"doctrine/inflector": "^2.0",
"laminas/laminas-code": "^3.3.2|^4.0",
"laminas/laminas-code": "^4.0",
"psr/log": "^1.0 | ^2.0 | ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0",
"jms/serializer": "^1.9|^2.0|^3.0",
"goetas-webservices/xsd2php-runtime": "^0.2.13@dev",
"symfony/validator": "^2.3.24|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/validator": "^6.0|^7.0",
"dms/phpunit-arraysubset-asserts": "^0.3.1"
},
"autoload": {
Expand Down
114 changes: 52 additions & 62 deletions src/AbstractConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,21 @@ abstract class AbstractConverter
{
use LoggerAwareTrait;

protected $baseSchemas = [
protected array $baseSchemas = [
'http://www.w3.org/2001/XMLSchema',
'http://www.w3.org/XML/1998/namespace',
];

protected $namespaces = [
protected array $namespaces = [
'http://www.w3.org/2001/XMLSchema' => '',
'http://www.w3.org/XML/1998/namespace' => '',
];

/**
* @var \GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy
*/
private $namingStrategy;
private NamingStrategy $namingStrategy;

abstract public function convert(array $schemas);

protected $typeAliases = [];

protected $aliasCache = [];

public function addAliasMap($ns, $name, callable $handler)
{
$this->logger->info("Added map $ns $name");
$this->typeAliases[$ns][$name] = $handler;
}
protected array $typeAliases = [];

public function addAliasMapType($ns, $name, $type)
{
$this->addAliasMap($ns, $name, function () use ($type) {
return $type;
});
}

public function getTypeAliases(): array
{
return $this->typeAliases;
}

public function getTypeAlias($type, ?Schema $schemapos = null)
{
$schema = $schemapos ?: $type->getSchema();

$cid = $schema->getTargetNamespace() . '|' . $type->getName();
if (isset($this->aliasCache[$cid])) {
return $this->aliasCache[$cid];
}
if (isset($this->typeAliases[$schema->getTargetNamespace()][$type->getName()])) {
return $this->aliasCache[$cid] = call_user_func($this->typeAliases[$schema->getTargetNamespace()][$type->getName()], $type);
}
}
protected array $aliasCache = [];

public function __construct(NamingStrategy $namingStrategy, ?LoggerInterface $logger = null)
{
Expand Down Expand Up @@ -186,31 +150,61 @@ public function __construct(NamingStrategy $namingStrategy, ?LoggerInterface $lo
});
}

/**
* @return \GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy
*/
protected function getNamingStrategy()
abstract public function convert(array $schemas);

public function addAliasMap(string $ns, string $name, callable $handler): void
{
$this->logger->info("Added map $ns $name");
$this->typeAliases[$ns][$name] = $handler;
}

public function addAliasMapType(string $ns, string $name, $type): void
{
$this->addAliasMap($ns, $name, function () use ($type) {
return $type;
});
}

public function getTypeAliases(): array
{
return $this->typeAliases;
}

public function getTypeAlias($type, Schema $schemapos = null)
{
$schema = $schemapos ?: $type->getSchema();

$cid = $schema->getTargetNamespace() . '|' . $type->getName();
if (isset($this->aliasCache[$cid])) {
return $this->aliasCache[$cid];
}
if (isset($this->typeAliases[$schema->getTargetNamespace()][$type->getName()])) {
return $this->aliasCache[$cid] =
call_user_func($this->typeAliases[$schema->getTargetNamespace()][$type->getName()], $type);
}

return null;
}

protected function getNamingStrategy(): NamingStrategy
{
return $this->namingStrategy;
}

public function addNamespace($ns, $phpNamespace)
public function addNamespace(string $ns, string $phpNamespace): static
{
$this->logger->info("Added ns mapping $ns, $phpNamespace");
$this->namespaces[$ns] = $phpNamespace;

return $this;
}

protected function cleanName($name)
protected function cleanName(string $name): string
{
return preg_replace('/<.*>/', '', $name);
}

/**
* @return \GoetasWebservices\XML\XSDReader\Schema\Type\Type|null
*/
protected function isArrayType(Type $type)
protected function isArrayType(Type $type): ?Type
{
if ($type instanceof SimpleType) {
if ($type->getList()) {
Expand All @@ -227,31 +221,27 @@ protected function isArrayType(Type $type)
return null;
}

/**
* @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null
*/
protected function isArrayNestedElement(Type $type)
protected function isArrayNestedElement(Type $type): ?ElementSingle
{
if ($type instanceof ComplexType && !$type->getParent() && !$type->getAttributes() && count($type->getElements()) === 1) {
$elements = $type->getElements();

return $this->isArrayElement(reset($elements));
}

return null;
}

/**
* @param mixed $element
*
* @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null
*/
protected function isArrayElement($element)
protected function isArrayElement(mixed $element): ?ElementSingle
{
if ($element instanceof ElementSingle && ($element->getMax() > 1 || $element->getMax() === -1)) {
return $element;
}

return null;
}

public function getNamespaces()
public function getNamespaces(): array
{
return $this->namespaces;
}
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->arrayNode('aliases')->fixXmlConfig('alias')
->prototype('array')
->prototype('scalar')
->end()
->prototype('scalar')
->end()
->end()
->booleanNode('strict_types')
->end()
->end();

return $treeBuilder;
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Xsd2PhpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function load(array $configs, ContainerBuilder $container)
$converter->addMethodCall('setUseCdata', [$config['configs_jms']['xml_cdata']]);
}

if ($config['strict_types'] ?? false) {
$container->getDefinition('goetas_webservices.xsd2php.php.class_generator')
->addMethodCall('enableStrictTypes');
}

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

Expand Down
Loading