depage-xmldb v2.3.1
XML documents in a relational database
Loading...
Searching...
No Matches
NodeContent.php
Go to the documentation of this file.
1<?php
2
12
13namespace Depage\XmlDb\Helpers;
14
15use Depage\XmlDb\Exceptions\XmlDbException;
16use Depage\XmlDb\XmlNs;
17
33{
38 public static function dbIdOf($node, XmlNs $dbNs, string $idAttribute)
39 {
40 return $node->nodeType == XML_ELEMENT_NODE
41 ? $node->getAttributeNS($dbNs->uri, $idAttribute)
42 : null;
43 }
44
49 public static function fromDomNode($node, XmlNs $dbNs, string $idAttribute): array
50 {
51 if ($node->nodeType == XML_ELEMENT_NODE) {
52 if ($node->prefix != '') {
53 $nodeName = $node->prefix . ':' . $node->localName;
54 } else {
55 $nodeName = $node->localName;
56 }
57
58 $attributes = [];
59 foreach ($node->attributes as $attrib) {
60 $attribNs = ($attrib->prefix == '') ? '' : $attrib->prefix . ':';
61
62 $attributes[$attribNs . $attrib->localName] = $attrib->value;
63 }
64
65 return ['ELEMENT_NODE', $nodeName, self::attributeString($attributes, $dbNs, $idAttribute)];
66 } elseif ($node->nodeType == XML_TEXT_NODE) {
67 return ['TEXT_NODE', null, $node->textContent];
68 } elseif ($node->nodeType == XML_COMMENT_NODE) {
69 return ['COMMENT_NODE', null, $node->textContent];
70 } elseif ($node->nodeType == XML_PI_NODE) {
71 return ['PI_NODE', $node->target, $node->textContent];
72 } elseif ($node->nodeType == XML_ENTITY_REF_NODE) {
73 return ['ENTITY_REF_NODE', null, $node->nodeName];
74 }
75
76 throw new XmlDbException('Unknown DOM node type: "' . $node->nodeType . '".');
77 }
78
85 public static function attributeString(array $attributes, XmlNs $dbNs, string $idAttribute): string
86 {
87 $attrStr = '';
88 $autogeneratedAttr = [
89 $dbNs->ns . ':' . $idAttribute,
90 $dbNs->ns . ':released',
91 $dbNs->ns . ':lastchange',
92 $dbNs->ns . ':lastchangeUid',
93 ];
94 ksort($attributes);
95 foreach ($attributes as $name => $value) {
96 if (!in_array($name, $autogeneratedAttr)) {
97 $attrStr .= "$name=\"" . htmlspecialchars($value) . "\" ";
98 }
99 }
100
101 return $attrStr;
102 }
103
113 public static function hash($type, $name, $value, array $childHashes): string
114 {
115 return hash('sha256', 'v1|' . $type . '|' . $name . '|' . $value . '|' . implode('|', $childHashes));
116 }
117}
118
119/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
static attributeString(array $attributes, XmlNs $dbNs, string $idAttribute)
builds the attribute string a node's value column holds from the attributes of a node,...
static hash($type, $name, $value, array $childHashes)
canonical hash over one node row and the hashes of its children; the "v1" prefix pins the canonicaliz...
static dbIdOf($node, XmlNs $dbNs, string $idAttribute)
reads the database id out of the db:id attribute of a node ('' if the node does not carry one)
static fromDomNode($node, XmlNs $dbNs, string $idAttribute)
returns the (type, name, value) tupel a DOM node is saved as, in exactly the same way saveNodeToDb st...
A namespace prefix and the URI it is bound to.
Definition XmlNs.php:9