|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use SixtyNine\Cloud\Color\ColorGeneratorInterface; |
|
6
|
|
|
use SixtyNine\Cloud\Filters\Filters; |
|
7
|
|
|
use SixtyNine\Cloud\Model\WordsList; |
|
8
|
|
|
|
|
9
|
|
|
class WordsListBuilder |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var \SixtyNine\Cloud\Filters\Filters */ |
|
12
|
|
|
protected $filters; |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $words; |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $url; |
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
protected $maxWords; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $sortOrder; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $sortBy; |
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
protected $randomizeOrientation; |
|
25
|
|
|
/** @var ColorGeneratorInterface */ |
|
26
|
|
|
protected $colorGenerator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Disallow direct instantiation |
|
30
|
|
|
*/ |
|
31
|
7 |
|
protected function __construct() |
|
32
|
|
|
{ |
|
33
|
7 |
|
$this->filters = FiltersBuilder::create()->build(); |
|
34
|
7 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return WordsListBuilder |
|
38
|
|
|
*/ |
|
39
|
7 |
|
public static function create() |
|
40
|
|
|
{ |
|
41
|
7 |
|
return new self(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Filters $filters |
|
46
|
|
|
* @return WordsListBuilder |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function setFilters(Filters $filters) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->filters = $filters; |
|
51
|
2 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $max |
|
56
|
|
|
* @return WordsListBuilder |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setMaxWords($max) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->maxWords = $max; |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $words |
|
66
|
|
|
* @return WordsListBuilder |
|
67
|
|
|
*/ |
|
68
|
5 |
|
public function importWords($words) |
|
69
|
|
|
{ |
|
70
|
5 |
|
$this->words = $words; |
|
71
|
5 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $url |
|
76
|
|
|
* @return WordsListBuilder |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function importUrl($url) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->url = $url; |
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $sortBy |
|
86
|
|
|
* @param string $sortOrder |
|
87
|
|
|
* @return WordsListBuilder |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function sort($sortBy, $sortOrder) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->sortBy = $sortBy; |
|
92
|
1 |
|
$this->sortOrder = $sortOrder; |
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param int $rate |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function randomizeOrientation($rate = 50) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$this->randomizeOrientation = $rate; |
|
103
|
3 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param ColorGeneratorInterface $generator |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function randomizeColors(ColorGeneratorInterface $generator) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$this->colorGenerator = $generator; |
|
113
|
3 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @return WordsList |
|
119
|
|
|
*/ |
|
120
|
7 |
|
public function build($name) |
|
121
|
|
|
{ |
|
122
|
7 |
|
$list = new WordsList(); |
|
123
|
7 |
|
$list->setName($name); |
|
124
|
|
|
|
|
125
|
7 |
|
if ($this->words) { |
|
126
|
5 |
|
$list->importWords($this->words, $this->filters, $this->maxWords); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
7 |
|
if ($this->url) { |
|
130
|
1 |
|
$list->importUrl($this->url, $this->filters, $this->maxWords); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
if ($this->sortBy && $this->sortOrder) { |
|
134
|
1 |
|
$list->sortWords($this->sortBy, $this->sortOrder); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
7 |
|
if (null !== $this->randomizeOrientation) { |
|
138
|
3 |
|
$list->randomizeOrientation($this->randomizeOrientation); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
7 |
|
if (null !== $this->colorGenerator) { |
|
142
|
3 |
|
$list->randomizeColors($this->colorGenerator); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
7 |
|
return $list; |
|
146
|
|
|
} |
|
147
|
|
|
} |