depage-xmldb
v2.3.1
XML documents in a relational database
Toggle main menu visibility
Loading...
Searching...
No Matches
NodeContent.php
Go to the documentation of this file.
1
<?php
2
12
13
namespace
Depage\XmlDb\Helpers;
14
15
use Depage\XmlDb\Exceptions\XmlDbException;
16
use Depage\XmlDb\XmlNs;
17
32
class
NodeContent
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 : */
Depage\XmlDb\Exceptions\XmlDbException
Definition
XmlDbException.php:10
Depage\XmlDb\Helpers\NodeContent
Definition
NodeContent.php:33
Depage\XmlDb\Helpers\NodeContent\attributeString
static attributeString(array $attributes, XmlNs $dbNs, string $idAttribute)
builds the attribute string a node's value column holds from the attributes of a node,...
Definition
NodeContent.php:85
Depage\XmlDb\Helpers\NodeContent\hash
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...
Definition
NodeContent.php:113
Depage\XmlDb\Helpers\NodeContent\dbIdOf
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)
Definition
NodeContent.php:38
Depage\XmlDb\Helpers\NodeContent\fromDomNode
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...
Definition
NodeContent.php:49
Depage\XmlDb\XmlNs
A namespace prefix and the URI it is bound to.
Definition
XmlNs.php:9
Helpers
NodeContent.php
Generated on
for depage-xmldb by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net