1 | <?php |
||||||
2 | |||||||
3 | namespace Kaliop\eZLoremIpsumBundle\Faker\Provider; |
||||||
4 | |||||||
5 | /** |
||||||
6 | * NB: A very crude implementation for now |
||||||
7 | * @see https://github.com/ezsystems/ezpublish-kernel/tree/master/eZ/Publish/Core/FieldType/Tests/RichText/Converter/Xslt/_fixtures/ezxml |
||||||
8 | */ |
||||||
9 | class XmlText extends Base |
||||||
10 | { |
||||||
11 | const P_TAG = "paragraph"; |
||||||
12 | const A_TAG = "link"; |
||||||
13 | const E_TAG = "embed"; |
||||||
14 | const TABLE_TAG = "table"; |
||||||
15 | const TR_TAG = "tr"; |
||||||
16 | const TD_TAG = "td"; |
||||||
17 | const UL_TAG = "ul"; |
||||||
18 | const OL_TAG = "ol"; |
||||||
19 | const LI_TAG = "li"; |
||||||
20 | const H_TAG = "header"; |
||||||
21 | const B_TAG = "strong"; |
||||||
22 | const I_TAG = "emphasize"; |
||||||
23 | const CUSTOM_TAG = "custom"; |
||||||
24 | |||||||
25 | /** |
||||||
26 | * @param integer $maxDepth |
||||||
27 | * @param integer $maxWidth |
||||||
28 | * |
||||||
29 | * @return string |
||||||
30 | */ |
||||||
31 | public function randomXmlText($maxDepth = 4, $maxWidth = 4) |
||||||
32 | { |
||||||
33 | $document = new \DOMDocument(); |
||||||
34 | |||||||
35 | $body = $document->createElement("section"); |
||||||
36 | $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xhtml', 'http://ez.no/namespaces/ezpublish3/xhtml/'); |
||||||
37 | $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:image', 'http://ez.no/namespaces/ezpublish3/image/'); |
||||||
38 | $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:custom', 'http://ez.no/namespaces/ezpublish3/custom/'); |
||||||
39 | $this->addRandomSubTree($body, $maxDepth, $maxWidth); |
||||||
40 | |||||||
41 | $document->appendChild($body); |
||||||
42 | |||||||
43 | $out = $document->saveXML(); |
||||||
44 | |||||||
45 | return $out; |
||||||
46 | } |
||||||
47 | |||||||
48 | private function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth) |
||||||
49 | { |
||||||
50 | $maxDepth--; |
||||||
51 | if ($maxDepth <= 0) { |
||||||
52 | return $root; |
||||||
53 | } |
||||||
54 | |||||||
55 | $siblings = mt_rand(1, $maxWidth); |
||||||
56 | for ($i = 0; $i < $siblings; $i++) { |
||||||
57 | if ($maxDepth == 1) { |
||||||
58 | $this->addRandomLeaf($root); |
||||||
59 | } else { |
||||||
60 | $sibling = $root->ownerDocument->createElement("section"); |
||||||
0 ignored issues
–
show
|
|||||||
61 | $root->appendChild($sibling); |
||||||
62 | $this->addRandomSubTree($sibling, mt_rand(0, $maxDepth), $maxWidth); |
||||||
63 | } |
||||||
64 | }; |
||||||
65 | return $root; |
||||||
66 | } |
||||||
67 | |||||||
68 | private function addRandomLeaf(\DOMElement $node) |
||||||
69 | { |
||||||
70 | $rand = mt_rand(1, 10); |
||||||
71 | switch($rand){ |
||||||
72 | case 1: |
||||||
73 | $this->addRandomE($node); |
||||||
74 | break; |
||||||
75 | case 2: |
||||||
76 | $this->addRandomA($node); |
||||||
77 | break; |
||||||
78 | case 3: |
||||||
79 | $this->addRandomOL($node); |
||||||
80 | break; |
||||||
81 | case 4: |
||||||
82 | $this->addRandomUL($node); |
||||||
83 | break; |
||||||
84 | case 5: |
||||||
85 | $this->addRandomH($node); |
||||||
86 | break; |
||||||
87 | case 6: |
||||||
88 | $this->addRandomB($node); |
||||||
89 | break; |
||||||
90 | case 7: |
||||||
91 | $this->addRandomI($node); |
||||||
92 | break; |
||||||
93 | case 8: |
||||||
94 | $this->addRandomTable($node); |
||||||
95 | break; |
||||||
96 | case 9: |
||||||
97 | $this->addRandomU($node); |
||||||
98 | break; |
||||||
99 | default: |
||||||
100 | $this->addRandomP($node); |
||||||
101 | break; |
||||||
102 | } |
||||||
103 | } |
||||||
104 | |||||||
105 | private function addRandomE(\DOMElement $element) |
||||||
106 | { |
||||||
107 | $p = $element->ownerDocument->createElement(static::P_TAG); |
||||||
0 ignored issues
–
show
The method
createElement() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
108 | $node = $element->ownerDocument->createElement(static::E_TAG); |
||||||
109 | $node->setAttribute("view", "embed"); |
||||||
110 | $node->setAttribute("size", "medium"); |
||||||
111 | /// @todo improve: generation of a radmon object id |
||||||
112 | $node->setAttribute("object_id", mt_rand(1, 100000)); |
||||||
113 | $p->appendChild($node); |
||||||
114 | $element->appendChild($p); |
||||||
115 | } |
||||||
116 | |||||||
117 | /** |
||||||
118 | * @todo add random align |
||||||
119 | * @param \DOMElement $element |
||||||
120 | * @param int $maxLength |
||||||
121 | */ |
||||||
122 | private function addRandomP(\DOMElement $element, $maxLength = 50) |
||||||
123 | { |
||||||
124 | $p = $element->ownerDocument->createElement(static::P_TAG); |
||||||
125 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
126 | // left-aligned paragraphs have double frequency |
||||||
127 | switch (mt_rand(1, 4)) { |
||||||
128 | case 1: |
||||||
129 | $p->setAttribute("align", "right"); |
||||||
130 | break; |
||||||
131 | case 2: |
||||||
132 | $p->setAttribute("align", "center"); |
||||||
133 | break; |
||||||
134 | } |
||||||
135 | $p->appendChild($text); |
||||||
136 | $element->appendChild($p); |
||||||
137 | } |
||||||
138 | |||||||
139 | private function addRandomA(\DOMElement $element, $maxLength = 10) |
||||||
140 | { |
||||||
141 | $p = $element->ownerDocument->createElement(static::P_TAG); |
||||||
0 ignored issues
–
show
|
|||||||
142 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
143 | $node = $element->ownerDocument->createElement(static::A_TAG); |
||||||
144 | $node->setAttribute("url", $this->getUrl()); |
||||||
145 | $node->appendChild($text); |
||||||
146 | $this->wrapInParagraph($node, $element); |
||||||
147 | } |
||||||
148 | |||||||
149 | private function addRandomH(\DOMElement $element, $maxLength = 10) |
||||||
150 | { |
||||||
151 | $h = static::H_TAG; |
||||||
152 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
153 | $node = $element->ownerDocument->createElement($h); |
||||||
154 | $node->appendChild($text); |
||||||
155 | $element->appendChild($node); |
||||||
156 | |||||||
157 | } |
||||||
158 | |||||||
159 | private function addRandomB(\DOMElement $element, $maxLength = 10) |
||||||
160 | { |
||||||
161 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
162 | $node = $element->ownerDocument->createElement(static::B_TAG); |
||||||
163 | $node->appendChild($text); |
||||||
164 | $this->wrapInParagraph($node, $element); |
||||||
165 | } |
||||||
166 | |||||||
167 | private function addRandomU(\DOMElement $element, $maxLength = 10) |
||||||
168 | { |
||||||
169 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
170 | $node = $element->ownerDocument->createElement(static::CUSTOM_TAG); |
||||||
171 | $node->setAttribute('name', 'underline'); |
||||||
172 | $node->appendChild($text); |
||||||
173 | $this->wrapInParagraph($node, $element); |
||||||
174 | } |
||||||
175 | |||||||
176 | private function addRandomI(\DOMElement $element, $maxLength = 10) |
||||||
177 | { |
||||||
178 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
||||||
179 | $node = $element->ownerDocument->createElement(static::I_TAG); |
||||||
180 | $node->appendChild($text); |
||||||
181 | $this->wrapInParagraph($node, $element); |
||||||
182 | } |
||||||
183 | |||||||
184 | private function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxLength = 10) |
||||||
185 | { |
||||||
186 | $rows = mt_rand(1, $maxRows); |
||||||
187 | $cols = mt_rand(1, $maxCols); |
||||||
188 | |||||||
189 | $table = $element->ownerDocument->createElement(static::TABLE_TAG); |
||||||
190 | |||||||
191 | for ($i = 0; $i < $rows; $i++) { |
||||||
192 | $tr = $element->ownerDocument->createElement(static::TR_TAG); |
||||||
193 | $table->appendChild($tr); |
||||||
194 | for ($j = 0; $j < $cols; $j++) { |
||||||
195 | $th = $element->ownerDocument->createElement(static::TD_TAG); |
||||||
196 | $th->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
||||||
197 | $tr->appendChild($th); |
||||||
198 | } |
||||||
199 | } |
||||||
200 | $this->wrapInParagraph($table, $element); |
||||||
201 | } |
||||||
202 | |||||||
203 | private function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4) |
||||||
204 | { |
||||||
205 | $num = mt_rand(1, $maxItems); |
||||||
206 | $ul = $element->ownerDocument->createElement(static::UL_TAG); |
||||||
207 | for ($i = 0; $i < $num; $i++) { |
||||||
208 | $li = $element->ownerDocument->createElement(static::LI_TAG); |
||||||
209 | $lip = $element->ownerDocument->createElement(static::P_TAG); |
||||||
210 | $lip->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
||||||
211 | $li->appendChild($lip); |
||||||
212 | $ul->appendChild($li); |
||||||
213 | } |
||||||
214 | $this->wrapInParagraph($ul, $element); |
||||||
215 | } |
||||||
216 | |||||||
217 | private function addRandomOL(\DOMElement $element, $maxItems = 11, $maxLength = 4) |
||||||
218 | { |
||||||
219 | $num = mt_rand(1, $maxItems); |
||||||
220 | $ul = $element->ownerDocument->createElement(static::OL_TAG); |
||||||
221 | for ($i = 0; $i < $num; $i++) { |
||||||
222 | $li = $element->ownerDocument->createElement(static::LI_TAG); |
||||||
223 | $lip = $element->ownerDocument->createElement(static::P_TAG); |
||||||
224 | $lip->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
||||||
225 | $li->appendChild($lip); |
||||||
226 | $ul->appendChild($li); |
||||||
227 | } |
||||||
228 | $this->wrapInParagraph($ul, $element); |
||||||
229 | } |
||||||
230 | |||||||
231 | protected function wrapInParagraph(\DOMElement $element, \DOMElement $parent, $maxLength = 10) |
||||||
232 | { |
||||||
233 | $p = $element->ownerDocument->createElement(static::P_TAG); |
||||||
234 | $chance = mt_rand(1, 4); |
||||||
235 | if ($chance == 1 || $chance == 4) { |
||||||
236 | $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)) . ' '); |
||||||
237 | $p->appendChild($text); |
||||||
238 | } |
||||||
239 | $p->appendChild($element); |
||||||
240 | if ($chance == 2 || $chance == 4) { |
||||||
241 | $text = $element->ownerDocument->createTextNode(' ' . $this->getSentence(mt_rand(1, $maxLength))); |
||||||
242 | $p->appendChild($text); |
||||||
243 | } |
||||||
244 | $parent->appendChild($p); |
||||||
245 | } |
||||||
246 | |||||||
247 | protected function getSentence($nbWords = 6, $variableNbWords = true) |
||||||
248 | { |
||||||
249 | return $this->generator->sentence($nbWords, $variableNbWords); |
||||||
250 | } |
||||||
251 | |||||||
252 | protected function getUrl() |
||||||
253 | { |
||||||
254 | return $this->generator->url; |
||||||
255 | } |
||||||
256 | } |
||||||
257 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.