Box::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * @param  string $description
35
     */
36
    public function __construct($description)
37
    {
38
        $description = trim($description, ' ()');
39
        $regex = '/([0-9e\-+\.]+), *([0-9e\-+\.]+) *\), *\( *([0-9e\-+\.]+), *([0-9e\-+\.]+)/';
40
41
        if (!preg_match($regex, $description, $matches)) {
42
            throw new \InvalidArgumentException(
43
                sprintf(
44
                    "Could not parse box representation '%s'.",
45
                    $description
46
                )
47
            );
48
        }
49
50
        $this->topX = (float) $matches[1];
51
        $this->topY = (float) $matches[2];
52
        $this->bottomX = (float) $matches[3];
53
        $this->bottomY = (float) $matches[4];
54
    }
55
56
    /**
57
     * __toString
58
     *
59
     * Return a string representation of Box.
60
     *
61
     * @return string
62
     */
63
    public function __toString()
64
    {
65
        return sprintf(
66
            "((%s,%s),(%s,%s))",
67
            $this->topX,
68
            $this->topY,
69
            $this->bottomX,
70
            $this->bottomY
71
        );
72
    }
73
}
74