customd /
word-finder
| 1 | <?php |
||||
| 2 | namespace CustomD\WordFinder; |
||||
| 3 | |||||
| 4 | use RuntimeException; |
||||
| 5 | use Illuminate\Support\Str; |
||||
| 6 | |||||
| 7 | class Word |
||||
| 8 | { |
||||
| 9 | |||||
| 10 | public const HORIZONTAL = 0; |
||||
| 11 | public const VERTICAL = 1; |
||||
| 12 | public const DIAGONAL_LEFT_TO_RIGHT = 2; |
||||
| 13 | public const DIAGONAL_RIGHT_TO_LEFT = 3; |
||||
| 14 | |||||
| 15 | protected int $start = 0; |
||||
| 16 | protected int $end = -1; |
||||
| 17 | protected int $orientation = 0; |
||||
| 18 | protected ?string $label = null; |
||||
| 19 | protected bool $inversed = false; |
||||
| 20 | |||||
| 21 | public function __construct($start, $end, int $orientation = 0, ?string $label = null, bool $inversed = false) |
||||
| 22 | { |
||||
| 23 | $this->setStart($start) |
||||
| 24 | ->setEnd($end) |
||||
| 25 | ->setOrientation($orientation) |
||||
| 26 | ->setLabel($label) |
||||
| 27 | ->setinversed($inversed); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | public static function createRandom(?int $start, ?string $label = null): self |
||||
| 31 | { |
||||
| 32 | return new self($start, -1, rand(0, 3), $label, rand(0, 1) === 1); |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | public function setStart(int $start): self |
||||
| 36 | { |
||||
| 37 | $this->start = $start; |
||||
| 38 | return $this; |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | public function setEnd($end): self |
||||
| 42 | { |
||||
| 43 | $this->end = $end; |
||||
| 44 | return $this; |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | public function setOrientation(int $orientation): self |
||||
| 48 | { |
||||
| 49 | if (! in_array($orientation, [0,1,2,3])) { |
||||
| 50 | throw new RuntimeException("Orientation not valid"); |
||||
| 51 | } |
||||
| 52 | $this->orientation = $orientation; |
||||
| 53 | return $this; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | public function setLabel(?string $label): self |
||||
| 57 | { |
||||
| 58 | if ($label !== null) { |
||||
| 59 | $this->label = $this->getInversed() ? $this->reverse($label) : $label; |
||||
| 60 | } |
||||
| 61 | return $this; |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | public function setInversed(bool $inversed): self |
||||
| 65 | { |
||||
| 66 | $this->inversed = $inversed; |
||||
| 67 | return $this; |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | public function getStart() |
||||
| 71 | { |
||||
| 72 | return $this->start; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | public function getEnd() |
||||
| 76 | { |
||||
| 77 | return $this->end; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | public function getOrientation(): int |
||||
| 81 | { |
||||
| 82 | return $this->orientation; |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | public function getLabel(bool $forceNonInverced = false): ?string |
||||
| 86 | { |
||||
| 87 | if ($forceNonInverced && $this->getInversed()) { |
||||
| 88 | return $this->reverse($this->label); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 89 | } |
||||
| 90 | return $this->label; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | public function getInversed(): bool |
||||
| 94 | { |
||||
| 95 | return $this->inversed; |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | public function reverse(string $string, ?string $encoding = null): string |
||||
| 99 | { |
||||
| 100 | $chars = mb_str_split($string, 1, $encoding ?? mb_internal_encoding()); |
||||
|
0 ignored issues
–
show
It seems like
$encoding ?? mb_internal_encoding() can also be of type true; however, parameter $encoding of mb_str_split() does only seem to accept null|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 101 | return implode('', array_reverse($chars)); |
||||
| 102 | } |
||||
| 103 | } |
||||
| 104 |