BaseRange   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getRegexp() 0 1 ?
getSubElement() 0 1 ?
A __construct() 0 25 3
A __toString() 0 4 1
1
<?php
2
/*
3
 * This file is part of PommProject's Foundation package.
4
 *
5
 * (c) 2014 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter\Type;
11
12
/**
13
 * BaseRange
14
 *
15
 * Abstract classes for range types.
16
 *
17
 * @package Foundation
18
 * @copyright 2014 Grégoire HUBERT
19
 * @author Grégoire HUBERT
20
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
21
 */
22
abstract class BaseRange
23
{
24
    const INFINITY_MAX = 'infinity';
25
    const INFINITY_MIN = '-infinity';
26
    const EMPTY_RANGE  = 'empty';
27
28
    public $start_limit;
29
    public $end_limit;
30
    public $start_incl;
31
    public $end_incl;
32
33
    protected $description;
34
35
    /**
36
     * getRegexp
37
     *
38
     * This function function must capture 4 elements of the description in
39
     * this order:
40
     *
41
     * - left bracket style
42
     * - left element
43
     * - right element
44
     * - right bracket style
45
     *
46
     * @return string
47
     */
48
    abstract protected function getRegexp();
49
50
    /**
51
     * getSubElement
52
     *
53
     * Return the representation for each element.
54
     *
55
     * @param  string $element
56
     * @return mixed
57
     */
58
    abstract protected function getSubElement($element);
59
60
    /**
61
     * __construct
62
     *
63
     * Create an instance from a string definition. This string definition
64
     * matches PostgreSQL range definition.
65
     *
66
     * @param  string $description
67
     * @throws \InvalidArgumentException
68
     */
69
    public function __construct($description)
70
    {
71
        if (!preg_match($this->getRegexp(), $description, $matches)) {
72
            throw new \InvalidArgumentException(
73
                sprintf(
74
                    "Could not parse range description '%s'.",
75
                    $description
76
                )
77
            );
78
        }
79
80
        if (count($matches) === 2) {
81
            $this->start_limit = self::EMPTY_RANGE;
82
            $this->end_limit   = self::EMPTY_RANGE;
83
            $this->start_incl  = null;
84
            $this->end_incl    = null;
85
        } else {
86
            $this->start_limit = $this->getSubElement($matches[3]);
87
            $this->end_limit   = $this->getSubElement($matches[4]);
88
            $this->start_incl  = (bool) ($matches[2] === '[');
89
            $this->end_incl    = (bool) ($matches[5] === ']');
90
        }
91
92
        $this->description = $description;
93
    }
94
95
    /**
96
     * __toString
97
     *
98
     * Text representation of a range.
99
     *
100
     * @return string
101
     */
102
    public function __toString()
103
    {
104
        return $this->description;
105
    }
106
}
107