1 | <?php |
||
16 | class Usher |
||
17 | { |
||
18 | const DEFAULT_MAX_TRIES = 100000; |
||
19 | |||
20 | /** @var int */ |
||
21 | protected $maxTries; |
||
22 | |||
23 | /** @var \SixtyNine\Cloud\Usher\MaskInterface */ |
||
24 | protected $mask; |
||
25 | |||
26 | /** @var \SixtyNine\Cloud\Placer\PlacerInterface */ |
||
27 | protected $placer; |
||
28 | |||
29 | /** @var \SixtyNine\Cloud\FontMetrics */ |
||
30 | protected $metrics; |
||
31 | |||
32 | /** @var Logger */ |
||
33 | protected $logger; |
||
34 | |||
35 | /** |
||
36 | * @param int $imgWidth |
||
37 | * @param int $imgHeight |
||
38 | * @param PlacerInterface $placer |
||
39 | * @param FontMetrics $metrics |
||
40 | * @param int $maxTries |
||
41 | */ |
||
42 | 6 | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * @param string $word |
||
60 | * @param string $font |
||
61 | * @param int $fontSize |
||
62 | * @param int $angle |
||
63 | * @param bool $precise |
||
64 | * @return bool|Box |
||
65 | */ |
||
66 | 6 | public function getPlace($word, $font, $fontSize, $angle, $precise = false) |
|
98 | |||
99 | /** |
||
100 | * @param string $word |
||
101 | * @param Box $place |
||
102 | * @param string $font |
||
103 | * @param int $size |
||
104 | * @param int $angle |
||
105 | */ |
||
106 | 1 | public function addWordToMask($word, Box $place, $font, $size, $angle) |
|
137 | |||
138 | /** |
||
139 | * Search a free place for a new box. |
||
140 | * @param \SixtyNine\DataTypes\Box $bounds |
||
141 | * @param \SixtyNine\DataTypes\Box $box |
||
142 | * @return bool|Box |
||
143 | */ |
||
144 | 6 | protected function searchPlace(Box $bounds, Box $box) |
|
145 | { |
||
146 | |||
147 | 6 | $this->logger->log(' Search place for ' . $box, Logger::DEBUG); |
|
148 | |||
149 | 6 | $placeFound = false; |
|
150 | 6 | $current = $this->placer->getFirstPlaceToTry(); |
|
151 | 6 | $curTry = 1; |
|
152 | 6 | $currentBox = null; |
|
153 | |||
154 | 6 | while (!$placeFound) { |
|
155 | |||
156 | 6 | if (!$current) { |
|
157 | 1 | return false; |
|
158 | } |
||
159 | |||
160 | 6 | if ($curTry > $this->maxTries) { |
|
161 | return false; |
||
162 | } |
||
163 | |||
164 | 6 | $currentBox = $box->move($current->getX(), $current->getY()); |
|
165 | |||
166 | 6 | $outOfBounds = !$currentBox->inside($bounds); |
|
167 | |||
168 | 6 | if (!$outOfBounds) { |
|
169 | 6 | $placeFound = !$this->mask->overlaps($currentBox); |
|
170 | 6 | $placeFound = $placeFound && !$outOfBounds; |
|
171 | } |
||
172 | |||
173 | 6 | $this->logger->log(sprintf( |
|
174 | 6 | ' Trying %s --> %s', |
|
175 | $currentBox, |
||
176 | 6 | $outOfBounds ? 'Out of bounds' : ($placeFound ? 'OK' : 'Collision') |
|
177 | 6 | ), Logger::DEBUG); |
|
178 | |||
179 | 6 | if ($placeFound) { |
|
180 | 6 | break; |
|
181 | } |
||
182 | |||
183 | 6 | $current = $this->placer->getNextPlaceToTry($current); |
|
184 | 6 | $curTry++; |
|
185 | } |
||
186 | |||
187 | 6 | return $currentBox->inside($bounds) ? $currentBox : false; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return MaskInterface |
||
192 | */ |
||
193 | 5 | public function getMask() |
|
197 | } |
||
198 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: