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