CanonicalRange::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 4
dl 0
loc 4
c 0
b 0
f 0
cc 1
rs 10
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, string $fromBound = '[', string $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