Total Complexity | 10 |
Total Lines | 104 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
19 | trait DeprecatedMethods |
||
20 | { |
||
21 | use MagicMethods |
||
22 | { |
||
23 | MagicMethods::__call as magicMethodsCall; |
||
24 | } |
||
25 | |||
26 | public function __call(string $name, array $arguments) |
||
27 | { |
||
28 | if (preg_match('(^insertAdjacent(?:Element|Text)$)i', $name)) |
||
29 | { |
||
30 | $methodName = '_' . $name; |
||
31 | |||
32 | return $this->$methodName(...$arguments); |
||
33 | } |
||
34 | if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m)) |
||
35 | { |
||
36 | $methodName = [ |
||
37 | 'ap' => 'append', |
||
38 | 'pre' => 'prepend', |
||
39 | 'apsibling' => 'after', |
||
40 | 'presibling' => 'before' |
||
41 | ][strtolower($m[1] . $m[2])]; |
||
42 | |||
43 | return $this->$methodName(...$arguments); |
||
44 | } |
||
45 | if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m)) |
||
46 | { |
||
47 | $name = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2]; |
||
48 | } |
||
49 | |||
50 | return $this->magicMethodsCall($name, $arguments); |
||
1 ignored issue
–
show
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @deprecated |
||
55 | */ |
||
56 | public function insertAdjacentXML(string $where, string $xml): void |
||
57 | { |
||
58 | $fragment = $this->ownerDocument->createDocumentFragment(); |
||
59 | $fragment->appendXML($this->addMissingNamespaceDeclarations($xml)); |
||
60 | |||
61 | $this->insertAdjacentNode($where, $fragment); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Add namespace declarations that may be missing in given XML |
||
66 | * |
||
67 | * @param string $xml Original XML |
||
68 | * @return string Modified XML |
||
69 | */ |
||
70 | protected function addMissingNamespaceDeclarations(string $xml): string |
||
71 | { |
||
72 | preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m); |
||
73 | $prefixes = array_flip($m[0]); |
||
74 | |||
75 | return preg_replace_callback( |
||
76 | '(<([-\\w]++):[^>]*?\\K\\s*/?>)', |
||
77 | function ($m) use ($prefixes) |
||
78 | { |
||
79 | $return = $m[0]; |
||
80 | $prefix = $m[1]; |
||
81 | if (!isset($prefixes[$prefix])) |
||
82 | { |
||
83 | $nsURI = $this->lookupNamespaceURI($prefix); |
||
1 ignored issue
–
show
|
|||
84 | $return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_XML1) . '"' . $return; |
||
85 | } |
||
86 | |||
87 | return $return; |
||
88 | }, |
||
89 | $xml |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Insert given node relative to this element's position |
||
95 | * |
||
96 | * @param string $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend' |
||
97 | * @param DOMNode $node |
||
98 | * @return void |
||
99 | */ |
||
100 | protected function insertAdjacentNode(string $where, DOMNode $node): void |
||
101 | { |
||
102 | match (strtolower($where)) |
||
103 | { |
||
104 | 'beforebegin' => $this->parentNode?->insertBefore($node, $this), |
||
105 | 'beforeend' => $this->appendChild($node), |
||
1 ignored issue
–
show
|
|||
106 | 'afterend' => $this->parentNode?->insertBefore($node, $this->nextSibling), |
||
107 | 'afterbegin' => $this->insertBefore($node, $this->firstChild), |
||
1 ignored issue
–
show
|
|||
108 | default => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR) |
||
109 | }; |
||
110 | } |
||
111 | |||
112 | private function _insertAdjacentElement(string $where, self $element): self |
||
113 | { |
||
114 | $this->insertAdjacentNode($where, $element); |
||
115 | |||
116 | return $element; |
||
117 | } |
||
118 | |||
119 | private function _insertAdjacentText(string $where, string $text): void |
||
123 | } |
||
124 | } |