1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use SixtyNine\Cloud\Color\ColorGeneratorInterface; |
7
|
|
|
use SixtyNine\Cloud\Filters\Filters; |
8
|
|
|
use JMS\Serializer\Annotation as JMS; |
9
|
|
|
|
10
|
|
|
class WordsList |
11
|
|
|
{ |
12
|
|
|
const SORT_ALPHA = 'text'; |
13
|
|
|
const SORT_COUNT = 'count'; |
14
|
|
|
const SORT_ANGLE = 'angle'; |
15
|
|
|
|
16
|
|
|
const SORT_ASC = 'asc'; |
17
|
|
|
const SORT_DESC = 'desc'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* @JMS\Type("string") |
22
|
|
|
*/ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ArrayCollection |
27
|
|
|
* @JMS\Type("ArrayCollection<SixtyNine\Cloud\Model\Word>") |
28
|
|
|
*/ |
29
|
|
|
protected $words; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
* @JMS\Exclude() |
34
|
|
|
*/ |
35
|
|
|
protected $allowedSortBy = array(self::SORT_ALPHA, self::SORT_ANGLE, self::SORT_COUNT); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
* @JMS\Exclude() |
40
|
|
|
*/ |
41
|
|
|
protected $allowedSortOrder = array(self::SORT_ASC, self::SORT_DESC); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
*/ |
46
|
7 |
|
public function __construct() |
47
|
|
|
{ |
48
|
7 |
|
$this->words = new ArrayCollection(); |
49
|
7 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
7 |
|
public function setName($name) |
56
|
|
|
{ |
57
|
7 |
|
$this->name = $name; |
58
|
7 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
3 |
|
public function getName() |
65
|
|
|
{ |
66
|
3 |
|
return $this->name; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if a Word with the same text already exists in the Cloud |
71
|
|
|
* @param string $text |
72
|
|
|
* @return null|Word |
73
|
|
|
*/ |
74
|
6 |
|
public function getWordForText($text) |
75
|
|
|
{ |
76
|
|
|
/** @var Word $word */ |
77
|
6 |
|
foreach ($this->words as $word) { |
78
|
6 |
|
if ($word->getText() === $text) { |
79
|
6 |
|
return $word; |
80
|
|
|
} |
81
|
|
|
} |
82
|
6 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Word $word |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
6 |
|
public function addWord(Word $word) |
90
|
|
|
{ |
91
|
6 |
|
if (!$this->words->contains($word)) { |
92
|
6 |
|
$this->words->add($word); |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Word $word |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function removeWord(Word $word) |
103
|
|
|
{ |
104
|
|
|
$this->words->remove($word); |
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return ArrayCollection |
110
|
|
|
*/ |
111
|
6 |
|
public function getWords() |
112
|
|
|
{ |
113
|
6 |
|
return $this->words; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return ArrayCollection |
118
|
|
|
*/ |
119
|
3 |
|
public function getWordsOrdered() |
120
|
|
|
{ |
121
|
3 |
|
$iterator = $this->words->getIterator(); |
122
|
|
|
$iterator->uasort(function ($a, $b) { |
123
|
3 |
|
return ($a->getPosition() < $b->getPosition()) ? -1 : 1; |
124
|
3 |
|
}); |
125
|
3 |
|
return new ArrayCollection(iterator_to_array($iterator)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $sortBy |
130
|
|
|
* @param string $sortOrder |
131
|
|
|
* @throws \InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
public function sortWords($sortBy, $sortOrder) |
134
|
|
|
{ |
135
|
|
|
if (!in_array($sortBy, $this->allowedSortBy)) { |
136
|
|
|
throw new \InvalidArgumentException('Invalid sort by: ' . $sortBy); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!in_array($sortOrder, $this->allowedSortOrder)) { |
140
|
|
|
throw new \InvalidArgumentException('Invalid sort order: ' - $sortOrder); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$sorter = function ($a, $b) use ($sortBy, $sortOrder) { |
144
|
|
|
$method = 'get' . ucfirst($sortBy); |
145
|
|
|
|
146
|
|
|
$attr1 = $a->$method(); |
147
|
|
|
$attr2 = $b->$method(); |
148
|
|
|
|
149
|
|
|
if ($sortOrder === self::SORT_ASC) { |
150
|
|
|
return $attr1 < $attr2; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $attr1 > $attr2; |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$iterator = $this->words->getIterator(); |
158
|
|
|
$iterator->uasort($sorter); |
159
|
|
|
|
160
|
|
|
$index = 0; |
161
|
|
|
foreach ($iterator as $word) { |
162
|
|
|
$word->setPosition($index); |
163
|
|
|
$index++; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return int |
169
|
|
|
* @JMS\VirtualProperty |
170
|
|
|
* @JMS\SerializedName("max-count") |
171
|
|
|
*/ |
172
|
4 |
|
public function getWordsMaxCount() |
173
|
|
|
{ |
174
|
4 |
|
$max = 0; |
175
|
|
|
/** @var Word $word */ |
176
|
4 |
|
foreach ($this->words as $word) { |
177
|
4 |
|
if ($word->getCount() > $max) { |
178
|
4 |
|
$max = $word->getCount(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
4 |
|
return $max; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return int |
186
|
|
|
* @JMS\VirtualProperty |
187
|
|
|
* @JMS\SerializedName("count") |
188
|
|
|
*/ |
189
|
1 |
|
public function getWordsCount() |
190
|
|
|
{ |
191
|
1 |
|
return $this->words->count(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $words |
196
|
|
|
* @param Filters $filters |
197
|
|
|
* @param int $maxWords |
198
|
|
|
*/ |
199
|
6 |
|
public function importWords($words, Filters $filters = null, $maxWords = 100) |
200
|
|
|
{ |
201
|
6 |
|
$array = preg_split("/[\n\r\t ]+/", $words); |
202
|
|
|
|
203
|
6 |
|
foreach (array_slice($array, 0, $maxWords) as $word) { |
204
|
6 |
|
$this->importWord($word, $filters); |
205
|
|
|
} |
206
|
6 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $word |
210
|
|
|
* @param Filters $filters |
211
|
|
|
*/ |
212
|
6 |
|
public function importWord($word, Filters $filters = null) |
213
|
|
|
{ |
214
|
6 |
|
if ($filters) { |
215
|
6 |
|
$word = $filters->apply($word); |
216
|
|
|
|
217
|
6 |
|
if (!$word) { |
218
|
1 |
|
return; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
6 |
|
$entity = $this->getWordForText($word); |
|
|
|
|
223
|
|
|
|
224
|
6 |
|
if (!$entity) { |
225
|
6 |
|
$entity = new Word(); |
226
|
|
|
$entity |
227
|
6 |
|
->setList($this) |
228
|
6 |
|
->setText($word) |
|
|
|
|
229
|
6 |
|
->setOrientation(Word::DIR_HORIZONTAL) |
230
|
6 |
|
->setColor('#000000') |
231
|
|
|
; |
232
|
6 |
|
$this->addWord($entity); |
233
|
|
|
} |
234
|
|
|
|
235
|
6 |
|
$entity->setCount($entity->getCount() + 1); |
236
|
6 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $html |
240
|
|
|
* @param Filters $filters |
241
|
|
|
* @param int $maxWords |
242
|
|
|
*/ |
243
|
1 |
|
public function importHtml($html, Filters $filters = null, $maxWords = 100) |
244
|
|
|
{ |
245
|
1 |
|
if (!$html) { |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
$d = new \DOMDocument; |
250
|
1 |
|
$mock = new \DOMDocument; |
251
|
1 |
|
libxml_use_internal_errors(true); |
252
|
1 |
|
$d->loadHTML($html); |
253
|
1 |
|
libxml_use_internal_errors(false); |
254
|
1 |
|
$body = $d->getElementsByTagName('body')->item(0); |
255
|
1 |
|
if ($body) { |
256
|
1 |
|
foreach ($body->childNodes as $child) { |
257
|
1 |
|
$mock->appendChild($mock->importNode($child, true)); |
258
|
|
|
} |
259
|
|
|
} |
260
|
1 |
|
$text = html_entity_decode(strip_tags($mock->saveHTML())); |
261
|
1 |
|
$this->importWords($text, $filters, $maxWords); |
262
|
1 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $url |
266
|
|
|
* @param Filters $filters |
267
|
|
|
* @param int $maxWords |
268
|
|
|
*/ |
269
|
1 |
|
public function importUrl($url, Filters $filters = null, $maxWords = 100) |
270
|
|
|
{ |
271
|
1 |
|
$this->importHtml(file_get_contents($url), $filters, $maxWords); |
272
|
1 |
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Apply the given $filters to the $list. |
276
|
|
|
* @param Filters $filters |
277
|
|
|
*/ |
278
|
|
|
public function filterWords(Filters $filters) |
279
|
|
|
{ |
280
|
|
|
/** @var Word $word */ |
281
|
|
|
foreach ($this->getWords() as $word) { |
282
|
|
|
$filtered = $filters->apply($word->getText()); |
283
|
|
|
if (!$filtered) { |
284
|
|
|
$this->words->remove($word); |
285
|
|
|
continue; |
286
|
|
|
} |
287
|
|
|
$word->setText($filtered); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param int $verticalProbability |
293
|
|
|
*/ |
294
|
3 |
|
public function randomizeOrientation($verticalProbability = 50) |
295
|
|
|
{ |
296
|
|
|
/** @var \SixtyNine\CloudBundle\Entity\Word $word */ |
297
|
3 |
|
foreach ($this->getWords() as $word) { |
298
|
|
|
|
299
|
3 |
|
$orientation = random_int(0, 100) <= $verticalProbability |
300
|
3 |
|
? Word::DIR_VERTICAL |
301
|
3 |
|
: Word::DIR_HORIZONTAL |
302
|
|
|
; |
303
|
|
|
|
304
|
3 |
|
$word->setOrientation($orientation); |
305
|
|
|
} |
306
|
3 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param ColorGeneratorInterface $colorGenerator |
310
|
|
|
*/ |
311
|
3 |
|
public function randomizeColors(ColorGeneratorInterface $colorGenerator) |
312
|
|
|
{ |
313
|
|
|
/** @var \SixtyNine\CloudBundle\Entity\Word $word */ |
314
|
3 |
|
foreach ($this->getWords() as $word) { |
315
|
3 |
|
$word->setColor($colorGenerator->getNextColor()); |
316
|
|
|
} |
317
|
3 |
|
} |
318
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: