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

Wildcard aliases #46

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions src/AbstractConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function getTypeAlias($type, Schema $schemapos = null)
if (isset($this->typeAliases[$schema->getTargetNamespace()][$type->getName()])) {
return $this->aliasCache[$cid] = call_user_func($this->typeAliases[$schema->getTargetNamespace()][$type->getName()], $type);
}

if (!empty($this->typeAliases[$schema->getTargetNamespace()])) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be moved to a private method and documented

foreach ($this->typeAliases[$schema->getTargetNamespace()] as $k => $f){
if (strpos($k, '*')!== false && preg_match("/^".str_replace("\*", ".*", preg_quote($k, "/"))."$/", $type->getName())) {
return $this->aliasCache[$cid] = call_user_func($f, $type);
}
}
}
}

public function __construct(NamingStrategy $namingStrategy, LoggerInterface $logger = null)
Expand Down
20 changes: 17 additions & 3 deletions src/Jms/YamlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,17 @@ private function visitTypeBase(&$class, &$data, Type $type, $name)
public function &visitElementDef(Schema $schema, ElementDef $element)
{
if (!isset($this->classes[spl_object_hash($element)])) {
$className = $this->findPHPNamespace($element) . "\\" . $this->getNamingStrategy()->getItemName($element);

$class = array();
$data = array();

if (($alias = $this->getTypeAlias($element))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be moved to a private method as well

$className = $alias;
$this->classes[spl_object_hash($element)]["skip"] = true;
}else{
$className = $this->findPHPNamespace($element) . "\\" . $this->getNamingStrategy()->getItemName($element);
}

$ns = $className;
$class[$ns] = &$data;
$data["xml_root_name"] = $element->getName();
Expand All @@ -147,7 +155,7 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
$data["xml_root_namespace"] = $schema->getTargetNamespace();

if (!$schema->getElementsQualification() && !($element instanceof Element && $element->isQualified())) {
$data["xml_root_name"] = "ns-" . substr(sha1($data["xml_root_namespace"]), 0, 8) . ":" . $data["xml_root_name"];
$data["xml_root_name"] = $this->getNsPrefix($data["xml_root_namespace"]) . ":" . $data["xml_root_name"];
}
}
$this->classes[spl_object_hash($element)]["class"] = &$class;
Expand All @@ -158,10 +166,16 @@ public function &visitElementDef(Schema $schema, ElementDef $element)
$this->handleClassExtension($class, $data, $element->getType(), $element->getName());
}
}
$this->classes[spl_object_hash($element)]["skip"] = in_array($element->getSchema()->getTargetNamespace(), $this->baseSchemas, true);
$this->classes[spl_object_hash($element)]["skip"] = $this->classes[spl_object_hash($element)]["skip"] || in_array($element->getSchema()->getTargetNamespace(), $this->baseSchemas, true);

return $this->classes[spl_object_hash($element)]["class"];
}

private function getNsPrefix($ns)
{
return "ns-" . substr(sha1($ns), 0, 8);
}

private function findPHPNamespace(SchemaItem $item)
{
$schema = $item->getSchema();
Expand Down
18 changes: 9 additions & 9 deletions src/Php/PhpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ public function visitElementDef(ElementDef $element, $skip = false)
$class->setName($this->getNamingStrategy()->getItemName($element));
$class->setDoc($element->getDoc());

if ($alias = $this->getTypeAlias($element)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here - private method to be added

$class->setName($alias);
$class->setNamespace(null);
$this->classes[spl_object_hash($element)]["class"] = $class;
$this->classes[spl_object_hash($element)]["skip"] = true;
$this->skipByType[spl_object_hash($element)] = true;
return $class;
}

if (!isset($this->namespaces[$schema->getTargetNamespace()])) {
throw new Exception(sprintf("Can't find a PHP namespace to '%s' namespace", $schema->getTargetNamespace()));
}
Expand All @@ -167,15 +176,6 @@ public function visitElementDef(ElementDef $element, $skip = false)
if (!$element->getType()->getName()) {
$this->visitTypeBase($class, $element->getType());
} else {

if ($alias = $this->getTypeAlias($element)) {
$class->setName($alias);
$class->setNamespace(null);
$this->classes[spl_object_hash($element)]["skip"] = true;
$this->skipByType[spl_object_hash($element)] = true;
return $class;
}

$this->handleClassExtension($class, $element->getType());
}
}
Expand Down