Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
arrayconverter.php
1<?php
2
4
12
14{
19 public function read($input): ?BaseGeometry
20 {
21 if (
22 !is_array($input)
23 || !isset($input['type'])
24 || !is_string($input['type'])
25 )
26 {
27 return null;
28 }
29
30 $method = 'arrayTo' . $input['type'];
31
32 return method_exists($this, $method)
33 ? $this->$method($input['coordinates'])
34 : null;
35 }
36
40 public function write(BaseGeometry $geometry)
41 {
42 return $this->getArray($geometry);
43 }
44
49 private function getArray($geometry): array
50 {
51 return [
52 'type' => $geometry->getGeometryType(),
53 'coordinates' => $geometry->asArray(),
54 ];
55 }
56
61 private function arrayToPoint(array $array): Point
62 {
63 return new Point($array[1], $array[0]);
64 }
65
70 private function arrayToLineString(array $array): LineString
71 {
72 $points = [];
73
74 foreach ($array as $item)
75 {
76 $points[] = $this->arrayToPoint($item);
77 }
78
79 return new LineString($points);
80 }
81
86 private function arrayToPolygon(array $array): Polygon
87 {
88 $lines = [];
89
90 foreach ($array as $item)
91 {
92 $lines[] = $this->arrayToLineString($item);
93 }
94
95 return new Polygon($lines);
96 }
97
102 private function arrayToMultiPoint(array $array): MultiPoint
103 {
104 $points = [];
105
106 foreach ($array as $item)
107 {
108 $points[] = $this->arrayToPoint($item);
109 }
110
111 return new MultiPoint($points);
112 }
113
118 private function arrayToMultiLineString(array $array): MultiLineString
119 {
120 $lines = [];
121
122 foreach ($array as $item)
123 {
124 $lines[] = $this->arrayToLineString($item);
125 }
126
127 return new MultiLineString($lines);
128 }
129
134 private function arrayToMultiPolygon(array $array): MultiPolygon
135 {
136 $polys = [];
137
138 foreach ($array as $item)
139 {
140 $polys[] = $this->arrayToPolygon($item);
141 }
142
143 return new MultiPolygon($polys);
144 }
145}