Completed
Pull Request — 2.0 (#75)
by Julien
02:03
created

BaseRange   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 3 Features 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 78
c 3
b 3
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
getRegexp() 0 1 ?
getSubElement() 0 1 ?
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
 * @abstract
22
 */
23
abstract class BaseRange
24
{
25
    public $start_limit;
26
    public $end_limit;
27
    public $start_incl;
28
    public $end_incl;
29
30
    protected $description;
31
32
    /**
33
     * getRegexp
34
     *
35
     * This function function must capture 4 elements of the description in
36
     * this order:
37
     *
38
     * - left bracket style
39
     * - left element
40
     * - right element
41
     * - right bracket style
42
     *
43
     * @access protected
44
     * @return string
45
     */
46
    abstract protected function getRegexp();
47
48
    /**
49
     * getSubElement
50
     *
51
     * Return the representation for each element.
52
     *
53
     * @access protected
54
     * @param  string $element
55
     * @return mixed
56
     */
57
    abstract protected function getSubElement($element);
58
59
    /**
60
     * __construct
61
     *
62
     * Create an instance from a string definition. This string definition
63
     * matches PostgreSQL range definition.
64
     *
65
     * @access public
66
     * @param  string $description
67
     * @throws \InvalidArgumentException
68
     */
69
70
    public function __construct($description)
71
    {
72
        if (!preg_match($this->getRegexp(), $description, $matches)) {
73
            throw new \InvalidArgumentException(
74
                sprintf(
75
                    "Could not parse NumRange description '%s'.",
76
                    $description
77
                )
78
            );
79
        }
80
81
        $this->start_limit = $this->getSubElement($matches[2]);
82
        $this->end_limit   = $this->getSubElement($matches[3]);
83
        $this->start_incl  = (bool) ($matches[1] === '[');
84
        $this->end_incl    = (bool) ($matches[4] === ']');
85
        $this->description = $description;
86
    }
87
88
    /**
89
     * __toString
90
     *
91
     * Text representation of a range.
92
     *
93
     * @access public
94
     * @return string
95
     */
96
    public function __toString()
97
    {
98
        return $this->description;
99
    }
100
}
101