Circle   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 2
A createPointFrom() 0 4 1
A __toString() 0 8 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
 * Circle
14
 *
15
 * PHP representation of PostgreSQL circle type.
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
class Circle
23
{
24
    public $center;
25
    public $radius;
26
27
    /**
28
     * __construct
29
     *
30
     * Create a circle from a description string.
31
     *
32
     * @param  string $description
33
     * @throws \InvalidArgumentException
34
     */
35
    public function __construct($description)
36
    {
37
        $description = trim($description, ' <>');
38
        $elts = preg_split(
39
            '/[,\s]*(\([^\)]+\))[,\s]*|[,\s]+/',
40
            $description,
41
            0,
42
            PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
43
        );
44
45
        if (count($elts) !== 2) {
46
            throw new \InvalidArgumentException(
47
                sprintf(
48
                    "Could not parse circle description '%s'.",
49
                    $description
50
                )
51
            );
52
        }
53
54
        $this->center = $this->createPointFrom($elts[0]);
55
        $this->radius = (float) $elts[1];
56
    }
57
58
    /**
59
     * createPointFrom
60
     *
61
     * Create a point from a description.
62
     *
63
     * @param  string $description
64
     * @return Point
65
     */
66
    protected function createPointFrom($description)
67
    {
68
        return new Point($description);
69
    }
70
71
    /**
72
     * __toString
73
     *
74
     * Create a string representation of the Circle.
75
     * Actually, it dumps a SQL compatible circle representation.
76
     *
77
     * @return string
78
     */
79
    public function __toString()
80
    {
81
        return sprintf(
82
            "<%s,%s>",
83
            (string) $this->center,
84
            $this->radius
85
        );
86
    }
87
}
88