1: <?php
2: namespace NGS;
3:
4: use NGS\Location;
5: use NGS\Converter\PrimitiveConverter;
6:
7: class Point extends Location
8: {
9: public function __construct($x=0, $y=0)
10: {
11: if (is_string($x) && strpos($x, ',')) {
12: $parts = explode(',', $x);
13: if (count($parts) !== 2) {
14: throw new \InvalidArgumentException('Cannot construct point from invalid format: "'.$x.'"');
15: }
16: if (filter_var($parts[0], FILTER_VALIDATE_INT) === false
17: || filter_var($parts[1], FILTER_VALIDATE_INT) === false) {
18: throw new \InvalidArgumentException('Cannot construct point from invalid format: "'.$x.'"');
19: }
20: $this->setX($parts[0]);
21: $this->setY($parts[1]);
22: } else {
23: parent::__construct($x, $y);
24: }
25: }
26:
27: 28: 29: 30: 31: 32: 33:
34: public static function toArray(array $items, $allowNullValues=false)
35: {
36: $results = array();
37: try {
38: foreach ($items as $key => $val) {
39: if ($allowNullValues && $val===null) {
40: $results[] = null;
41: } elseif ($val === null) {
42: throw new \InvalidArgumentException('Null value found in provided array');
43: } elseif (!$val instanceof \NGS\Point) {
44: $results[] = new \NGS\Point($val);
45: } else {
46: $results[] = $val;
47: }
48: }
49: }
50: catch(\Exception $e) {
51: throw new \InvalidArgumentException('Element at index '.$key.' could not be converted to Location: '.$e->getMessage(), 42, $e);
52: }
53: return $results;
54: }
55:
56: public function setX($value)
57: {
58: $this->x = PrimitiveConverter::toInteger($value);
59: }
60:
61: public function setY($value)
62: {
63: $this->y = PrimitiveConverter::toInteger($value);
64: }
65:
66: public function __toString()
67: {
68: return "{$this->x},{$this->y}";
69: }
70: }
71: