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); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
return $this->label; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getInversed(): bool |
94
|
|
|
{ |
95
|
|
|
return $this->inversed; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function reverse(string $str): string |
99
|
|
|
{ |
100
|
|
|
return $str; |
101
|
|
|
$r = ''; |
|
|
|
|
102
|
|
|
for ($i = Str::length($str); $i>=0; $i--) { |
103
|
|
|
$r .= Str::substr($str, $i, 1); |
104
|
|
|
} |
105
|
|
|
return $r; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|