Completed
Push — master ( 5ba831...bd1989 )
by grégoire
01:54
created

Box   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 10 1
A __construct() 0 20 2
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
 * Box
14
 *
15
 * PHP type for PostgreSQL's box type.
16
 *
17
 * @package Foundation
18
 * @copyright 2017 Grégoire HUBERT
19
 * @author Miha Vrhovnik
20
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
21
 */
22
class Box
23
{
24
    public $topX;
25
    public $topY;
26
    public $bottomX;
27
    public $bottomY;
28
29
    /**
30
     * __construct
31
     *
32
     * Create a box from a string description.
33
     *
34
     * @access public
35
     * @param  string $description
36
     */
37
    public function __construct($description)
38
    {
39
        $description = trim($description, ' ()');
40
        $regex = '/([0-9e\-+\.]+), *([0-9e\-+\.]+) *\), *\( *([0-9e\-+\.]+), *([0-9e\-+\.]+)/';
41
42
        if (!preg_match($regex, $description, $matches)) {
43
            throw new \InvalidArgumentException(
44
                sprintf(
45
                    "Could not parse box representation '%s'.",
46
                    $description
47
                )
48
            );
49
        }
50
51
        $this->topX = (float) $matches[1];
52
        $this->topY = (float) $matches[2];
53
        $this->bottomX = (float) $matches[3];
54
        $this->bottomY = (float) $matches[4];
55
56
    }
57
58
    /**
59
     * __toString
60
     *
61
     * Return a string representation of Box.
62
     *
63
     * @access public
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return sprintf(
69
            "((%s,%s),(%s,%s))",
70
            $this->topX,
71
            $this->topY,
72
            $this->bottomX,
73
            $this->bottomY
74
        );
75
    }
76
}
77