1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/old-town-workflow |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\Loader; |
7
|
|
|
|
8
|
|
|
use DOMElement; |
9
|
|
|
use OldTown\Workflow\Exception\NotExistsRequiredAttributeException; |
10
|
|
|
use OldTown\Workflow\Exception\NotExistsRequiredElementException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class XmlUtil |
14
|
|
|
* |
15
|
|
|
* @package OldTown\Workflow\Loader |
16
|
|
|
*/ |
17
|
|
|
abstract class XmlUtil |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Ищет среди потомков элемента $parent, первый элемент с именем тега $childName |
21
|
|
|
* |
22
|
|
|
* @param DOMElement $parent |
23
|
|
|
* @param string $childName |
24
|
|
|
* |
25
|
|
|
* @return DOMElement|null |
26
|
|
|
*/ |
27
|
112 |
|
public static function getChildElement(DOMElement $parent, $childName) |
28
|
|
|
{ |
29
|
112 |
|
$listElements = $parent->getElementsByTagName($childName); |
30
|
112 |
|
if ($listElements->length > 0) { |
31
|
88 |
View Code Duplication |
for ($i = 0; $i < $listElements->length; $i++) { |
|
|
|
|
32
|
88 |
|
$currentItem = $listElements->item($i); |
33
|
88 |
|
if ($parent->isSameNode($currentItem->parentNode)) { |
34
|
88 |
|
return $currentItem; |
35
|
|
|
} |
36
|
17 |
|
} |
37
|
16 |
|
} |
38
|
|
|
|
39
|
105 |
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ищет среди потомков элемента $parent, первый элемент с именем тега $childName. В случае если элемент не найден, |
44
|
|
|
* бросается исключение |
45
|
|
|
* |
46
|
|
|
* @param DOMElement $parent |
47
|
|
|
* @param string $childName |
48
|
|
|
* |
49
|
|
|
* @return DOMElement |
50
|
|
|
*/ |
51
|
5 |
|
public static function getRequiredChildElement(DOMElement $parent, $childName) |
52
|
|
|
{ |
53
|
5 |
|
$element = static::getChildElement($parent, $childName); |
54
|
|
|
|
55
|
5 |
|
if (!$element instanceof DOMElement) { |
56
|
1 |
|
$errMsg = sprintf('Отсутствует элемен %s', $childName); |
57
|
1 |
|
$exception = new NotExistsRequiredElementException($errMsg); |
58
|
|
|
|
59
|
1 |
|
throw $exception; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return $element; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Ищет среди потомков элемента $parent, элементы с именем тега $childName |
69
|
|
|
* |
70
|
|
|
* @param DOMElement $parent |
71
|
|
|
* @param string $childName |
72
|
|
|
* |
73
|
|
|
* @return DOMElement[]|null |
74
|
|
|
*/ |
75
|
124 |
|
public static function getChildElements(DOMElement $parent, $childName) |
76
|
|
|
{ |
77
|
124 |
|
$listElements = $parent->getElementsByTagName($childName); |
78
|
124 |
|
if ($listElements->length > 0) { |
79
|
107 |
|
$list = []; |
80
|
|
|
|
81
|
107 |
View Code Duplication |
for ($i = 0; $i < $listElements->length; $i++) { |
|
|
|
|
82
|
107 |
|
$currentItem = $listElements->item($i); |
83
|
|
|
|
84
|
107 |
|
if (!$parent->isSameNode($currentItem->parentNode)) { |
85
|
7 |
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
107 |
|
$list[] = $currentItem; |
89
|
107 |
|
} |
90
|
|
|
|
91
|
107 |
|
return $list; |
92
|
|
|
} |
93
|
|
|
|
94
|
90 |
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Ищет среди потомков элемента $parent, первый элемент с именем тега $childName и возвращает его текст |
99
|
|
|
* |
100
|
|
|
* @param DOMElement $parent |
101
|
|
|
* @param string $childName |
102
|
|
|
* |
103
|
|
|
* @return null |
104
|
|
|
*/ |
105
|
2 |
|
public static function getChildText(DOMElement $parent, $childName) |
106
|
|
|
{ |
107
|
2 |
|
$child = self::getChildElement($parent, $childName); |
108
|
|
|
|
109
|
2 |
|
if (null === $child) { |
110
|
1 |
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return self::getText($child); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Получает текст из нод потомков |
118
|
|
|
* |
119
|
|
|
* @param DOMElement $node |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
67 |
|
public static function getText(DOMElement $node) |
123
|
|
|
{ |
124
|
67 |
|
$s = ''; |
125
|
67 |
|
for ($i = 0; $i < $node->childNodes->length; $i++) { |
126
|
67 |
|
$child = $node->childNodes->item($i); |
127
|
|
|
|
128
|
67 |
|
switch ($child->nodeType) { |
129
|
67 |
|
case XML_CDATA_SECTION_NODE: |
130
|
67 |
|
case XML_TEXT_NODE: { |
131
|
67 |
|
$s .= $child->nodeValue; |
132
|
|
|
} |
133
|
67 |
|
} |
134
|
67 |
|
} |
135
|
|
|
|
136
|
67 |
|
return $s; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param DOMElement $node |
141
|
|
|
* @param string $attributeName |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
134 |
|
public static function getRequiredAttributeValue(DOMElement $node, $attributeName) |
146
|
|
|
{ |
147
|
134 |
|
$attributeName = (string)$attributeName; |
148
|
|
|
|
149
|
134 |
|
$attribute = $node->attributes->getNamedItem($attributeName); |
150
|
134 |
|
if (!$attribute) { |
151
|
1 |
|
$errMsg = "Отсутствует атрибут {$attributeName} у тега {$node->nodeName}"; |
152
|
1 |
|
$exception = new NotExistsRequiredAttributeException($errMsg); |
153
|
1 |
|
$exception->setRequiredAttributeName($attributeName); |
154
|
|
|
|
155
|
1 |
|
throw $exception; |
156
|
|
|
} |
157
|
|
|
|
158
|
133 |
|
$value = $attribute->nodeValue; |
159
|
|
|
|
160
|
133 |
|
return $value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @fixme Реализовать методу \OldTown\Workflow\Loader\XmlUtil::encode |
166
|
|
|
* |
167
|
|
|
* @param string $string |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
20 |
|
public static function encode($string) |
171
|
|
|
{ |
172
|
20 |
|
return $string; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.