Skip to content

Commit

Permalink
chore: extract XML namespace generation into additional methods of `c…
Browse files Browse the repository at this point in the history
…lass.ApiModelOO`
  • Loading branch information
wazelin committed Aug 11, 2021
1 parent 281c636 commit f21ddc5
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions core/kernel/impl/class.ApiModelOO.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class core_kernel_impl_ApiModelOO extends core_kernel_impl_Api implements core_k
*/
private static $instance = null;

/** @var string[] */
private $namespaces = [];

/** @var int */
private $customNamespacesCounter = 0;

// --- OPERATIONS ---


Expand Down Expand Up @@ -98,22 +104,12 @@ public function getResourceDescriptionXML($uriResource)
$dbWrapper = core_kernel_classes_DbWrapper::singleton();
$subject = $dbWrapper->quote($uriResource);

$namespaceCounter = core_kernel_persistence_smoothsql_SmoothModel::DEFAULT_WRITABLE_MODEL;

$namespaces = [
'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf',
'http://www.w3.org/2000/01/rdf-schema#' => 'rdfs',
ROOT_URL . '#' => "ns$namespaceCounter",
];

try {
$dom = new DOMDocument();
$dom->formatOutput = true;
$root = $dom->createElement('rdf:RDF');

foreach ($namespaces as $namespace => $namespaceId) {
$root->setAttribute("xmlns:$namespaceId", $namespace);
}
$this->addCoreNamespaces($root);
$dom->appendChild($root);

$description = $dom->createElement('rdf:Description');
Expand All @@ -130,15 +126,8 @@ public function getResourceDescriptionXML($uriResource)
}

[$namespace, $property] = explode('#', $predicate, 2);
$namespace = "$namespace#";

if (!isset($namespaces[$namespace])) {
$namespaceId = sprintf('ns%u', ++$namespaceCounter);
$namespaces[$namespace] = $namespaceId;
$root->setAttribute("xmlns:$namespaceId", $namespace);
}

$namespaceId = $namespaces[$namespace];
$namespaceId = $this->addCustomNamespace($root, $namespace);
$nodeName = "$namespaceId:$property";

try {
Expand All @@ -149,7 +138,7 @@ public function getResourceDescriptionXML($uriResource)

if (preg_match("/^http:\/\/(.*)#[a-zA-Z1-9]*/", $object)) {
$node->setAttribute('rdf:resource', $object);
} elseif (!empty($object) && !is_null($object)) {
} elseif (!empty($object)) {

/**
* Replace the CDATA section inside XML fields by a replacement tag:
Expand Down Expand Up @@ -423,4 +412,41 @@ private function createClassCollection(string $debug = ''): core_kernel_classes_
{
return new core_kernel_classes_ContainerCollection(new core_kernel_classes_Container(), $debug);
}

private function addCoreNamespaces(DOMElement $root): void
{
$this->namespaces = [
'http://www.w3.org/1999/02/22-rdf-syntax-ns' => 'rdf',
'http://www.w3.org/2000/01/rdf-schema' => 'rdfs',
];

foreach ($this->namespaces as $namespace => $namespaceId) {
$this->addNamespace($root, $namespaceId, $namespace);
}

$this->customNamespacesCounter = 0;
$this->addCustomNamespace($root, ROOT_URL);
}

/**
* @param DOMElement $root
* @param string $namespace
*
* @return string|null A namespace ID
*/
private function addCustomNamespace(DOMElement $root, string $namespace): string
{
if (!isset($this->namespaces[$namespace])) {
$namespaceId = sprintf('ns%u', ++$this->customNamespacesCounter);
$this->namespaces[$namespace] = $namespaceId;
$this->addNamespace($root, $namespaceId, $namespace);
}

return $this->namespaces[$namespace];
}

private function addNamespace(DOMElement $root, string $namespaceId, string $namespace): void
{
$root->setAttribute("xmlns:$namespaceId", "$namespace#");
}
}

0 comments on commit f21ddc5

Please sign in to comment.