Completed
Push — master ( f38bb4...c3151d )
by belamov
25s
created

Range::from()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 2
nop 0
dl 0
loc 7
c 0
b 0
f 0
cc 2
rs 10
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
    /**
53
     * @return string
54
     */
55
    public function __toString()
56
    {
57
        return "{$this->fromBound}{$this->from},{$this->to}{$this->toBound}";
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    abstract public function forSql(): string;
64
65
    private function checkForEmptyBoundaries(): void
66
    {
67
        if ($this->to === '') {
68
            $this->to = null;
69
        }
70
        if ($this->from === '') {
71
            $this->from = null;
72
        }
73
    }
74
}
75