CanonicalRange   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 41
c 0
b 0
f 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canonicalizeUpperBoundary() 0 7 3
A __construct() 0 4 1
A canonicalizeBoundaries() 0 4 1
A canonicalizeLowerBoundary() 0 7 3
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