1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Belamov\PostgresRange\Ranges; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
|
7
|
|
|
abstract class Range implements JsonSerializable |
8
|
|
|
{ |
9
|
|
|
protected string $fromBound; |
10
|
|
|
protected string $toBound; |
11
|
|
|
protected ?string $from; |
12
|
|
|
protected ?string $to; |
13
|
|
|
|
14
|
|
|
public function __construct(?string $from = null, ?string $to = null, string $fromBound = '[', string $toBound = ')') |
15
|
|
|
{ |
16
|
|
|
$this->from = $from; |
17
|
|
|
$this->to = $to; |
18
|
|
|
$this->fromBound = $fromBound; |
19
|
|
|
$this->toBound = $toBound; |
20
|
|
|
|
21
|
|
|
$this->checkForEmptyBoundaries(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
|
public function from() |
28
|
|
|
{ |
29
|
|
|
if ($this->from === null) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this->transformBoundary($this->from); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $boundary |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
abstract protected function transformBoundary(string $boundary); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function to() |
46
|
|
|
{ |
47
|
|
|
if ($this->to === null) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->transformBoundary($this->to); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasLowerBoundary(): bool |
55
|
|
|
{ |
56
|
|
|
return $this->from !== null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function hasUpperBoundary(): bool |
60
|
|
|
{ |
61
|
|
|
return $this->to !== null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function __toString() |
68
|
|
|
{ |
69
|
|
|
return "{$this->fromBound}{$this->from},{$this->to}{$this->toBound}"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
abstract public function forSql(): string; |
76
|
|
|
|
77
|
|
|
private function checkForEmptyBoundaries(): void |
78
|
|
|
{ |
79
|
|
|
if ($this->to === '') { |
80
|
|
|
$this->to = null; |
81
|
|
|
} |
82
|
|
|
if ($this->from === '') { |
83
|
|
|
$this->from = null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function fromBound(): string |
88
|
|
|
{ |
89
|
|
|
return $this->fromBound; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function toBound(): string |
93
|
|
|
{ |
94
|
|
|
return $this->toBound; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function jsonSerialize(): string |
98
|
|
|
{ |
99
|
|
|
return $this->__toString(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|