Passed
Push — master ( a3e1af...365548 )
by belamov
02:48
created

Range::hasUpperBoundary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Belamov\PostgresRange\Ranges;
4
5
abstract class Range
6
{
7
    protected string $fromBound;
8
    protected string $toBound;
9
    protected ?string $from;
10
    protected ?string $to;
11
12
    public function __construct(string $from = null, string $to = null, $fromBound = '[', $toBound = ')')
13
    {
14
        $this->from = $from;
15
        $this->to = $to;
16
        $this->fromBound = $fromBound;
17
        $this->toBound = $toBound;
18
19
        $this->checkForEmptyBoundaries();
20
    }
21
22
    /**
23
     * @return mixed
24
     */
25
    public function from()
26
    {
27
        if ($this->from === null) {
28
            return;
29
        }
30
31
        return $this->transformBoundary($this->from);
32
    }
33
34
    /**
35
     * @param  string  $boundary
36
     * @return mixed
37
     */
38
    abstract protected function transformBoundary(string $boundary);
39
40
    /**
41
     * @return mixed
42
     */
43
    public function to()
44
    {
45
        if ($this->to === null) {
46
            return;
47
        }
48
49
        return $this->transformBoundary($this->to);
50
    }
51
52
    public function hasLowerBoundary(): bool
53
    {
54
        return $this->from !== null;
55
    }
56
57
    public function hasUpperBoundary(): bool
58
    {
59
        return $this->to !== null;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function __toString()
66
    {
67
        return "{$this->fromBound}{$this->from},{$this->to}{$this->toBound}";
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    abstract public function forSql(): string;
74
75
    private function checkForEmptyBoundaries(): void
76
    {
77
        if ($this->to === '') {
78
            $this->to = null;
79
        }
80
        if ($this->from === '') {
81
            $this->from = null;
82
        }
83
    }
84
}
85