@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | protected function setWordsCollection(Collection $wordsCollection): self |
21 | 21 | { |
22 | 22 | $this->wordsCollection = $wordsCollection->filter( |
23 | - fn ($word) => Str::length($word) >= $this->minWordLen && Str::length($word) <= $this->maxWordLen |
|
23 | + fn($word) => Str::length($word) >= $this->minWordLen && Str::length($word) <= $this->maxWordLen |
|
24 | 24 | ) |
25 | 25 | ->map(fn($word) => Str::upper($word)); |
26 | 26 | |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | $len = $this->wordLengths->keys()->random(); |
41 | 41 | } while (in_array($len, $exclude)); |
42 | 42 | |
43 | - $available = $this->wordsCollection->filter(fn ($word) => Str::length($word) === $len)->count(); |
|
43 | + $available = $this->wordsCollection->filter(fn($word) => Str::length($word) === $len)->count(); |
|
44 | 44 | |
45 | 45 | if ($available > 0) { |
46 | 46 | return $len; |
@@ -53,14 +53,14 @@ discard block |
||
53 | 53 | |
54 | 54 | protected function markWordUsed($word): void |
55 | 55 | { |
56 | - $this->wordsCollection = $this->wordsCollection->reject(function ($current) use ($word) { |
|
56 | + $this->wordsCollection = $this->wordsCollection->reject(function($current) use ($word) { |
|
57 | 57 | return $current === $word; |
58 | 58 | }); |
59 | 59 | } |
60 | 60 | |
61 | 61 | protected function getRandomWord(int $len): string |
62 | 62 | { |
63 | - $word = $this->wordsCollection->filter(function ($word) use ($len) { |
|
63 | + $word = $this->wordsCollection->filter(function($word) use ($len) { |
|
64 | 64 | return Str::length($word) === $len; |
65 | 65 | })->random(1)->first(); |
66 | 66 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | protected function getWordLike(string $pattern): ?string |
73 | 73 | { |
74 | 74 | $pattern = Str::replace("_", ".", $pattern); |
75 | - $words = $this->wordsCollection->filter(function ($word) use ($pattern) { |
|
75 | + $words = $this->wordsCollection->filter(function($word) use ($pattern) { |
|
76 | 76 | return preg_match("/^$pattern\$/i", $word); |
77 | 77 | }); |
78 | 78 |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | |
47 | 47 | public function setOrientation(int $orientation): self |
48 | 48 | { |
49 | - if (! in_array($orientation, [0,1,2,3])) { |
|
49 | + if (!in_array($orientation, [0, 1, 2, 3])) { |
|
50 | 50 | throw new RuntimeException("Orientation not valid"); |
51 | 51 | } |
52 | 52 | $this->orientation = $orientation; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | { |
100 | 100 | return $str; |
101 | 101 | $r = ''; |
102 | - for ($i = Str::length($str); $i>=0; $i--) { |
|
102 | + for ($i = Str::length($str); $i >= 0; $i--) { |
|
103 | 103 | $r .= Str::substr($str, $i, 1); |
104 | 104 | } |
105 | 105 | return $r; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | $this->cells = array_fill(0, $this->gridSize * $this->gridSize, null); |
74 | 74 | |
75 | 75 | for ($i = 0; $i < (2 * $this->gridSize * $this->gridSize); $i++) { |
76 | - $this->columnArray[$i]=$this->getColumnDefault($i); |
|
76 | + $this->columnArray[$i] = $this->getColumnDefault($i); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | return $this; |
@@ -82,15 +82,15 @@ discard block |
||
82 | 82 | public function generate(): self |
83 | 83 | { |
84 | 84 | $blocks = $this->gridSize * $this->gridSize; |
85 | - $i=rand(0, $blocks-1); |
|
85 | + $i = rand(0, $blocks - 1); |
|
86 | 86 | |
87 | - $complete=0; |
|
87 | + $complete = 0; |
|
88 | 88 | while ($complete < $blocks) { |
89 | 89 | $this->placeWord($i); |
90 | 90 | $complete++; |
91 | 91 | $i++; |
92 | - if ($i==$blocks) { |
|
93 | - $i=0; |
|
92 | + if ($i == $blocks) { |
|
93 | + $i = 0; |
|
94 | 94 | } |
95 | 95 | } |
96 | 96 | |
@@ -100,25 +100,25 @@ discard block |
||
100 | 100 | protected function addPlacedWord(Word $word, int $increment, int $len): void |
101 | 101 | { |
102 | 102 | $string = ''; |
103 | - $flag=false; |
|
103 | + $flag = false; |
|
104 | 104 | |
105 | - for ($i=$word->getStart(); $i<=$word->getEnd(); $i+=$increment) { |
|
105 | + for ($i = $word->getStart(); $i <= $word->getEnd(); $i += $increment) { |
|
106 | 106 | if ($this->cells[$i] === null) { |
107 | 107 | $string .= '_'; |
108 | 108 | } else { |
109 | 109 | $string .= $this->cells[$i]; |
110 | - $flag=true; |
|
110 | + $flag = true; |
|
111 | 111 | } |
112 | 112 | } |
113 | 113 | |
114 | - if (! $flag) { |
|
114 | + if (!$flag) { |
|
115 | 115 | $randomWord = $this->getRandomWord($len); |
116 | 116 | $word->setLabel($randomWord); |
117 | 117 | $this->addWord($word); |
118 | 118 | return; |
119 | 119 | } |
120 | 120 | |
121 | - if (strpos($string, '_')===false) { |
|
121 | + if (strpos($string, '_') === false) { |
|
122 | 122 | return; |
123 | 123 | } |
124 | 124 | |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | protected function placeWordHorizontally(Word $word, int $len): void |
130 | 130 | { |
131 | 131 | $inc = 1; |
132 | - $word->setEnd($word->getStart()+$len-1); |
|
132 | + $word->setEnd($word->getStart() + $len - 1); |
|
133 | 133 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
134 | - $word->setStart($word->getStart()-1); |
|
135 | - $word->setEnd($word->getStart()+$len-1); |
|
134 | + $word->setStart($word->getStart() - 1); |
|
135 | + $word->setEnd($word->getStart() + $len - 1); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | $this->addPlacedWord($word, $inc, $len); |
@@ -140,11 +140,11 @@ discard block |
||
140 | 140 | |
141 | 141 | protected function placeWordVertical(Word $word, int $len): void |
142 | 142 | { |
143 | - $inc=$this->gridSize; |
|
144 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
145 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
146 | - $word->setStart($word->getStart()-$this->gridSize); |
|
147 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
143 | + $inc = $this->gridSize; |
|
144 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
145 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
146 | + $word->setStart($word->getStart() - $this->gridSize); |
|
147 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | $this->addPlacedWord($word, $inc, $len); |
@@ -152,30 +152,30 @@ discard block |
||
152 | 152 | |
153 | 153 | protected function placeWordDiagonallyLtr(Word $word, int $len): void |
154 | 154 | { |
155 | - $inc=$this->gridSize+1; |
|
156 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
155 | + $inc = $this->gridSize + 1; |
|
156 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
157 | 157 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
158 | - $word->setStart($word->getStart()-1); |
|
159 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
158 | + $word->setStart($word->getStart() - 1); |
|
159 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
160 | 160 | } |
161 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
162 | - $word->setStart($word->getStart()-$this->gridSize); |
|
163 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
161 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
162 | + $word->setStart($word->getStart() - $this->gridSize); |
|
163 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
164 | 164 | } |
165 | 165 | $this->addPlacedWord($word, $inc, $len); |
166 | 166 | } |
167 | 167 | |
168 | 168 | protected function placeWordDiagonallyRtl(Word $word, int $len): void |
169 | 169 | { |
170 | - $inc=$this->gridSize-1; |
|
171 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
170 | + $inc = $this->gridSize - 1; |
|
171 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
172 | 172 | while ($this->columnArray[$word->getEnd()] > $this->columnArray[$word->getStart()]) { |
173 | - $word->setStart($word->getStart()+1); |
|
174 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
173 | + $word->setStart($word->getStart() + 1); |
|
174 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
175 | 175 | } |
176 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
177 | - $word->setStart($word->getStart()-$this->gridSize); |
|
178 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
176 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
177 | + $word->setStart($word->getStart() - $this->gridSize); |
|
178 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
179 | 179 | } |
180 | 180 | $this->addPlacedWord($word, $inc, $len); |
181 | 181 | } |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | |
210 | 210 | protected function getColumnDefault(int $x): int |
211 | 211 | { |
212 | - return ($x % $this->gridSize)+1; |
|
212 | + return ($x % $this->gridSize) + 1; |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | protected function addWord(Word $word): void |
@@ -218,23 +218,23 @@ discard block |
||
218 | 218 | return; |
219 | 219 | } |
220 | 220 | |
221 | - $j=0; |
|
221 | + $j = 0; |
|
222 | 222 | $incrementBy = 1; |
223 | 223 | switch ($word->getOrientation()) { |
224 | 224 | case Word::HORIZONTAL: |
225 | - $incrementBy=1; |
|
225 | + $incrementBy = 1; |
|
226 | 226 | break; |
227 | 227 | |
228 | 228 | case Word::VERTICAL: |
229 | - $incrementBy=$this->gridSize; |
|
229 | + $incrementBy = $this->gridSize; |
|
230 | 230 | break; |
231 | 231 | |
232 | 232 | case Word::DIAGONAL_LEFT_TO_RIGHT: |
233 | - $incrementBy=$this->gridSize+1; |
|
233 | + $incrementBy = $this->gridSize + 1; |
|
234 | 234 | break; |
235 | 235 | |
236 | 236 | case Word::DIAGONAL_RIGHT_TO_LEFT: |
237 | - $incrementBy=$this->gridSize-1; |
|
237 | + $incrementBy = $this->gridSize - 1; |
|
238 | 238 | break; |
239 | 239 | } |
240 | 240 | |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | $j++; |
244 | 244 | } |
245 | 245 | |
246 | - $this->wordsList[]=$word; |
|
246 | + $this->wordsList[] = $word; |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | public function getTextGrid() |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | |
278 | 278 | public function getPuzzleWords() |
279 | 279 | { |
280 | - return collect($this->wordsList)->map(function (Word $word) { |
|
280 | + return collect($this->wordsList)->map(function(Word $word) { |
|
281 | 281 | return $word->getLabel(true); |
282 | 282 | }); |
283 | 283 | } |