1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Belamov\PostgresRange\Ranges; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CanonicalRange. |
7
|
|
|
* |
8
|
|
|
* Range that automatically canonicalizes its boundaries to [) |
9
|
|
|
*/ |
10
|
|
|
abstract class CanonicalRange extends Range |
11
|
|
|
{ |
12
|
|
|
public function __construct(string $from = null, string $to = null, $fromBound = '[', $toBound = ')') |
13
|
|
|
{ |
14
|
|
|
parent::__construct($from, $to, $fromBound, $toBound); |
15
|
|
|
$this->canonicalizeBoundaries(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The built-in range types int4range, int8range, and daterange all use a canonical form |
20
|
|
|
* that includes the lower bound and excludes the upper bound; that is, [). |
21
|
|
|
*/ |
22
|
|
|
private function canonicalizeBoundaries(): void |
23
|
|
|
{ |
24
|
|
|
$this->canonicalizeLowerBoundary(); |
25
|
|
|
$this->canonicalizeUpperBoundary(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function canonicalizeLowerBoundary(): void |
29
|
|
|
{ |
30
|
|
|
if ($this->fromBound === '(') { |
31
|
|
|
if ($this->from !== null) { |
32
|
|
|
$this->from = $this->addToDiscreteBoundary($this->from); |
33
|
|
|
} |
34
|
|
|
$this->fromBound = '['; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $boundary |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
abstract protected function addToDiscreteBoundary(string $boundary): string; |
43
|
|
|
|
44
|
|
|
private function canonicalizeUpperBoundary(): void |
45
|
|
|
{ |
46
|
|
|
if ($this->toBound === ']') { |
47
|
|
|
if ($this->to !== null) { |
48
|
|
|
$this->to = $this->addToDiscreteBoundary($this->to); |
49
|
|
|
} |
50
|
|
|
$this->toBound = ')'; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function fromBound(): string |
55
|
|
|
{ |
56
|
|
|
$this->canonicalizeLowerBoundary(); |
57
|
|
|
|
58
|
|
|
return parent::fromBound(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function toBound(): string |
62
|
|
|
{ |
63
|
|
|
$this->canonicalizeUpperBoundary(); |
64
|
|
|
|
65
|
|
|
return parent::toBound(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|