@@ -49,6 +49,9 @@ discard block |
||
49 | 49 | return $this->geometryToKML($geometry); |
50 | 50 | } |
51 | 51 | |
52 | + /** |
|
53 | + * @param string $text |
|
54 | + */ |
|
52 | 55 | public function geomFromText($text) { |
53 | 56 | |
54 | 57 | // Change to lower-case and strip all CDATA |
@@ -184,6 +187,9 @@ discard block |
||
184 | 187 | return $coordinates; |
185 | 188 | } |
186 | 189 | |
190 | + /** |
|
191 | + * @param Geometry $geom |
|
192 | + */ |
|
187 | 193 | private function geometryToKML($geom) { |
188 | 194 | $type = strtolower($geom->getGeomType()); |
189 | 195 | switch ($type) { |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @return Geometry|GeometryCollection |
32 | 32 | */ |
33 | 33 | public function read($kml) { |
34 | - return $this->geomFromText($kml); |
|
34 | + return $this->geomFromText($kml); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -42,218 +42,218 @@ discard block |
||
42 | 42 | * @return string The KML string representation of the input geometries |
43 | 43 | */ |
44 | 44 | public function write(Geometry $geometry, $namespace = FALSE) { |
45 | - if ($namespace) { |
|
46 | - $this->namespace = $namespace; |
|
47 | - $this->nss = $namespace.':'; |
|
48 | - } |
|
49 | - return $this->geometryToKML($geometry); |
|
45 | + if ($namespace) { |
|
46 | + $this->namespace = $namespace; |
|
47 | + $this->nss = $namespace.':'; |
|
48 | + } |
|
49 | + return $this->geometryToKML($geometry); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | public function geomFromText($text) { |
53 | 53 | |
54 | - // Change to lower-case and strip all CDATA |
|
55 | - $text = mb_strtolower($text, mb_detect_encoding($text)); |
|
56 | - $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
57 | - |
|
58 | - // Load into DOMDOcument |
|
59 | - $xmlobj = new DOMDocument(); |
|
60 | - @$xmlobj->loadXML($text); |
|
61 | - if ($xmlobj === false) { |
|
62 | - throw new Exception("Invalid KML: ". $text); |
|
63 | - } |
|
64 | - |
|
65 | - $this->xmlobj = $xmlobj; |
|
66 | - try { |
|
67 | - $geom = $this->geomFromXML(); |
|
68 | - } catch(InvalidText $e) { |
|
69 | - throw new Exception("Cannot Read Geometry From KML: ". $text); |
|
70 | - } catch(Exception $e) { |
|
71 | - throw $e; |
|
72 | - } |
|
73 | - |
|
74 | - return $geom; |
|
54 | + // Change to lower-case and strip all CDATA |
|
55 | + $text = mb_strtolower($text, mb_detect_encoding($text)); |
|
56 | + $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
57 | + |
|
58 | + // Load into DOMDOcument |
|
59 | + $xmlobj = new DOMDocument(); |
|
60 | + @$xmlobj->loadXML($text); |
|
61 | + if ($xmlobj === false) { |
|
62 | + throw new Exception("Invalid KML: ". $text); |
|
63 | + } |
|
64 | + |
|
65 | + $this->xmlobj = $xmlobj; |
|
66 | + try { |
|
67 | + $geom = $this->geomFromXML(); |
|
68 | + } catch(InvalidText $e) { |
|
69 | + throw new Exception("Cannot Read Geometry From KML: ". $text); |
|
70 | + } catch(Exception $e) { |
|
71 | + throw $e; |
|
72 | + } |
|
73 | + |
|
74 | + return $geom; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | protected function geomFromXML() { |
78 | - $geometries = array(); |
|
79 | - $geom_types = geoPHP::geometryList(); |
|
80 | - $placemark_elements = $this->xmlobj->getElementsByTagName('placemark'); |
|
81 | - if ($placemark_elements->length) { |
|
82 | - foreach ($placemark_elements as $placemark) { |
|
83 | - foreach ($placemark->childNodes as $child) { |
|
84 | - // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection |
|
85 | - $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; |
|
86 | - if (array_key_exists($node_name, $geom_types)) { |
|
87 | - $function = 'parse'.$geom_types[$node_name]; |
|
88 | - $geometries[] = $this->$function($child); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - else { |
|
94 | - // The document does not have a placemark, try to create a valid geometry from the root element |
|
95 | - $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; |
|
96 | - if (array_key_exists($node_name, $geom_types)) { |
|
97 | - $function = 'parse'.$geom_types[$node_name]; |
|
98 | - $geometries[] = $this->$function($this->xmlobj->documentElement); |
|
99 | - } |
|
100 | - } |
|
101 | - return geoPHP::geometryReduce($geometries); |
|
78 | + $geometries = array(); |
|
79 | + $geom_types = geoPHP::geometryList(); |
|
80 | + $placemark_elements = $this->xmlobj->getElementsByTagName('placemark'); |
|
81 | + if ($placemark_elements->length) { |
|
82 | + foreach ($placemark_elements as $placemark) { |
|
83 | + foreach ($placemark->childNodes as $child) { |
|
84 | + // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection |
|
85 | + $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; |
|
86 | + if (array_key_exists($node_name, $geom_types)) { |
|
87 | + $function = 'parse'.$geom_types[$node_name]; |
|
88 | + $geometries[] = $this->$function($child); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + else { |
|
94 | + // The document does not have a placemark, try to create a valid geometry from the root element |
|
95 | + $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; |
|
96 | + if (array_key_exists($node_name, $geom_types)) { |
|
97 | + $function = 'parse'.$geom_types[$node_name]; |
|
98 | + $geometries[] = $this->$function($this->xmlobj->documentElement); |
|
99 | + } |
|
100 | + } |
|
101 | + return geoPHP::geometryReduce($geometries); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | protected function childElements($xml, $nodename = '') { |
105 | - $children = array(); |
|
106 | - if ($xml->childNodes) { |
|
107 | - foreach ($xml->childNodes as $child) { |
|
108 | - if ($child->nodeName == $nodename) { |
|
109 | - $children[] = $child; |
|
110 | - } |
|
111 | - } |
|
112 | - } |
|
113 | - return $children; |
|
105 | + $children = array(); |
|
106 | + if ($xml->childNodes) { |
|
107 | + foreach ($xml->childNodes as $child) { |
|
108 | + if ($child->nodeName == $nodename) { |
|
109 | + $children[] = $child; |
|
110 | + } |
|
111 | + } |
|
112 | + } |
|
113 | + return $children; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | protected function parsePoint($xml) { |
117 | - $coordinates = $this->_extractCoordinates($xml); |
|
118 | - return new Point($coordinates[0][0],$coordinates[0][1]); |
|
117 | + $coordinates = $this->_extractCoordinates($xml); |
|
118 | + return new Point($coordinates[0][0],$coordinates[0][1]); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | protected function parseLineString($xml) { |
122 | - $coordinates = $this->_extractCoordinates($xml); |
|
123 | - $point_array = array(); |
|
124 | - foreach ($coordinates as $set) { |
|
125 | - $point_array[] = new Point($set[0],$set[1]); |
|
126 | - } |
|
127 | - return new LineString($point_array); |
|
122 | + $coordinates = $this->_extractCoordinates($xml); |
|
123 | + $point_array = array(); |
|
124 | + foreach ($coordinates as $set) { |
|
125 | + $point_array[] = new Point($set[0],$set[1]); |
|
126 | + } |
|
127 | + return new LineString($point_array); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | protected function parsePolygon($xml) { |
131 | - $components = array(); |
|
132 | - |
|
133 | - $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); |
|
134 | - $outer_boundary_element = $outer_boundary_element_a[0]; |
|
135 | - $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); |
|
136 | - $outer_ring_element = $outer_ring_element_a[0]; |
|
137 | - $components[] = $this->parseLineString($outer_ring_element); |
|
138 | - |
|
139 | - if (count($components) != 1) { |
|
140 | - throw new Exception("Invalid KML"); |
|
141 | - } |
|
142 | - |
|
143 | - $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); |
|
144 | - if (count($inner_boundary_element_a)) { |
|
145 | - foreach ($inner_boundary_element_a as $inner_boundary_element) { |
|
146 | - foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) { |
|
147 | - $components[] = $this->parseLineString($inner_ring_element); |
|
148 | - } |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - return new Polygon($components); |
|
131 | + $components = array(); |
|
132 | + |
|
133 | + $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); |
|
134 | + $outer_boundary_element = $outer_boundary_element_a[0]; |
|
135 | + $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); |
|
136 | + $outer_ring_element = $outer_ring_element_a[0]; |
|
137 | + $components[] = $this->parseLineString($outer_ring_element); |
|
138 | + |
|
139 | + if (count($components) != 1) { |
|
140 | + throw new Exception("Invalid KML"); |
|
141 | + } |
|
142 | + |
|
143 | + $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); |
|
144 | + if (count($inner_boundary_element_a)) { |
|
145 | + foreach ($inner_boundary_element_a as $inner_boundary_element) { |
|
146 | + foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) { |
|
147 | + $components[] = $this->parseLineString($inner_ring_element); |
|
148 | + } |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + return new Polygon($components); |
|
153 | 153 | } |
154 | 154 | |
155 | 155 | protected function parseGeometryCollection($xml) { |
156 | - $components = array(); |
|
157 | - $geom_types = geoPHP::geometryList(); |
|
158 | - foreach ($xml->childNodes as $child) { |
|
159 | - $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; |
|
160 | - if (array_key_exists($nodeName, $geom_types)) { |
|
161 | - $function = 'parse'.$geom_types[$nodeName]; |
|
162 | - $components[] = $this->$function($child); |
|
163 | - } |
|
164 | - } |
|
165 | - return new GeometryCollection($components); |
|
156 | + $components = array(); |
|
157 | + $geom_types = geoPHP::geometryList(); |
|
158 | + foreach ($xml->childNodes as $child) { |
|
159 | + $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; |
|
160 | + if (array_key_exists($nodeName, $geom_types)) { |
|
161 | + $function = 'parse'.$geom_types[$nodeName]; |
|
162 | + $components[] = $this->$function($child); |
|
163 | + } |
|
164 | + } |
|
165 | + return new GeometryCollection($components); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | protected function _extractCoordinates($xml) { |
169 | - $coord_elements = $this->childElements($xml, 'coordinates'); |
|
170 | - $coordinates = array(); |
|
171 | - if (count($coord_elements)) { |
|
172 | - $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue)); |
|
173 | - foreach ($coord_sets as $set_string) { |
|
174 | - $set_string = trim($set_string); |
|
175 | - if ($set_string) { |
|
176 | - $set_array = explode(',',$set_string); |
|
177 | - if (count($set_array) >= 2) { |
|
178 | - $coordinates[] = $set_array; |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - return $coordinates; |
|
169 | + $coord_elements = $this->childElements($xml, 'coordinates'); |
|
170 | + $coordinates = array(); |
|
171 | + if (count($coord_elements)) { |
|
172 | + $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue)); |
|
173 | + foreach ($coord_sets as $set_string) { |
|
174 | + $set_string = trim($set_string); |
|
175 | + if ($set_string) { |
|
176 | + $set_array = explode(',',$set_string); |
|
177 | + if (count($set_array) >= 2) { |
|
178 | + $coordinates[] = $set_array; |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + return $coordinates; |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | private function geometryToKML($geom) { |
188 | - $type = strtolower($geom->getGeomType()); |
|
189 | - switch ($type) { |
|
190 | - case 'point': |
|
191 | - return $this->pointToKML($geom); |
|
192 | - break; |
|
193 | - case 'linestring': |
|
194 | - return $this->linestringToKML($geom); |
|
195 | - break; |
|
196 | - case 'polygon': |
|
197 | - return $this->polygonToKML($geom); |
|
198 | - break; |
|
199 | - case 'multipoint': |
|
200 | - case 'multilinestring': |
|
201 | - case 'multipolygon': |
|
202 | - case 'geometrycollection': |
|
203 | - return $this->collectionToKML($geom); |
|
204 | - break; |
|
205 | - } |
|
188 | + $type = strtolower($geom->getGeomType()); |
|
189 | + switch ($type) { |
|
190 | + case 'point': |
|
191 | + return $this->pointToKML($geom); |
|
192 | + break; |
|
193 | + case 'linestring': |
|
194 | + return $this->linestringToKML($geom); |
|
195 | + break; |
|
196 | + case 'polygon': |
|
197 | + return $this->polygonToKML($geom); |
|
198 | + break; |
|
199 | + case 'multipoint': |
|
200 | + case 'multilinestring': |
|
201 | + case 'multipolygon': |
|
202 | + case 'geometrycollection': |
|
203 | + return $this->collectionToKML($geom); |
|
204 | + break; |
|
205 | + } |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | private function pointToKML($geom) { |
209 | - return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; |
|
209 | + return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | private function linestringToKML($geom, $type = FALSE) { |
213 | - if (!$type) { |
|
214 | - $type = $geom->getGeomType(); |
|
215 | - } |
|
213 | + if (!$type) { |
|
214 | + $type = $geom->getGeomType(); |
|
215 | + } |
|
216 | 216 | |
217 | - $str = '<'.$this->nss . $type .'>'; |
|
217 | + $str = '<'.$this->nss . $type .'>'; |
|
218 | 218 | |
219 | - if (!$geom->isEmpty()) { |
|
220 | - $str .= '<'.$this->nss.'coordinates>'; |
|
221 | - $i=0; |
|
222 | - foreach ($geom->getComponents() as $comp) { |
|
223 | - if ($i != 0) $str .= ' '; |
|
224 | - $str .= $comp->getX() .','. $comp->getY(); |
|
225 | - $i++; |
|
226 | - } |
|
219 | + if (!$geom->isEmpty()) { |
|
220 | + $str .= '<'.$this->nss.'coordinates>'; |
|
221 | + $i=0; |
|
222 | + foreach ($geom->getComponents() as $comp) { |
|
223 | + if ($i != 0) $str .= ' '; |
|
224 | + $str .= $comp->getX() .','. $comp->getY(); |
|
225 | + $i++; |
|
226 | + } |
|
227 | 227 | |
228 | - $str .= '</'.$this->nss.'coordinates>'; |
|
229 | - } |
|
228 | + $str .= '</'.$this->nss.'coordinates>'; |
|
229 | + } |
|
230 | 230 | |
231 | - $str .= '</'. $this->nss . $type .'>'; |
|
231 | + $str .= '</'. $this->nss . $type .'>'; |
|
232 | 232 | |
233 | - return $str; |
|
233 | + return $str; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | public function polygonToKML($geom) { |
237 | - $components = $geom->getComponents(); |
|
238 | - if (!empty($components)) { |
|
239 | - $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; |
|
240 | - foreach (array_slice($components, 1) as $comp) { |
|
241 | - $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; |
|
237 | + $components = $geom->getComponents(); |
|
238 | + if (!empty($components)) { |
|
239 | + $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; |
|
240 | + foreach (array_slice($components, 1) as $comp) { |
|
241 | + $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | public function collectionToKML($geom) { |
249 | - $components = $geom->getComponents(); |
|
250 | - $str = '<'.$this->nss.'MultiGeometry>'; |
|
251 | - foreach ($geom->getComponents() as $comp) { |
|
252 | - $sub_adapter = new KML(); |
|
253 | - $str .= $sub_adapter->write($comp); |
|
254 | - } |
|
255 | - |
|
256 | - return $str .'</'.$this->nss.'MultiGeometry>'; |
|
249 | + $components = $geom->getComponents(); |
|
250 | + $str = '<'.$this->nss.'MultiGeometry>'; |
|
251 | + foreach ($geom->getComponents() as $comp) { |
|
252 | + $sub_adapter = new KML(); |
|
253 | + $str .= $sub_adapter->write($comp); |
|
254 | + } |
|
255 | + |
|
256 | + return $str .'</'.$this->nss.'MultiGeometry>'; |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | } |
@@ -53,21 +53,21 @@ discard block |
||
53 | 53 | |
54 | 54 | // Change to lower-case and strip all CDATA |
55 | 55 | $text = mb_strtolower($text, mb_detect_encoding($text)); |
56 | - $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
56 | + $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s', '', $text); |
|
57 | 57 | |
58 | 58 | // Load into DOMDOcument |
59 | 59 | $xmlobj = new DOMDocument(); |
60 | 60 | @$xmlobj->loadXML($text); |
61 | 61 | if ($xmlobj === false) { |
62 | - throw new Exception("Invalid KML: ". $text); |
|
62 | + throw new Exception("Invalid KML: ".$text); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | $this->xmlobj = $xmlobj; |
66 | 66 | try { |
67 | 67 | $geom = $this->geomFromXML(); |
68 | - } catch(InvalidText $e) { |
|
69 | - throw new Exception("Cannot Read Geometry From KML: ". $text); |
|
70 | - } catch(Exception $e) { |
|
68 | + } catch (InvalidText $e) { |
|
69 | + throw new Exception("Cannot Read Geometry From KML: ".$text); |
|
70 | + } catch (Exception $e) { |
|
71 | 71 | throw $e; |
72 | 72 | } |
73 | 73 | |
@@ -115,14 +115,14 @@ discard block |
||
115 | 115 | |
116 | 116 | protected function parsePoint($xml) { |
117 | 117 | $coordinates = $this->_extractCoordinates($xml); |
118 | - return new Point($coordinates[0][0],$coordinates[0][1]); |
|
118 | + return new Point($coordinates[0][0], $coordinates[0][1]); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | protected function parseLineString($xml) { |
122 | 122 | $coordinates = $this->_extractCoordinates($xml); |
123 | 123 | $point_array = array(); |
124 | 124 | foreach ($coordinates as $set) { |
125 | - $point_array[] = new Point($set[0],$set[1]); |
|
125 | + $point_array[] = new Point($set[0], $set[1]); |
|
126 | 126 | } |
127 | 127 | return new LineString($point_array); |
128 | 128 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | foreach ($coord_sets as $set_string) { |
174 | 174 | $set_string = trim($set_string); |
175 | 175 | if ($set_string) { |
176 | - $set_array = explode(',',$set_string); |
|
176 | + $set_array = explode(',', $set_string); |
|
177 | 177 | if (count($set_array) >= 2) { |
178 | 178 | $coordinates[] = $set_array; |
179 | 179 | } |
@@ -214,21 +214,21 @@ discard block |
||
214 | 214 | $type = $geom->getGeomType(); |
215 | 215 | } |
216 | 216 | |
217 | - $str = '<'.$this->nss . $type .'>'; |
|
217 | + $str = '<'.$this->nss.$type.'>'; |
|
218 | 218 | |
219 | 219 | if (!$geom->isEmpty()) { |
220 | 220 | $str .= '<'.$this->nss.'coordinates>'; |
221 | - $i=0; |
|
221 | + $i = 0; |
|
222 | 222 | foreach ($geom->getComponents() as $comp) { |
223 | 223 | if ($i != 0) $str .= ' '; |
224 | - $str .= $comp->getX() .','. $comp->getY(); |
|
224 | + $str .= $comp->getX().','.$comp->getY(); |
|
225 | 225 | $i++; |
226 | 226 | } |
227 | 227 | |
228 | 228 | $str .= '</'.$this->nss.'coordinates>'; |
229 | 229 | } |
230 | 230 | |
231 | - $str .= '</'. $this->nss . $type .'>'; |
|
231 | + $str .= '</'.$this->nss.$type.'>'; |
|
232 | 232 | |
233 | 233 | return $str; |
234 | 234 | } |
@@ -236,13 +236,13 @@ discard block |
||
236 | 236 | public function polygonToKML($geom) { |
237 | 237 | $components = $geom->getComponents(); |
238 | 238 | if (!empty($components)) { |
239 | - $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; |
|
239 | + $str = '<'.$this->nss.'outerBoundaryIs>'.$this->linestringToKML($components[0], 'LinearRing').'</'.$this->nss.'outerBoundaryIs>'; |
|
240 | 240 | foreach (array_slice($components, 1) as $comp) { |
241 | - $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; |
|
241 | + $str .= '<'.$this->nss.'innerBoundaryIs>'.$this->linestringToKML($comp).'</'.$this->nss.'innerBoundaryIs>'; |
|
242 | 242 | } |
243 | 243 | } |
244 | 244 | |
245 | - return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; |
|
245 | + return '<'.$this->nss.'Polygon>'.$str.'</'.$this->nss.'Polygon>'; |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | public function collectionToKML($geom) { |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $str .= $sub_adapter->write($comp); |
254 | 254 | } |
255 | 255 | |
256 | - return $str .'</'.$this->nss.'MultiGeometry>'; |
|
256 | + return $str.'</'.$this->nss.'MultiGeometry>'; |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | } |
@@ -89,8 +89,7 @@ discard block |
||
89 | 89 | } |
90 | 90 | } |
91 | 91 | } |
92 | - } |
|
93 | - else { |
|
92 | + } else { |
|
94 | 93 | // The document does not have a placemark, try to create a valid geometry from the root element |
95 | 94 | $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; |
96 | 95 | if (array_key_exists($node_name, $geom_types)) { |
@@ -220,7 +219,9 @@ discard block |
||
220 | 219 | $str .= '<'.$this->nss.'coordinates>'; |
221 | 220 | $i=0; |
222 | 221 | foreach ($geom->getComponents() as $comp) { |
223 | - if ($i != 0) $str .= ' '; |
|
222 | + if ($i != 0) { |
|
223 | + $str .= ' '; |
|
224 | + } |
|
224 | 225 | $str .= $comp->getX() .','. $comp->getY(); |
225 | 226 | $i++; |
226 | 227 | } |
@@ -123,6 +123,9 @@ discard block |
||
123 | 123 | return new Polygon($components); |
124 | 124 | } |
125 | 125 | |
126 | + /** |
|
127 | + * @param string $type |
|
128 | + */ |
|
126 | 129 | function getMulti(&$mem, $type) { |
127 | 130 | // Get the number of items expected in this multi out of the first 4 bytes |
128 | 131 | $multi_length = unpack('L',fread($mem,4)); |
@@ -196,6 +199,9 @@ discard block |
||
196 | 199 | } |
197 | 200 | } |
198 | 201 | |
202 | + /** |
|
203 | + * @param Geometry $point |
|
204 | + */ |
|
199 | 205 | function writePoint($point) { |
200 | 206 | // Set the coords |
201 | 207 | $wkb = pack('dd',$point->x(), $point->y()); |
@@ -215,6 +221,9 @@ discard block |
||
215 | 221 | return $wkb; |
216 | 222 | } |
217 | 223 | |
224 | + /** |
|
225 | + * @param Geometry $poly |
|
226 | + */ |
|
218 | 227 | function writePolygon($poly) { |
219 | 228 | // Set the number of lines in this poly |
220 | 229 | $wkb = pack('L',$poly->numGeometries()); |
@@ -227,6 +236,9 @@ discard block |
||
227 | 236 | return $wkb; |
228 | 237 | } |
229 | 238 | |
239 | + /** |
|
240 | + * @param Geometry $geometry |
|
241 | + */ |
|
230 | 242 | function writeMulti($geometry) { |
231 | 243 | // Set the number of components |
232 | 244 | $wkb = pack('L',$geometry->numGeometries()); |
@@ -29,120 +29,120 @@ discard block |
||
29 | 29 | * @return Geometry |
30 | 30 | */ |
31 | 31 | public function read($wkb, $is_hex_string = FALSE) { |
32 | - if ($is_hex_string) { |
|
33 | - $wkb = pack('H*',$wkb); |
|
34 | - } |
|
32 | + if ($is_hex_string) { |
|
33 | + $wkb = pack('H*',$wkb); |
|
34 | + } |
|
35 | 35 | |
36 | - if (empty($wkb)) { |
|
37 | - throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb)); |
|
38 | - } |
|
36 | + if (empty($wkb)) { |
|
37 | + throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb)); |
|
38 | + } |
|
39 | 39 | |
40 | - $mem = fopen('php://memory', 'r+'); |
|
41 | - fwrite($mem, $wkb); |
|
42 | - fseek($mem, 0); |
|
40 | + $mem = fopen('php://memory', 'r+'); |
|
41 | + fwrite($mem, $wkb); |
|
42 | + fseek($mem, 0); |
|
43 | 43 | |
44 | - $geometry = $this->getGeometry($mem); |
|
45 | - fclose($mem); |
|
46 | - return $geometry; |
|
44 | + $geometry = $this->getGeometry($mem); |
|
45 | + fclose($mem); |
|
46 | + return $geometry; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | function getGeometry(&$mem) { |
50 | - $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
51 | - if ($base_info['order'] !== 1) { |
|
52 | - throw new Exception('Only NDR (little endian) SKB format is supported at the moment'); |
|
53 | - } |
|
54 | - |
|
55 | - if ($base_info['z']) { |
|
56 | - $this->dimension++; |
|
57 | - $this->z = TRUE; |
|
58 | - } |
|
59 | - if ($base_info['m']) { |
|
60 | - $this->dimension++; |
|
61 | - $this->m = TRUE; |
|
62 | - } |
|
63 | - |
|
64 | - // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
65 | - if ($base_info['s']) { |
|
66 | - fread($mem, 4); |
|
67 | - } |
|
68 | - |
|
69 | - switch ($base_info['type']) { |
|
70 | - case 1: |
|
71 | - return $this->getPoint($mem); |
|
72 | - case 2: |
|
73 | - return $this->getLinstring($mem); |
|
74 | - case 3: |
|
75 | - return $this->getPolygon($mem); |
|
76 | - case 4: |
|
77 | - return $this->getMulti($mem,'point'); |
|
78 | - case 5: |
|
79 | - return $this->getMulti($mem,'line'); |
|
80 | - case 6: |
|
81 | - return $this->getMulti($mem,'polygon'); |
|
82 | - case 7: |
|
83 | - return $this->getMulti($mem,'geometry'); |
|
84 | - } |
|
50 | + $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
51 | + if ($base_info['order'] !== 1) { |
|
52 | + throw new Exception('Only NDR (little endian) SKB format is supported at the moment'); |
|
53 | + } |
|
54 | + |
|
55 | + if ($base_info['z']) { |
|
56 | + $this->dimension++; |
|
57 | + $this->z = TRUE; |
|
58 | + } |
|
59 | + if ($base_info['m']) { |
|
60 | + $this->dimension++; |
|
61 | + $this->m = TRUE; |
|
62 | + } |
|
63 | + |
|
64 | + // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
65 | + if ($base_info['s']) { |
|
66 | + fread($mem, 4); |
|
67 | + } |
|
68 | + |
|
69 | + switch ($base_info['type']) { |
|
70 | + case 1: |
|
71 | + return $this->getPoint($mem); |
|
72 | + case 2: |
|
73 | + return $this->getLinstring($mem); |
|
74 | + case 3: |
|
75 | + return $this->getPolygon($mem); |
|
76 | + case 4: |
|
77 | + return $this->getMulti($mem,'point'); |
|
78 | + case 5: |
|
79 | + return $this->getMulti($mem,'line'); |
|
80 | + case 6: |
|
81 | + return $this->getMulti($mem,'polygon'); |
|
82 | + case 7: |
|
83 | + return $this->getMulti($mem,'geometry'); |
|
84 | + } |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | function getPoint(&$mem) { |
88 | - $point_coords = unpack("d*", fread($mem,$this->dimension*8)); |
|
89 | - return new Point($point_coords[1],$point_coords[2]); |
|
88 | + $point_coords = unpack("d*", fread($mem,$this->dimension*8)); |
|
89 | + return new Point($point_coords[1],$point_coords[2]); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | function getLinstring(&$mem) { |
93 | - // Get the number of points expected in this string out of the first 4 bytes |
|
94 | - $line_length = unpack('L',fread($mem,4)); |
|
95 | - |
|
96 | - // Return an empty linestring if there is no line-length |
|
97 | - if (!$line_length[1]) return new LineString(); |
|
98 | - |
|
99 | - // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
100 | - $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
|
101 | - |
|
102 | - // We have our coords, build up the linestring |
|
103 | - $components = array(); |
|
104 | - $i = 1; |
|
105 | - $num_coords = count($line_coords); |
|
106 | - while ($i <= $num_coords) { |
|
107 | - $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
108 | - $i += 2; |
|
109 | - } |
|
110 | - return new LineString($components); |
|
93 | + // Get the number of points expected in this string out of the first 4 bytes |
|
94 | + $line_length = unpack('L',fread($mem,4)); |
|
95 | + |
|
96 | + // Return an empty linestring if there is no line-length |
|
97 | + if (!$line_length[1]) return new LineString(); |
|
98 | + |
|
99 | + // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
100 | + $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
|
101 | + |
|
102 | + // We have our coords, build up the linestring |
|
103 | + $components = array(); |
|
104 | + $i = 1; |
|
105 | + $num_coords = count($line_coords); |
|
106 | + while ($i <= $num_coords) { |
|
107 | + $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
108 | + $i += 2; |
|
109 | + } |
|
110 | + return new LineString($components); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | function getPolygon(&$mem) { |
114 | - // Get the number of linestring expected in this poly out of the first 4 bytes |
|
115 | - $poly_length = unpack('L',fread($mem,4)); |
|
116 | - |
|
117 | - $components = array(); |
|
118 | - $i = 1; |
|
119 | - while ($i <= $poly_length[1]) { |
|
120 | - $components[] = $this->getLinstring($mem); |
|
121 | - $i++; |
|
122 | - } |
|
123 | - return new Polygon($components); |
|
114 | + // Get the number of linestring expected in this poly out of the first 4 bytes |
|
115 | + $poly_length = unpack('L',fread($mem,4)); |
|
116 | + |
|
117 | + $components = array(); |
|
118 | + $i = 1; |
|
119 | + while ($i <= $poly_length[1]) { |
|
120 | + $components[] = $this->getLinstring($mem); |
|
121 | + $i++; |
|
122 | + } |
|
123 | + return new Polygon($components); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | function getMulti(&$mem, $type) { |
127 | - // Get the number of items expected in this multi out of the first 4 bytes |
|
128 | - $multi_length = unpack('L',fread($mem,4)); |
|
129 | - |
|
130 | - $components = array(); |
|
131 | - $i = 1; |
|
132 | - while ($i <= $multi_length[1]) { |
|
133 | - $components[] = $this->getGeometry($mem); |
|
134 | - $i++; |
|
135 | - } |
|
136 | - switch ($type) { |
|
137 | - case 'point': |
|
138 | - return new MultiPoint($components); |
|
139 | - case 'line': |
|
140 | - return new MultiLineString($components); |
|
141 | - case 'polygon': |
|
142 | - return new MultiPolygon($components); |
|
143 | - case 'geometry': |
|
144 | - return new GeometryCollection($components); |
|
145 | - } |
|
127 | + // Get the number of items expected in this multi out of the first 4 bytes |
|
128 | + $multi_length = unpack('L',fread($mem,4)); |
|
129 | + |
|
130 | + $components = array(); |
|
131 | + $i = 1; |
|
132 | + while ($i <= $multi_length[1]) { |
|
133 | + $components[] = $this->getGeometry($mem); |
|
134 | + $i++; |
|
135 | + } |
|
136 | + switch ($type) { |
|
137 | + case 'point': |
|
138 | + return new MultiPoint($components); |
|
139 | + case 'line': |
|
140 | + return new MultiLineString($components); |
|
141 | + case 'polygon': |
|
142 | + return new MultiPolygon($components); |
|
143 | + case 'geometry': |
|
144 | + return new GeometryCollection($components); |
|
145 | + } |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -153,90 +153,90 @@ discard block |
||
153 | 153 | * @return string The WKB string representation of the input geometries |
154 | 154 | */ |
155 | 155 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
156 | - // We always write into NDR (little endian) |
|
157 | - $wkb = pack('c',1); |
|
158 | - |
|
159 | - switch ($geometry->getGeomType()) { |
|
160 | - case 'Point'; |
|
161 | - $wkb .= pack('L',1); |
|
162 | - $wkb .= $this->writePoint($geometry); |
|
163 | - break; |
|
164 | - case 'LineString'; |
|
165 | - $wkb .= pack('L',2); |
|
166 | - $wkb .= $this->writeLineString($geometry); |
|
167 | - break; |
|
168 | - case 'Polygon'; |
|
169 | - $wkb .= pack('L',3); |
|
170 | - $wkb .= $this->writePolygon($geometry); |
|
171 | - break; |
|
172 | - case 'MultiPoint'; |
|
173 | - $wkb .= pack('L',4); |
|
174 | - $wkb .= $this->writeMulti($geometry); |
|
175 | - break; |
|
176 | - case 'MultiLineString'; |
|
177 | - $wkb .= pack('L',5); |
|
178 | - $wkb .= $this->writeMulti($geometry); |
|
179 | - break; |
|
180 | - case 'MultiPolygon'; |
|
181 | - $wkb .= pack('L',6); |
|
182 | - $wkb .= $this->writeMulti($geometry); |
|
183 | - break; |
|
184 | - case 'GeometryCollection'; |
|
185 | - $wkb .= pack('L',7); |
|
186 | - $wkb .= $this->writeMulti($geometry); |
|
187 | - break; |
|
188 | - } |
|
189 | - |
|
190 | - if ($write_as_hex) { |
|
191 | - $unpacked = unpack('H*',$wkb); |
|
192 | - return $unpacked[1]; |
|
193 | - } |
|
194 | - else { |
|
195 | - return $wkb; |
|
196 | - } |
|
156 | + // We always write into NDR (little endian) |
|
157 | + $wkb = pack('c',1); |
|
158 | + |
|
159 | + switch ($geometry->getGeomType()) { |
|
160 | + case 'Point'; |
|
161 | + $wkb .= pack('L',1); |
|
162 | + $wkb .= $this->writePoint($geometry); |
|
163 | + break; |
|
164 | + case 'LineString'; |
|
165 | + $wkb .= pack('L',2); |
|
166 | + $wkb .= $this->writeLineString($geometry); |
|
167 | + break; |
|
168 | + case 'Polygon'; |
|
169 | + $wkb .= pack('L',3); |
|
170 | + $wkb .= $this->writePolygon($geometry); |
|
171 | + break; |
|
172 | + case 'MultiPoint'; |
|
173 | + $wkb .= pack('L',4); |
|
174 | + $wkb .= $this->writeMulti($geometry); |
|
175 | + break; |
|
176 | + case 'MultiLineString'; |
|
177 | + $wkb .= pack('L',5); |
|
178 | + $wkb .= $this->writeMulti($geometry); |
|
179 | + break; |
|
180 | + case 'MultiPolygon'; |
|
181 | + $wkb .= pack('L',6); |
|
182 | + $wkb .= $this->writeMulti($geometry); |
|
183 | + break; |
|
184 | + case 'GeometryCollection'; |
|
185 | + $wkb .= pack('L',7); |
|
186 | + $wkb .= $this->writeMulti($geometry); |
|
187 | + break; |
|
188 | + } |
|
189 | + |
|
190 | + if ($write_as_hex) { |
|
191 | + $unpacked = unpack('H*',$wkb); |
|
192 | + return $unpacked[1]; |
|
193 | + } |
|
194 | + else { |
|
195 | + return $wkb; |
|
196 | + } |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | function writePoint($point) { |
200 | - // Set the coords |
|
201 | - $wkb = pack('dd',$point->x(), $point->y()); |
|
200 | + // Set the coords |
|
201 | + $wkb = pack('dd',$point->x(), $point->y()); |
|
202 | 202 | |
203 | - return $wkb; |
|
203 | + return $wkb; |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | function writeLineString($line) { |
207 | - // Set the number of points in this line |
|
208 | - $wkb = pack('L',$line->numPoints()); |
|
207 | + // Set the number of points in this line |
|
208 | + $wkb = pack('L',$line->numPoints()); |
|
209 | 209 | |
210 | - // Set the coords |
|
211 | - foreach ($line->getComponents() as $point) { |
|
212 | - $wkb .= pack('dd',$point->x(), $point->y()); |
|
213 | - } |
|
210 | + // Set the coords |
|
211 | + foreach ($line->getComponents() as $point) { |
|
212 | + $wkb .= pack('dd',$point->x(), $point->y()); |
|
213 | + } |
|
214 | 214 | |
215 | - return $wkb; |
|
215 | + return $wkb; |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | function writePolygon($poly) { |
219 | - // Set the number of lines in this poly |
|
220 | - $wkb = pack('L',$poly->numGeometries()); |
|
219 | + // Set the number of lines in this poly |
|
220 | + $wkb = pack('L',$poly->numGeometries()); |
|
221 | 221 | |
222 | - // Write the lines |
|
223 | - foreach ($poly->getComponents() as $line) { |
|
224 | - $wkb .= $this->writeLineString($line); |
|
225 | - } |
|
222 | + // Write the lines |
|
223 | + foreach ($poly->getComponents() as $line) { |
|
224 | + $wkb .= $this->writeLineString($line); |
|
225 | + } |
|
226 | 226 | |
227 | - return $wkb; |
|
227 | + return $wkb; |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | function writeMulti($geometry) { |
231 | - // Set the number of components |
|
232 | - $wkb = pack('L',$geometry->numGeometries()); |
|
231 | + // Set the number of components |
|
232 | + $wkb = pack('L',$geometry->numGeometries()); |
|
233 | 233 | |
234 | - // Write the components |
|
235 | - foreach ($geometry->getComponents() as $component) { |
|
236 | - $wkb .= $this->write($component); |
|
237 | - } |
|
234 | + // Write the components |
|
235 | + foreach ($geometry->getComponents() as $component) { |
|
236 | + $wkb .= $this->write($component); |
|
237 | + } |
|
238 | 238 | |
239 | - return $wkb; |
|
239 | + return $wkb; |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | } |
@@ -30,11 +30,11 @@ discard block |
||
30 | 30 | */ |
31 | 31 | public function read($wkb, $is_hex_string = FALSE) { |
32 | 32 | if ($is_hex_string) { |
33 | - $wkb = pack('H*',$wkb); |
|
33 | + $wkb = pack('H*', $wkb); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | if (empty($wkb)) { |
37 | - throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb)); |
|
37 | + throw new Exception('Cannot read empty WKB geometry. Found '.gettype($wkb)); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | $mem = fopen('php://memory', 'r+'); |
@@ -74,37 +74,37 @@ discard block |
||
74 | 74 | case 3: |
75 | 75 | return $this->getPolygon($mem); |
76 | 76 | case 4: |
77 | - return $this->getMulti($mem,'point'); |
|
77 | + return $this->getMulti($mem, 'point'); |
|
78 | 78 | case 5: |
79 | - return $this->getMulti($mem,'line'); |
|
79 | + return $this->getMulti($mem, 'line'); |
|
80 | 80 | case 6: |
81 | - return $this->getMulti($mem,'polygon'); |
|
81 | + return $this->getMulti($mem, 'polygon'); |
|
82 | 82 | case 7: |
83 | - return $this->getMulti($mem,'geometry'); |
|
83 | + return $this->getMulti($mem, 'geometry'); |
|
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
87 | 87 | function getPoint(&$mem) { |
88 | - $point_coords = unpack("d*", fread($mem,$this->dimension*8)); |
|
89 | - return new Point($point_coords[1],$point_coords[2]); |
|
88 | + $point_coords = unpack("d*", fread($mem, $this->dimension*8)); |
|
89 | + return new Point($point_coords[1], $point_coords[2]); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | function getLinstring(&$mem) { |
93 | 93 | // Get the number of points expected in this string out of the first 4 bytes |
94 | - $line_length = unpack('L',fread($mem,4)); |
|
94 | + $line_length = unpack('L', fread($mem, 4)); |
|
95 | 95 | |
96 | 96 | // Return an empty linestring if there is no line-length |
97 | 97 | if (!$line_length[1]) return new LineString(); |
98 | 98 | |
99 | 99 | // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
100 | - $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
|
100 | + $line_coords = unpack('d*', fread($mem, $line_length[1]*$this->dimension*8)); |
|
101 | 101 | |
102 | 102 | // We have our coords, build up the linestring |
103 | 103 | $components = array(); |
104 | 104 | $i = 1; |
105 | 105 | $num_coords = count($line_coords); |
106 | 106 | while ($i <= $num_coords) { |
107 | - $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
107 | + $components[] = new Point($line_coords[$i], $line_coords[$i + 1]); |
|
108 | 108 | $i += 2; |
109 | 109 | } |
110 | 110 | return new LineString($components); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | |
113 | 113 | function getPolygon(&$mem) { |
114 | 114 | // Get the number of linestring expected in this poly out of the first 4 bytes |
115 | - $poly_length = unpack('L',fread($mem,4)); |
|
115 | + $poly_length = unpack('L', fread($mem, 4)); |
|
116 | 116 | |
117 | 117 | $components = array(); |
118 | 118 | $i = 1; |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | |
126 | 126 | function getMulti(&$mem, $type) { |
127 | 127 | // Get the number of items expected in this multi out of the first 4 bytes |
128 | - $multi_length = unpack('L',fread($mem,4)); |
|
128 | + $multi_length = unpack('L', fread($mem, 4)); |
|
129 | 129 | |
130 | 130 | $components = array(); |
131 | 131 | $i = 1; |
@@ -154,41 +154,41 @@ discard block |
||
154 | 154 | */ |
155 | 155 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
156 | 156 | // We always write into NDR (little endian) |
157 | - $wkb = pack('c',1); |
|
157 | + $wkb = pack('c', 1); |
|
158 | 158 | |
159 | 159 | switch ($geometry->getGeomType()) { |
160 | 160 | case 'Point'; |
161 | - $wkb .= pack('L',1); |
|
161 | + $wkb .= pack('L', 1); |
|
162 | 162 | $wkb .= $this->writePoint($geometry); |
163 | 163 | break; |
164 | 164 | case 'LineString'; |
165 | - $wkb .= pack('L',2); |
|
165 | + $wkb .= pack('L', 2); |
|
166 | 166 | $wkb .= $this->writeLineString($geometry); |
167 | 167 | break; |
168 | 168 | case 'Polygon'; |
169 | - $wkb .= pack('L',3); |
|
169 | + $wkb .= pack('L', 3); |
|
170 | 170 | $wkb .= $this->writePolygon($geometry); |
171 | 171 | break; |
172 | 172 | case 'MultiPoint'; |
173 | - $wkb .= pack('L',4); |
|
173 | + $wkb .= pack('L', 4); |
|
174 | 174 | $wkb .= $this->writeMulti($geometry); |
175 | 175 | break; |
176 | 176 | case 'MultiLineString'; |
177 | - $wkb .= pack('L',5); |
|
177 | + $wkb .= pack('L', 5); |
|
178 | 178 | $wkb .= $this->writeMulti($geometry); |
179 | 179 | break; |
180 | 180 | case 'MultiPolygon'; |
181 | - $wkb .= pack('L',6); |
|
181 | + $wkb .= pack('L', 6); |
|
182 | 182 | $wkb .= $this->writeMulti($geometry); |
183 | 183 | break; |
184 | 184 | case 'GeometryCollection'; |
185 | - $wkb .= pack('L',7); |
|
185 | + $wkb .= pack('L', 7); |
|
186 | 186 | $wkb .= $this->writeMulti($geometry); |
187 | 187 | break; |
188 | 188 | } |
189 | 189 | |
190 | 190 | if ($write_as_hex) { |
191 | - $unpacked = unpack('H*',$wkb); |
|
191 | + $unpacked = unpack('H*', $wkb); |
|
192 | 192 | return $unpacked[1]; |
193 | 193 | } |
194 | 194 | else { |
@@ -198,18 +198,18 @@ discard block |
||
198 | 198 | |
199 | 199 | function writePoint($point) { |
200 | 200 | // Set the coords |
201 | - $wkb = pack('dd',$point->x(), $point->y()); |
|
201 | + $wkb = pack('dd', $point->x(), $point->y()); |
|
202 | 202 | |
203 | 203 | return $wkb; |
204 | 204 | } |
205 | 205 | |
206 | 206 | function writeLineString($line) { |
207 | 207 | // Set the number of points in this line |
208 | - $wkb = pack('L',$line->numPoints()); |
|
208 | + $wkb = pack('L', $line->numPoints()); |
|
209 | 209 | |
210 | 210 | // Set the coords |
211 | 211 | foreach ($line->getComponents() as $point) { |
212 | - $wkb .= pack('dd',$point->x(), $point->y()); |
|
212 | + $wkb .= pack('dd', $point->x(), $point->y()); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | return $wkb; |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | |
218 | 218 | function writePolygon($poly) { |
219 | 219 | // Set the number of lines in this poly |
220 | - $wkb = pack('L',$poly->numGeometries()); |
|
220 | + $wkb = pack('L', $poly->numGeometries()); |
|
221 | 221 | |
222 | 222 | // Write the lines |
223 | 223 | foreach ($poly->getComponents() as $line) { |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | |
230 | 230 | function writeMulti($geometry) { |
231 | 231 | // Set the number of components |
232 | - $wkb = pack('L',$geometry->numGeometries()); |
|
232 | + $wkb = pack('L', $geometry->numGeometries()); |
|
233 | 233 | |
234 | 234 | // Write the components |
235 | 235 | foreach ($geometry->getComponents() as $component) { |
@@ -94,7 +94,9 @@ discard block |
||
94 | 94 | $line_length = unpack('L',fread($mem,4)); |
95 | 95 | |
96 | 96 | // Return an empty linestring if there is no line-length |
97 | - if (!$line_length[1]) return new LineString(); |
|
97 | + if (!$line_length[1]) { |
|
98 | + return new LineString(); |
|
99 | + } |
|
98 | 100 | |
99 | 101 | // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
100 | 102 | $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
@@ -190,8 +192,7 @@ discard block |
||
190 | 192 | if ($write_as_hex) { |
191 | 193 | $unpacked = unpack('H*',$wkb); |
192 | 194 | return $unpacked[1]; |
193 | - } |
|
194 | - else { |
|
195 | + } else { |
|
195 | 196 | return $wkb; |
196 | 197 | } |
197 | 198 | } |
@@ -8,7 +8,6 @@ discard block |
||
8 | 8 | /** |
9 | 9 | * Read WKT string into geometry objects |
10 | 10 | * |
11 | - * @param string $WKT A WKT string |
|
12 | 11 | * |
13 | 12 | * @return Geometry |
14 | 13 | */ |
@@ -184,11 +183,17 @@ discard block |
||
184 | 183 | else return $str; |
185 | 184 | } |
186 | 185 | |
186 | + /** |
|
187 | + * @param string $char |
|
188 | + */ |
|
187 | 189 | protected function beginsWith($str, $char) { |
188 | 190 | if (substr($str,0,strlen($char)) == $char) return TRUE; |
189 | 191 | else return FALSE; |
190 | 192 | } |
191 | 193 | |
194 | + /** |
|
195 | + * @param string $char |
|
196 | + */ |
|
192 | 197 | protected function endsWith($str, $char) { |
193 | 198 | if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
194 | 199 | else return FALSE; |
@@ -13,185 +13,185 @@ discard block |
||
13 | 13 | * @return Geometry |
14 | 14 | */ |
15 | 15 | public function read($wkt) { |
16 | - $wkt = trim($wkt); |
|
16 | + $wkt = trim($wkt); |
|
17 | 17 | |
18 | - // If it contains a ';', then it contains additional SRID data |
|
19 | - if (strpos($wkt,';')) { |
|
20 | - $parts = explode(';', $wkt); |
|
21 | - $wkt = $parts[1]; |
|
22 | - $eparts = explode('=',$parts[0]); |
|
23 | - $srid = $eparts[1]; |
|
24 | - } |
|
25 | - else { |
|
26 | - $srid = NULL; |
|
27 | - } |
|
18 | + // If it contains a ';', then it contains additional SRID data |
|
19 | + if (strpos($wkt,';')) { |
|
20 | + $parts = explode(';', $wkt); |
|
21 | + $wkt = $parts[1]; |
|
22 | + $eparts = explode('=',$parts[0]); |
|
23 | + $srid = $eparts[1]; |
|
24 | + } |
|
25 | + else { |
|
26 | + $srid = NULL; |
|
27 | + } |
|
28 | 28 | |
29 | - // If geos is installed, then we take a shortcut and let it parse the WKT |
|
30 | - if (geoPHP::geosInstalled()) { |
|
31 | - $reader = new GEOSWKTReader(); |
|
32 | - if ($srid) { |
|
33 | - $geom = geoPHP::geosToGeometry($reader->read($wkt)); |
|
34 | - $geom->setSRID($srid); |
|
35 | - return $geom; |
|
36 | - } |
|
37 | - else { |
|
38 | - return geoPHP::geosToGeometry($reader->read($wkt)); |
|
39 | - } |
|
40 | - } |
|
41 | - $wkt = str_replace(', ', ',', $wkt); |
|
29 | + // If geos is installed, then we take a shortcut and let it parse the WKT |
|
30 | + if (geoPHP::geosInstalled()) { |
|
31 | + $reader = new GEOSWKTReader(); |
|
32 | + if ($srid) { |
|
33 | + $geom = geoPHP::geosToGeometry($reader->read($wkt)); |
|
34 | + $geom->setSRID($srid); |
|
35 | + return $geom; |
|
36 | + } |
|
37 | + else { |
|
38 | + return geoPHP::geosToGeometry($reader->read($wkt)); |
|
39 | + } |
|
40 | + } |
|
41 | + $wkt = str_replace(', ', ',', $wkt); |
|
42 | 42 | |
43 | - // For each geometry type, check to see if we have a match at the |
|
44 | - // beggining of the string. If we do, then parse using that type |
|
45 | - foreach (geoPHP::geometryList() as $geom_type) { |
|
46 | - $wkt_geom = strtoupper($geom_type); |
|
47 | - if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) { |
|
48 | - $data_string = $this->getDataString($wkt); |
|
49 | - $method = 'parse'.$geom_type; |
|
43 | + // For each geometry type, check to see if we have a match at the |
|
44 | + // beggining of the string. If we do, then parse using that type |
|
45 | + foreach (geoPHP::geometryList() as $geom_type) { |
|
46 | + $wkt_geom = strtoupper($geom_type); |
|
47 | + if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) { |
|
48 | + $data_string = $this->getDataString($wkt); |
|
49 | + $method = 'parse'.$geom_type; |
|
50 | 50 | |
51 | - if ($srid) { |
|
52 | - $geom = $this->$method($data_string); |
|
53 | - $geom->setSRID($srid); |
|
54 | - return $geom; |
|
55 | - } |
|
56 | - else { |
|
57 | - return $this->$method($data_string); |
|
58 | - } |
|
51 | + if ($srid) { |
|
52 | + $geom = $this->$method($data_string); |
|
53 | + $geom->setSRID($srid); |
|
54 | + return $geom; |
|
55 | + } |
|
56 | + else { |
|
57 | + return $this->$method($data_string); |
|
58 | + } |
|
59 | 59 | |
60 | - } |
|
61 | - } |
|
60 | + } |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | private function parsePoint($data_string) { |
65 | - $data_string = $this->trimParens($data_string); |
|
66 | - $parts = explode(' ',$data_string); |
|
67 | - return new Point($parts[0], $parts[1]); |
|
65 | + $data_string = $this->trimParens($data_string); |
|
66 | + $parts = explode(' ',$data_string); |
|
67 | + return new Point($parts[0], $parts[1]); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | private function parseLineString($data_string) { |
71 | - $data_string = $this->trimParens($data_string); |
|
71 | + $data_string = $this->trimParens($data_string); |
|
72 | 72 | |
73 | - // If it's marked as empty, then return an empty line |
|
74 | - if ($data_string == 'EMPTY') return new LineString(); |
|
73 | + // If it's marked as empty, then return an empty line |
|
74 | + if ($data_string == 'EMPTY') return new LineString(); |
|
75 | 75 | |
76 | - $parts = explode(',',$data_string); |
|
77 | - $points = array(); |
|
78 | - foreach ($parts as $part) { |
|
79 | - $points[] = $this->parsePoint($part); |
|
80 | - } |
|
81 | - return new LineString($points); |
|
76 | + $parts = explode(',',$data_string); |
|
77 | + $points = array(); |
|
78 | + foreach ($parts as $part) { |
|
79 | + $points[] = $this->parsePoint($part); |
|
80 | + } |
|
81 | + return new LineString($points); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | private function parsePolygon($data_string) { |
85 | - $data_string = $this->trimParens($data_string); |
|
85 | + $data_string = $this->trimParens($data_string); |
|
86 | 86 | |
87 | - // If it's marked as empty, then return an empty polygon |
|
88 | - if ($data_string == 'EMPTY') return new Polygon(); |
|
87 | + // If it's marked as empty, then return an empty polygon |
|
88 | + if ($data_string == 'EMPTY') return new Polygon(); |
|
89 | 89 | |
90 | - $parts = explode('),(',$data_string); |
|
91 | - $lines = array(); |
|
92 | - foreach ($parts as $part) { |
|
93 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
95 | - $lines[] = $this->parseLineString($part); |
|
96 | - } |
|
97 | - return new Polygon($lines); |
|
90 | + $parts = explode('),(',$data_string); |
|
91 | + $lines = array(); |
|
92 | + foreach ($parts as $part) { |
|
93 | + if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | + if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
95 | + $lines[] = $this->parseLineString($part); |
|
96 | + } |
|
97 | + return new Polygon($lines); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | private function parseMultiPoint($data_string) { |
101 | - $data_string = $this->trimParens($data_string); |
|
101 | + $data_string = $this->trimParens($data_string); |
|
102 | 102 | |
103 | - // If it's marked as empty, then return an empty MutiPoint |
|
104 | - if ($data_string == 'EMPTY') return new MultiPoint(); |
|
103 | + // If it's marked as empty, then return an empty MutiPoint |
|
104 | + if ($data_string == 'EMPTY') return new MultiPoint(); |
|
105 | 105 | |
106 | - $parts = explode(',',$data_string); |
|
107 | - $points = array(); |
|
108 | - foreach ($parts as $part) { |
|
109 | - $points[] = $this->parsePoint($part); |
|
110 | - } |
|
111 | - return new MultiPoint($points); |
|
106 | + $parts = explode(',',$data_string); |
|
107 | + $points = array(); |
|
108 | + foreach ($parts as $part) { |
|
109 | + $points[] = $this->parsePoint($part); |
|
110 | + } |
|
111 | + return new MultiPoint($points); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | private function parseMultiLineString($data_string) { |
115 | - $data_string = $this->trimParens($data_string); |
|
115 | + $data_string = $this->trimParens($data_string); |
|
116 | 116 | |
117 | - // If it's marked as empty, then return an empty multi-linestring |
|
118 | - if ($data_string == 'EMPTY') return new MultiLineString(); |
|
117 | + // If it's marked as empty, then return an empty multi-linestring |
|
118 | + if ($data_string == 'EMPTY') return new MultiLineString(); |
|
119 | 119 | |
120 | - $parts = explode('),(',$data_string); |
|
121 | - $lines = array(); |
|
122 | - foreach ($parts as $part) { |
|
123 | - // Repair the string if the explode broke it |
|
124 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
126 | - $lines[] = $this->parseLineString($part); |
|
127 | - } |
|
128 | - return new MultiLineString($lines); |
|
120 | + $parts = explode('),(',$data_string); |
|
121 | + $lines = array(); |
|
122 | + foreach ($parts as $part) { |
|
123 | + // Repair the string if the explode broke it |
|
124 | + if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | + if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
126 | + $lines[] = $this->parseLineString($part); |
|
127 | + } |
|
128 | + return new MultiLineString($lines); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | private function parseMultiPolygon($data_string) { |
132 | - $data_string = $this->trimParens($data_string); |
|
132 | + $data_string = $this->trimParens($data_string); |
|
133 | 133 | |
134 | - // If it's marked as empty, then return an empty multi-polygon |
|
135 | - if ($data_string == 'EMPTY') return new MultiPolygon(); |
|
134 | + // If it's marked as empty, then return an empty multi-polygon |
|
135 | + if ($data_string == 'EMPTY') return new MultiPolygon(); |
|
136 | 136 | |
137 | - $parts = explode(')),((',$data_string); |
|
138 | - $polys = array(); |
|
139 | - foreach ($parts as $part) { |
|
140 | - // Repair the string if the explode broke it |
|
141 | - if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
143 | - $polys[] = $this->parsePolygon($part); |
|
144 | - } |
|
145 | - return new MultiPolygon($polys); |
|
137 | + $parts = explode(')),((',$data_string); |
|
138 | + $polys = array(); |
|
139 | + foreach ($parts as $part) { |
|
140 | + // Repair the string if the explode broke it |
|
141 | + if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | + if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
143 | + $polys[] = $this->parsePolygon($part); |
|
144 | + } |
|
145 | + return new MultiPolygon($polys); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | private function parseGeometryCollection($data_string) { |
149 | - $data_string = $this->trimParens($data_string); |
|
149 | + $data_string = $this->trimParens($data_string); |
|
150 | 150 | |
151 | - // If it's marked as empty, then return an empty geom-collection |
|
152 | - if ($data_string == 'EMPTY') return new GeometryCollection(); |
|
151 | + // If it's marked as empty, then return an empty geom-collection |
|
152 | + if ($data_string == 'EMPTY') return new GeometryCollection(); |
|
153 | 153 | |
154 | - $geometries = array(); |
|
155 | - $matches = array(); |
|
156 | - $str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string); |
|
157 | - $components = explode('|', trim($str)); |
|
154 | + $geometries = array(); |
|
155 | + $matches = array(); |
|
156 | + $str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string); |
|
157 | + $components = explode('|', trim($str)); |
|
158 | 158 | |
159 | - foreach ($components as $component) { |
|
160 | - $geometries[] = $this->read($component); |
|
161 | - } |
|
162 | - return new GeometryCollection($geometries); |
|
159 | + foreach ($components as $component) { |
|
160 | + $geometries[] = $this->read($component); |
|
161 | + } |
|
162 | + return new GeometryCollection($geometries); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | protected function getDataString($wkt) { |
166 | - $first_paren = strpos($wkt, '('); |
|
166 | + $first_paren = strpos($wkt, '('); |
|
167 | 167 | |
168 | - if ($first_paren !== FALSE) { |
|
169 | - return substr($wkt, $first_paren); |
|
170 | - } |
|
171 | - return FALSE; |
|
168 | + if ($first_paren !== FALSE) { |
|
169 | + return substr($wkt, $first_paren); |
|
170 | + } |
|
171 | + return FALSE; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
175 | 175 | * Trim the parenthesis and spaces |
176 | 176 | */ |
177 | 177 | protected function trimParens($str) { |
178 | - $str = trim($str); |
|
178 | + $str = trim($str); |
|
179 | 179 | |
180 | - // We want to only strip off one set of parenthesis |
|
181 | - if ($this->beginsWith($str, '(')) { |
|
182 | - return substr($str,1,-1); |
|
183 | - } |
|
184 | - else return $str; |
|
180 | + // We want to only strip off one set of parenthesis |
|
181 | + if ($this->beginsWith($str, '(')) { |
|
182 | + return substr($str,1,-1); |
|
183 | + } |
|
184 | + else return $str; |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | protected function beginsWith($str, $char) { |
188 | - if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
189 | - else return FALSE; |
|
188 | + if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
189 | + else return FALSE; |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | protected function endsWith($str, $char) { |
193 | - if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
194 | - else return FALSE; |
|
193 | + if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
194 | + else return FALSE; |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -202,19 +202,19 @@ discard block |
||
202 | 202 | * @return string The WKT string representation of the input geometries |
203 | 203 | */ |
204 | 204 | public function write(Geometry $geometry) { |
205 | - // If geos is installed, then we take a shortcut and let it write the WKT |
|
206 | - if (geoPHP::geosInstalled()) { |
|
207 | - $writer = new GEOSWKTWriter(); |
|
208 | - $writer->setTrim(TRUE); |
|
209 | - return $writer->write($geometry->geos()); |
|
210 | - } |
|
205 | + // If geos is installed, then we take a shortcut and let it write the WKT |
|
206 | + if (geoPHP::geosInstalled()) { |
|
207 | + $writer = new GEOSWKTWriter(); |
|
208 | + $writer->setTrim(TRUE); |
|
209 | + return $writer->write($geometry->geos()); |
|
210 | + } |
|
211 | 211 | |
212 | - if ($geometry->isEmpty()) { |
|
213 | - return strtoupper($geometry->geometryType()).' EMPTY'; |
|
214 | - } |
|
215 | - else if ($data = $this->extractData($geometry)) { |
|
216 | - return strtoupper($geometry->geometryType()).' ('.$data.')'; |
|
217 | - } |
|
212 | + if ($geometry->isEmpty()) { |
|
213 | + return strtoupper($geometry->geometryType()).' EMPTY'; |
|
214 | + } |
|
215 | + else if ($data = $this->extractData($geometry)) { |
|
216 | + return strtoupper($geometry->geometryType()).' ('.$data.')'; |
|
217 | + } |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -225,28 +225,28 @@ discard block |
||
225 | 225 | * @return string |
226 | 226 | */ |
227 | 227 | public function extractData($geometry) { |
228 | - $parts = array(); |
|
229 | - switch ($geometry->geometryType()) { |
|
230 | - case 'Point': |
|
231 | - return $geometry->getX().' '.$geometry->getY(); |
|
232 | - case 'LineString': |
|
233 | - foreach ($geometry->getComponents() as $component) { |
|
234 | - $parts[] = $this->extractData($component); |
|
235 | - } |
|
236 | - return implode(', ', $parts); |
|
237 | - case 'Polygon': |
|
238 | - case 'MultiPoint': |
|
239 | - case 'MultiLineString': |
|
240 | - case 'MultiPolygon': |
|
241 | - foreach ($geometry->getComponents() as $component) { |
|
242 | - $parts[] = '('.$this->extractData($component).')'; |
|
243 | - } |
|
244 | - return implode(', ', $parts); |
|
245 | - case 'GeometryCollection': |
|
246 | - foreach ($geometry->getComponents() as $component) { |
|
247 | - $parts[] = strtoupper($component->geometryType()).' ('.$this->extractData($component).')'; |
|
248 | - } |
|
249 | - return implode(', ', $parts); |
|
250 | - } |
|
228 | + $parts = array(); |
|
229 | + switch ($geometry->geometryType()) { |
|
230 | + case 'Point': |
|
231 | + return $geometry->getX().' '.$geometry->getY(); |
|
232 | + case 'LineString': |
|
233 | + foreach ($geometry->getComponents() as $component) { |
|
234 | + $parts[] = $this->extractData($component); |
|
235 | + } |
|
236 | + return implode(', ', $parts); |
|
237 | + case 'Polygon': |
|
238 | + case 'MultiPoint': |
|
239 | + case 'MultiLineString': |
|
240 | + case 'MultiPolygon': |
|
241 | + foreach ($geometry->getComponents() as $component) { |
|
242 | + $parts[] = '('.$this->extractData($component).')'; |
|
243 | + } |
|
244 | + return implode(', ', $parts); |
|
245 | + case 'GeometryCollection': |
|
246 | + foreach ($geometry->getComponents() as $component) { |
|
247 | + $parts[] = strtoupper($component->geometryType()).' ('.$this->extractData($component).')'; |
|
248 | + } |
|
249 | + return implode(', ', $parts); |
|
250 | + } |
|
251 | 251 | } |
252 | 252 | } |
@@ -16,10 +16,10 @@ discard block |
||
16 | 16 | $wkt = trim($wkt); |
17 | 17 | |
18 | 18 | // If it contains a ';', then it contains additional SRID data |
19 | - if (strpos($wkt,';')) { |
|
19 | + if (strpos($wkt, ';')) { |
|
20 | 20 | $parts = explode(';', $wkt); |
21 | 21 | $wkt = $parts[1]; |
22 | - $eparts = explode('=',$parts[0]); |
|
22 | + $eparts = explode('=', $parts[0]); |
|
23 | 23 | $srid = $eparts[1]; |
24 | 24 | } |
25 | 25 | else { |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | |
64 | 64 | private function parsePoint($data_string) { |
65 | 65 | $data_string = $this->trimParens($data_string); |
66 | - $parts = explode(' ',$data_string); |
|
66 | + $parts = explode(' ', $data_string); |
|
67 | 67 | return new Point($parts[0], $parts[1]); |
68 | 68 | } |
69 | 69 | |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | // If it's marked as empty, then return an empty line |
74 | 74 | if ($data_string == 'EMPTY') return new LineString(); |
75 | 75 | |
76 | - $parts = explode(',',$data_string); |
|
76 | + $parts = explode(',', $data_string); |
|
77 | 77 | $points = array(); |
78 | 78 | foreach ($parts as $part) { |
79 | 79 | $points[] = $this->parsePoint($part); |
@@ -87,11 +87,11 @@ discard block |
||
87 | 87 | // If it's marked as empty, then return an empty polygon |
88 | 88 | if ($data_string == 'EMPTY') return new Polygon(); |
89 | 89 | |
90 | - $parts = explode('),(',$data_string); |
|
90 | + $parts = explode('),(', $data_string); |
|
91 | 91 | $lines = array(); |
92 | 92 | foreach ($parts as $part) { |
93 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
93 | + if (!$this->beginsWith($part, '(')) $part = '('.$part; |
|
94 | + if (!$this->endsWith($part, ')')) $part = $part.')'; |
|
95 | 95 | $lines[] = $this->parseLineString($part); |
96 | 96 | } |
97 | 97 | return new Polygon($lines); |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | // If it's marked as empty, then return an empty MutiPoint |
104 | 104 | if ($data_string == 'EMPTY') return new MultiPoint(); |
105 | 105 | |
106 | - $parts = explode(',',$data_string); |
|
106 | + $parts = explode(',', $data_string); |
|
107 | 107 | $points = array(); |
108 | 108 | foreach ($parts as $part) { |
109 | 109 | $points[] = $this->parsePoint($part); |
@@ -117,12 +117,12 @@ discard block |
||
117 | 117 | // If it's marked as empty, then return an empty multi-linestring |
118 | 118 | if ($data_string == 'EMPTY') return new MultiLineString(); |
119 | 119 | |
120 | - $parts = explode('),(',$data_string); |
|
120 | + $parts = explode('),(', $data_string); |
|
121 | 121 | $lines = array(); |
122 | 122 | foreach ($parts as $part) { |
123 | 123 | // Repair the string if the explode broke it |
124 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
124 | + if (!$this->beginsWith($part, '(')) $part = '('.$part; |
|
125 | + if (!$this->endsWith($part, ')')) $part = $part.')'; |
|
126 | 126 | $lines[] = $this->parseLineString($part); |
127 | 127 | } |
128 | 128 | return new MultiLineString($lines); |
@@ -134,12 +134,12 @@ discard block |
||
134 | 134 | // If it's marked as empty, then return an empty multi-polygon |
135 | 135 | if ($data_string == 'EMPTY') return new MultiPolygon(); |
136 | 136 | |
137 | - $parts = explode(')),((',$data_string); |
|
137 | + $parts = explode(')),((', $data_string); |
|
138 | 138 | $polys = array(); |
139 | 139 | foreach ($parts as $part) { |
140 | 140 | // Repair the string if the explode broke it |
141 | - if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
141 | + if (!$this->beginsWith($part, '((')) $part = '(('.$part; |
|
142 | + if (!$this->endsWith($part, '))')) $part = $part.'))'; |
|
143 | 143 | $polys[] = $this->parsePolygon($part); |
144 | 144 | } |
145 | 145 | return new MultiPolygon($polys); |
@@ -179,18 +179,18 @@ discard block |
||
179 | 179 | |
180 | 180 | // We want to only strip off one set of parenthesis |
181 | 181 | if ($this->beginsWith($str, '(')) { |
182 | - return substr($str,1,-1); |
|
182 | + return substr($str, 1, -1); |
|
183 | 183 | } |
184 | 184 | else return $str; |
185 | 185 | } |
186 | 186 | |
187 | 187 | protected function beginsWith($str, $char) { |
188 | - if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
188 | + if (substr($str, 0, strlen($char)) == $char) return TRUE; |
|
189 | 189 | else return FALSE; |
190 | 190 | } |
191 | 191 | |
192 | 192 | protected function endsWith($str, $char) { |
193 | - if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
193 | + if (substr($str, (0 - strlen($char))) == $char) return TRUE; |
|
194 | 194 | else return FALSE; |
195 | 195 | } |
196 | 196 |
@@ -21,8 +21,7 @@ discard block |
||
21 | 21 | $wkt = $parts[1]; |
22 | 22 | $eparts = explode('=',$parts[0]); |
23 | 23 | $srid = $eparts[1]; |
24 | - } |
|
25 | - else { |
|
24 | + } else { |
|
26 | 25 | $srid = NULL; |
27 | 26 | } |
28 | 27 | |
@@ -33,8 +32,7 @@ discard block |
||
33 | 32 | $geom = geoPHP::geosToGeometry($reader->read($wkt)); |
34 | 33 | $geom->setSRID($srid); |
35 | 34 | return $geom; |
36 | - } |
|
37 | - else { |
|
35 | + } else { |
|
38 | 36 | return geoPHP::geosToGeometry($reader->read($wkt)); |
39 | 37 | } |
40 | 38 | } |
@@ -52,8 +50,7 @@ discard block |
||
52 | 50 | $geom = $this->$method($data_string); |
53 | 51 | $geom->setSRID($srid); |
54 | 52 | return $geom; |
55 | - } |
|
56 | - else { |
|
53 | + } else { |
|
57 | 54 | return $this->$method($data_string); |
58 | 55 | } |
59 | 56 | |
@@ -71,7 +68,9 @@ discard block |
||
71 | 68 | $data_string = $this->trimParens($data_string); |
72 | 69 | |
73 | 70 | // If it's marked as empty, then return an empty line |
74 | - if ($data_string == 'EMPTY') return new LineString(); |
|
71 | + if ($data_string == 'EMPTY') { |
|
72 | + return new LineString(); |
|
73 | + } |
|
75 | 74 | |
76 | 75 | $parts = explode(',',$data_string); |
77 | 76 | $points = array(); |
@@ -85,13 +84,19 @@ discard block |
||
85 | 84 | $data_string = $this->trimParens($data_string); |
86 | 85 | |
87 | 86 | // If it's marked as empty, then return an empty polygon |
88 | - if ($data_string == 'EMPTY') return new Polygon(); |
|
87 | + if ($data_string == 'EMPTY') { |
|
88 | + return new Polygon(); |
|
89 | + } |
|
89 | 90 | |
90 | 91 | $parts = explode('),(',$data_string); |
91 | 92 | $lines = array(); |
92 | 93 | foreach ($parts as $part) { |
93 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
94 | + if (!$this->beginsWith($part,'(')) { |
|
95 | + $part = '(' . $part; |
|
96 | + } |
|
97 | + if (!$this->endsWith($part,')')) { |
|
98 | + $part = $part . ')'; |
|
99 | + } |
|
95 | 100 | $lines[] = $this->parseLineString($part); |
96 | 101 | } |
97 | 102 | return new Polygon($lines); |
@@ -101,7 +106,9 @@ discard block |
||
101 | 106 | $data_string = $this->trimParens($data_string); |
102 | 107 | |
103 | 108 | // If it's marked as empty, then return an empty MutiPoint |
104 | - if ($data_string == 'EMPTY') return new MultiPoint(); |
|
109 | + if ($data_string == 'EMPTY') { |
|
110 | + return new MultiPoint(); |
|
111 | + } |
|
105 | 112 | |
106 | 113 | $parts = explode(',',$data_string); |
107 | 114 | $points = array(); |
@@ -115,14 +122,20 @@ discard block |
||
115 | 122 | $data_string = $this->trimParens($data_string); |
116 | 123 | |
117 | 124 | // If it's marked as empty, then return an empty multi-linestring |
118 | - if ($data_string == 'EMPTY') return new MultiLineString(); |
|
125 | + if ($data_string == 'EMPTY') { |
|
126 | + return new MultiLineString(); |
|
127 | + } |
|
119 | 128 | |
120 | 129 | $parts = explode('),(',$data_string); |
121 | 130 | $lines = array(); |
122 | 131 | foreach ($parts as $part) { |
123 | 132 | // Repair the string if the explode broke it |
124 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
133 | + if (!$this->beginsWith($part,'(')) { |
|
134 | + $part = '(' . $part; |
|
135 | + } |
|
136 | + if (!$this->endsWith($part,')')) { |
|
137 | + $part = $part . ')'; |
|
138 | + } |
|
126 | 139 | $lines[] = $this->parseLineString($part); |
127 | 140 | } |
128 | 141 | return new MultiLineString($lines); |
@@ -132,14 +145,20 @@ discard block |
||
132 | 145 | $data_string = $this->trimParens($data_string); |
133 | 146 | |
134 | 147 | // If it's marked as empty, then return an empty multi-polygon |
135 | - if ($data_string == 'EMPTY') return new MultiPolygon(); |
|
148 | + if ($data_string == 'EMPTY') { |
|
149 | + return new MultiPolygon(); |
|
150 | + } |
|
136 | 151 | |
137 | 152 | $parts = explode(')),((',$data_string); |
138 | 153 | $polys = array(); |
139 | 154 | foreach ($parts as $part) { |
140 | 155 | // Repair the string if the explode broke it |
141 | - if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
156 | + if (!$this->beginsWith($part,'((')) { |
|
157 | + $part = '((' . $part; |
|
158 | + } |
|
159 | + if (!$this->endsWith($part,'))')) { |
|
160 | + $part = $part . '))'; |
|
161 | + } |
|
143 | 162 | $polys[] = $this->parsePolygon($part); |
144 | 163 | } |
145 | 164 | return new MultiPolygon($polys); |
@@ -149,7 +168,9 @@ discard block |
||
149 | 168 | $data_string = $this->trimParens($data_string); |
150 | 169 | |
151 | 170 | // If it's marked as empty, then return an empty geom-collection |
152 | - if ($data_string == 'EMPTY') return new GeometryCollection(); |
|
171 | + if ($data_string == 'EMPTY') { |
|
172 | + return new GeometryCollection(); |
|
173 | + } |
|
153 | 174 | |
154 | 175 | $geometries = array(); |
155 | 176 | $matches = array(); |
@@ -180,18 +201,25 @@ discard block |
||
180 | 201 | // We want to only strip off one set of parenthesis |
181 | 202 | if ($this->beginsWith($str, '(')) { |
182 | 203 | return substr($str,1,-1); |
204 | + } else { |
|
205 | + return $str; |
|
183 | 206 | } |
184 | - else return $str; |
|
185 | 207 | } |
186 | 208 | |
187 | 209 | protected function beginsWith($str, $char) { |
188 | - if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
189 | - else return FALSE; |
|
210 | + if (substr($str,0,strlen($char)) == $char) { |
|
211 | + return TRUE; |
|
212 | + } else { |
|
213 | + return FALSE; |
|
214 | + } |
|
190 | 215 | } |
191 | 216 | |
192 | 217 | protected function endsWith($str, $char) { |
193 | - if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
194 | - else return FALSE; |
|
218 | + if (substr($str,(0 - strlen($char))) == $char) { |
|
219 | + return TRUE; |
|
220 | + } else { |
|
221 | + return FALSE; |
|
222 | + } |
|
195 | 223 | } |
196 | 224 | |
197 | 225 | /** |
@@ -211,8 +239,7 @@ discard block |
||
211 | 239 | |
212 | 240 | if ($geometry->isEmpty()) { |
213 | 241 | return strtoupper($geometry->geometryType()).' EMPTY'; |
214 | - } |
|
215 | - else if ($data = $this->extractData($geometry)) { |
|
242 | + } else if ($data = $this->extractData($geometry)) { |
|
216 | 243 | return strtoupper($geometry->geometryType()).' ('.$data.')'; |
217 | 244 | } |
218 | 245 | } |
@@ -193,6 +193,9 @@ |
||
193 | 193 | } |
194 | 194 | } |
195 | 195 | |
196 | + /** |
|
197 | + * @param Point $point |
|
198 | + */ |
|
196 | 199 | public function pointOnVertex($point) { |
197 | 200 | foreach($this->getPoints() as $vertex) { |
198 | 201 | if ($point->equals($vertex)) { |
@@ -9,70 +9,70 @@ discard block |
||
9 | 9 | protected $geom_type = 'Polygon'; |
10 | 10 | |
11 | 11 | public function area($exterior_only = FALSE, $signed = FALSE) { |
12 | - if ($this->isEmpty()) return 0; |
|
12 | + if ($this->isEmpty()) return 0; |
|
13 | 13 | |
14 | - if ($this->geos() && $exterior_only == FALSE) { |
|
15 | - return $this->geos()->area(); |
|
16 | - } |
|
14 | + if ($this->geos() && $exterior_only == FALSE) { |
|
15 | + return $this->geos()->area(); |
|
16 | + } |
|
17 | 17 | |
18 | - $exterior_ring = $this->components[0]; |
|
19 | - $pts = $exterior_ring->getComponents(); |
|
18 | + $exterior_ring = $this->components[0]; |
|
19 | + $pts = $exterior_ring->getComponents(); |
|
20 | 20 | |
21 | - $c = count($pts); |
|
22 | - if((int)$c == '0') return NULL; |
|
23 | - $a = '0'; |
|
24 | - foreach($pts as $k => $p){ |
|
25 | - $j = ($k + 1) % $c; |
|
26 | - $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
27 | - } |
|
21 | + $c = count($pts); |
|
22 | + if((int)$c == '0') return NULL; |
|
23 | + $a = '0'; |
|
24 | + foreach($pts as $k => $p){ |
|
25 | + $j = ($k + 1) % $c; |
|
26 | + $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
27 | + } |
|
28 | 28 | |
29 | - if ($signed) $area = ($a / 2); |
|
30 | - else $area = abs(($a / 2)); |
|
29 | + if ($signed) $area = ($a / 2); |
|
30 | + else $area = abs(($a / 2)); |
|
31 | 31 | |
32 | - if ($exterior_only == TRUE) { |
|
33 | - return $area; |
|
34 | - } |
|
35 | - foreach ($this->components as $delta => $component) { |
|
36 | - if ($delta != 0) { |
|
37 | - $inner_poly = new Polygon(array($component)); |
|
38 | - $area -= $inner_poly->area(); |
|
39 | - } |
|
40 | - } |
|
41 | - return $area; |
|
32 | + if ($exterior_only == TRUE) { |
|
33 | + return $area; |
|
34 | + } |
|
35 | + foreach ($this->components as $delta => $component) { |
|
36 | + if ($delta != 0) { |
|
37 | + $inner_poly = new Polygon(array($component)); |
|
38 | + $area -= $inner_poly->area(); |
|
39 | + } |
|
40 | + } |
|
41 | + return $area; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function centroid() { |
45 | - if ($this->isEmpty()) return NULL; |
|
45 | + if ($this->isEmpty()) return NULL; |
|
46 | 46 | |
47 | - if ($this->geos()) { |
|
48 | - return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
49 | - } |
|
47 | + if ($this->geos()) { |
|
48 | + return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
49 | + } |
|
50 | 50 | |
51 | - $exterior_ring = $this->components[0]; |
|
52 | - $pts = $exterior_ring->getComponents(); |
|
51 | + $exterior_ring = $this->components[0]; |
|
52 | + $pts = $exterior_ring->getComponents(); |
|
53 | 53 | |
54 | - $c = count($pts); |
|
55 | - if((int)$c == '0') return NULL; |
|
56 | - $cn = array('x' => '0', 'y' => '0'); |
|
57 | - $a = $this->area(TRUE, TRUE); |
|
54 | + $c = count($pts); |
|
55 | + if((int)$c == '0') return NULL; |
|
56 | + $cn = array('x' => '0', 'y' => '0'); |
|
57 | + $a = $this->area(TRUE, TRUE); |
|
58 | 58 | |
59 | - // If this is a polygon with no area. Just return the first point. |
|
60 | - if ($a == 0) { |
|
61 | - return $this->exteriorRing()->pointN(1); |
|
62 | - } |
|
59 | + // If this is a polygon with no area. Just return the first point. |
|
60 | + if ($a == 0) { |
|
61 | + return $this->exteriorRing()->pointN(1); |
|
62 | + } |
|
63 | 63 | |
64 | - foreach($pts as $k => $p){ |
|
65 | - $j = ($k + 1) % $c; |
|
66 | - $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
67 | - $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; |
|
68 | - $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; |
|
69 | - } |
|
64 | + foreach($pts as $k => $p){ |
|
65 | + $j = ($k + 1) % $c; |
|
66 | + $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
67 | + $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; |
|
68 | + $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; |
|
69 | + } |
|
70 | 70 | |
71 | - $cn['x'] = $cn['x'] / ( 6 * $a); |
|
72 | - $cn['y'] = $cn['y'] / ( 6 * $a); |
|
71 | + $cn['x'] = $cn['x'] / ( 6 * $a); |
|
72 | + $cn['y'] = $cn['y'] / ( 6 * $a); |
|
73 | 73 | |
74 | - $centroid = new Point($cn['x'], $cn['y']); |
|
75 | - return $centroid; |
|
74 | + $centroid = new Point($cn['x'], $cn['y']); |
|
75 | + return $centroid; |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
@@ -98,41 +98,41 @@ discard block |
||
98 | 98 | } |
99 | 99 | |
100 | 100 | public function exteriorRing() { |
101 | - if ($this->isEmpty()) return new LineString(); |
|
102 | - return $this->components[0]; |
|
101 | + if ($this->isEmpty()) return new LineString(); |
|
102 | + return $this->components[0]; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | public function numInteriorRings() { |
106 | - if ($this->isEmpty()) return 0; |
|
107 | - return $this->numGeometries()-1; |
|
106 | + if ($this->isEmpty()) return 0; |
|
107 | + return $this->numGeometries()-1; |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | public function interiorRingN($n) { |
111 | - return $this->geometryN($n+1); |
|
111 | + return $this->geometryN($n+1); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | public function dimension() { |
115 | - if ($this->isEmpty()) return 0; |
|
116 | - return 2; |
|
115 | + if ($this->isEmpty()) return 0; |
|
116 | + return 2; |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | public function isSimple() { |
120 | - if ($this->geos()) { |
|
121 | - return $this->geos()->isSimple(); |
|
122 | - } |
|
120 | + if ($this->geos()) { |
|
121 | + return $this->geos()->isSimple(); |
|
122 | + } |
|
123 | 123 | |
124 | - $segments = $this->explode(); |
|
124 | + $segments = $this->explode(); |
|
125 | 125 | |
126 | - foreach ($segments as $i => $segment) { |
|
127 | - foreach ($segments as $j => $check_segment) { |
|
128 | - if ($i != $j) { |
|
129 | - if ($segment->lineSegmentIntersect($check_segment)) { |
|
130 | - return FALSE; |
|
131 | - } |
|
132 | - } |
|
133 | - } |
|
134 | - } |
|
135 | - return TRUE; |
|
126 | + foreach ($segments as $i => $segment) { |
|
127 | + foreach ($segments as $j => $check_segment) { |
|
128 | + if ($i != $j) { |
|
129 | + if ($segment->lineSegmentIntersect($check_segment)) { |
|
130 | + return FALSE; |
|
131 | + } |
|
132 | + } |
|
133 | + } |
|
134 | + } |
|
135 | + return TRUE; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
@@ -146,59 +146,59 @@ discard block |
||
146 | 146 | * @return boolean |
147 | 147 | */ |
148 | 148 | public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) { |
149 | - $vertices = $this->getPoints(); |
|
149 | + $vertices = $this->getPoints(); |
|
150 | 150 | |
151 | - // Check if the point sits exactly on a vertex |
|
152 | - if ($this->pointOnVertex($point, $vertices)) { |
|
153 | - return $pointOnVertex ? TRUE : FALSE; |
|
154 | - } |
|
151 | + // Check if the point sits exactly on a vertex |
|
152 | + if ($this->pointOnVertex($point, $vertices)) { |
|
153 | + return $pointOnVertex ? TRUE : FALSE; |
|
154 | + } |
|
155 | 155 | |
156 | - // Check if the point is inside the polygon or on the boundary |
|
157 | - $intersections = 0; |
|
158 | - $vertices_count = count($vertices); |
|
156 | + // Check if the point is inside the polygon or on the boundary |
|
157 | + $intersections = 0; |
|
158 | + $vertices_count = count($vertices); |
|
159 | 159 | |
160 | - for ($i=1; $i < $vertices_count; $i++) { |
|
161 | - $vertex1 = $vertices[$i-1]; |
|
162 | - $vertex2 = $vertices[$i]; |
|
163 | - if ($vertex1->y() == $vertex2->y() |
|
164 | - && $vertex1->y() == $point->y() |
|
165 | - && $point->x() > min($vertex1->x(), $vertex2->x()) |
|
166 | - && $point->x() < max($vertex1->x(), $vertex2->x())) { |
|
167 | - // Check if point is on an horizontal polygon boundary |
|
168 | - return $pointOnBoundary ? TRUE : FALSE; |
|
169 | - } |
|
170 | - if ($point->y() > min($vertex1->y(), $vertex2->y()) |
|
171 | - && $point->y() <= max($vertex1->y(), $vertex2->y()) |
|
172 | - && $point->x() <= max($vertex1->x(), $vertex2->x()) |
|
173 | - && $vertex1->y() != $vertex2->y()) { |
|
174 | - $xinters = |
|
175 | - ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
|
176 | - / ($vertex2->y() - $vertex1->y()) |
|
177 | - + $vertex1->x(); |
|
178 | - if ($xinters == $point->x()) { |
|
179 | - // Check if point is on the polygon boundary (other than horizontal) |
|
180 | - return $pointOnBoundary ? TRUE : FALSE; |
|
181 | - } |
|
182 | - if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { |
|
183 | - $intersections++; |
|
184 | - } |
|
185 | - } |
|
186 | - } |
|
187 | - // If the number of edges we passed through is even, then it's in the polygon. |
|
188 | - if ($intersections % 2 != 0) { |
|
189 | - return TRUE; |
|
190 | - } |
|
191 | - else { |
|
192 | - return FALSE; |
|
193 | - } |
|
160 | + for ($i=1; $i < $vertices_count; $i++) { |
|
161 | + $vertex1 = $vertices[$i-1]; |
|
162 | + $vertex2 = $vertices[$i]; |
|
163 | + if ($vertex1->y() == $vertex2->y() |
|
164 | + && $vertex1->y() == $point->y() |
|
165 | + && $point->x() > min($vertex1->x(), $vertex2->x()) |
|
166 | + && $point->x() < max($vertex1->x(), $vertex2->x())) { |
|
167 | + // Check if point is on an horizontal polygon boundary |
|
168 | + return $pointOnBoundary ? TRUE : FALSE; |
|
169 | + } |
|
170 | + if ($point->y() > min($vertex1->y(), $vertex2->y()) |
|
171 | + && $point->y() <= max($vertex1->y(), $vertex2->y()) |
|
172 | + && $point->x() <= max($vertex1->x(), $vertex2->x()) |
|
173 | + && $vertex1->y() != $vertex2->y()) { |
|
174 | + $xinters = |
|
175 | + ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
|
176 | + / ($vertex2->y() - $vertex1->y()) |
|
177 | + + $vertex1->x(); |
|
178 | + if ($xinters == $point->x()) { |
|
179 | + // Check if point is on the polygon boundary (other than horizontal) |
|
180 | + return $pointOnBoundary ? TRUE : FALSE; |
|
181 | + } |
|
182 | + if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { |
|
183 | + $intersections++; |
|
184 | + } |
|
185 | + } |
|
186 | + } |
|
187 | + // If the number of edges we passed through is even, then it's in the polygon. |
|
188 | + if ($intersections % 2 != 0) { |
|
189 | + return TRUE; |
|
190 | + } |
|
191 | + else { |
|
192 | + return FALSE; |
|
193 | + } |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | public function pointOnVertex($point) { |
197 | - foreach($this->getPoints() as $vertex) { |
|
198 | - if ($point->equals($vertex)) { |
|
199 | - return true; |
|
200 | - } |
|
201 | - } |
|
197 | + foreach($this->getPoints() as $vertex) { |
|
198 | + if ($point->equals($vertex)) { |
|
199 | + return true; |
|
200 | + } |
|
201 | + } |
|
202 | 202 | } |
203 | 203 | |
204 | 204 |
@@ -19,15 +19,15 @@ discard block |
||
19 | 19 | $pts = $exterior_ring->getComponents(); |
20 | 20 | |
21 | 21 | $c = count($pts); |
22 | - if((int)$c == '0') return NULL; |
|
22 | + if ((int) $c == '0') return NULL; |
|
23 | 23 | $a = '0'; |
24 | - foreach($pts as $k => $p){ |
|
25 | - $j = ($k + 1) % $c; |
|
26 | - $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
24 | + foreach ($pts as $k => $p) { |
|
25 | + $j = ($k + 1)%$c; |
|
26 | + $a = $a + ($p->getX()*$pts[$j]->getY()) - ($p->getY()*$pts[$j]->getX()); |
|
27 | 27 | } |
28 | 28 | |
29 | - if ($signed) $area = ($a / 2); |
|
30 | - else $area = abs(($a / 2)); |
|
29 | + if ($signed) $area = ($a/2); |
|
30 | + else $area = abs(($a/2)); |
|
31 | 31 | |
32 | 32 | if ($exterior_only == TRUE) { |
33 | 33 | return $area; |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | $pts = $exterior_ring->getComponents(); |
53 | 53 | |
54 | 54 | $c = count($pts); |
55 | - if((int)$c == '0') return NULL; |
|
55 | + if ((int) $c == '0') return NULL; |
|
56 | 56 | $cn = array('x' => '0', 'y' => '0'); |
57 | 57 | $a = $this->area(TRUE, TRUE); |
58 | 58 | |
@@ -61,15 +61,15 @@ discard block |
||
61 | 61 | return $this->exteriorRing()->pointN(1); |
62 | 62 | } |
63 | 63 | |
64 | - foreach($pts as $k => $p){ |
|
65 | - $j = ($k + 1) % $c; |
|
66 | - $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
67 | - $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; |
|
68 | - $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; |
|
64 | + foreach ($pts as $k => $p) { |
|
65 | + $j = ($k + 1)%$c; |
|
66 | + $P = ($p->getX()*$pts[$j]->getY()) - ($p->getY()*$pts[$j]->getX()); |
|
67 | + $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX())*$P; |
|
68 | + $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY())*$P; |
|
69 | 69 | } |
70 | 70 | |
71 | - $cn['x'] = $cn['x'] / ( 6 * $a); |
|
72 | - $cn['y'] = $cn['y'] / ( 6 * $a); |
|
71 | + $cn['x'] = $cn['x']/(6*$a); |
|
72 | + $cn['y'] = $cn['y']/(6*$a); |
|
73 | 73 | |
74 | 74 | $centroid = new Point($cn['x'], $cn['y']); |
75 | 75 | return $centroid; |
@@ -85,10 +85,10 @@ discard block |
||
85 | 85 | |
86 | 86 | $max = array('length' => 0, 'point' => null); |
87 | 87 | |
88 | - foreach($this->getPoints() as $point) { |
|
88 | + foreach ($this->getPoints() as $point) { |
|
89 | 89 | $lineString = new LineString(array($centroid, $point)); |
90 | 90 | |
91 | - if($lineString->length() > $max['length']) { |
|
91 | + if ($lineString->length() > $max['length']) { |
|
92 | 92 | $max['length'] = $lineString->length(); |
93 | 93 | $max['point'] = $point; |
94 | 94 | } |
@@ -104,11 +104,11 @@ discard block |
||
104 | 104 | |
105 | 105 | public function numInteriorRings() { |
106 | 106 | if ($this->isEmpty()) return 0; |
107 | - return $this->numGeometries()-1; |
|
107 | + return $this->numGeometries() - 1; |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | public function interiorRingN($n) { |
111 | - return $this->geometryN($n+1); |
|
111 | + return $this->geometryN($n + 1); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | public function dimension() { |
@@ -157,8 +157,8 @@ discard block |
||
157 | 157 | $intersections = 0; |
158 | 158 | $vertices_count = count($vertices); |
159 | 159 | |
160 | - for ($i=1; $i < $vertices_count; $i++) { |
|
161 | - $vertex1 = $vertices[$i-1]; |
|
160 | + for ($i = 1; $i < $vertices_count; $i++) { |
|
161 | + $vertex1 = $vertices[$i - 1]; |
|
162 | 162 | $vertex2 = $vertices[$i]; |
163 | 163 | if ($vertex1->y() == $vertex2->y() |
164 | 164 | && $vertex1->y() == $point->y() |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | && $point->x() <= max($vertex1->x(), $vertex2->x()) |
173 | 173 | && $vertex1->y() != $vertex2->y()) { |
174 | 174 | $xinters = |
175 | - ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
|
175 | + ($point->y() - $vertex1->y())*($vertex2->x() - $vertex1->x()) |
|
176 | 176 | / ($vertex2->y() - $vertex1->y()) |
177 | 177 | + $vertex1->x(); |
178 | 178 | if ($xinters == $point->x()) { |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | } |
186 | 186 | } |
187 | 187 | // If the number of edges we passed through is even, then it's in the polygon. |
188 | - if ($intersections % 2 != 0) { |
|
188 | + if ($intersections%2 != 0) { |
|
189 | 189 | return TRUE; |
190 | 190 | } |
191 | 191 | else { |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | } |
195 | 195 | |
196 | 196 | public function pointOnVertex($point) { |
197 | - foreach($this->getPoints() as $vertex) { |
|
197 | + foreach ($this->getPoints() as $vertex) { |
|
198 | 198 | if ($point->equals($vertex)) { |
199 | 199 | return true; |
200 | 200 | } |
@@ -9,7 +9,9 @@ discard block |
||
9 | 9 | protected $geom_type = 'Polygon'; |
10 | 10 | |
11 | 11 | public function area($exterior_only = FALSE, $signed = FALSE) { |
12 | - if ($this->isEmpty()) return 0; |
|
12 | + if ($this->isEmpty()) { |
|
13 | + return 0; |
|
14 | + } |
|
13 | 15 | |
14 | 16 | if ($this->geos() && $exterior_only == FALSE) { |
15 | 17 | return $this->geos()->area(); |
@@ -19,15 +21,20 @@ discard block |
||
19 | 21 | $pts = $exterior_ring->getComponents(); |
20 | 22 | |
21 | 23 | $c = count($pts); |
22 | - if((int)$c == '0') return NULL; |
|
24 | + if((int)$c == '0') { |
|
25 | + return NULL; |
|
26 | + } |
|
23 | 27 | $a = '0'; |
24 | 28 | foreach($pts as $k => $p){ |
25 | 29 | $j = ($k + 1) % $c; |
26 | 30 | $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
27 | 31 | } |
28 | 32 | |
29 | - if ($signed) $area = ($a / 2); |
|
30 | - else $area = abs(($a / 2)); |
|
33 | + if ($signed) { |
|
34 | + $area = ($a / 2); |
|
35 | + } else { |
|
36 | + $area = abs(($a / 2)); |
|
37 | + } |
|
31 | 38 | |
32 | 39 | if ($exterior_only == TRUE) { |
33 | 40 | return $area; |
@@ -42,7 +49,9 @@ discard block |
||
42 | 49 | } |
43 | 50 | |
44 | 51 | public function centroid() { |
45 | - if ($this->isEmpty()) return NULL; |
|
52 | + if ($this->isEmpty()) { |
|
53 | + return NULL; |
|
54 | + } |
|
46 | 55 | |
47 | 56 | if ($this->geos()) { |
48 | 57 | return geoPHP::geosToGeometry($this->geos()->centroid()); |
@@ -52,7 +61,9 @@ discard block |
||
52 | 61 | $pts = $exterior_ring->getComponents(); |
53 | 62 | |
54 | 63 | $c = count($pts); |
55 | - if((int)$c == '0') return NULL; |
|
64 | + if((int)$c == '0') { |
|
65 | + return NULL; |
|
66 | + } |
|
56 | 67 | $cn = array('x' => '0', 'y' => '0'); |
57 | 68 | $a = $this->area(TRUE, TRUE); |
58 | 69 | |
@@ -98,12 +109,16 @@ discard block |
||
98 | 109 | } |
99 | 110 | |
100 | 111 | public function exteriorRing() { |
101 | - if ($this->isEmpty()) return new LineString(); |
|
112 | + if ($this->isEmpty()) { |
|
113 | + return new LineString(); |
|
114 | + } |
|
102 | 115 | return $this->components[0]; |
103 | 116 | } |
104 | 117 | |
105 | 118 | public function numInteriorRings() { |
106 | - if ($this->isEmpty()) return 0; |
|
119 | + if ($this->isEmpty()) { |
|
120 | + return 0; |
|
121 | + } |
|
107 | 122 | return $this->numGeometries()-1; |
108 | 123 | } |
109 | 124 | |
@@ -112,7 +127,9 @@ discard block |
||
112 | 127 | } |
113 | 128 | |
114 | 129 | public function dimension() { |
115 | - if ($this->isEmpty()) return 0; |
|
130 | + if ($this->isEmpty()) { |
|
131 | + return 0; |
|
132 | + } |
|
116 | 133 | return 2; |
117 | 134 | } |
118 | 135 | |
@@ -187,8 +204,7 @@ discard block |
||
187 | 204 | // If the number of edges we passed through is even, then it's in the polygon. |
188 | 205 | if ($intersections % 2 != 0) { |
189 | 206 | return TRUE; |
190 | - } |
|
191 | - else { |
|
207 | + } else { |
|
192 | 208 | return FALSE; |
193 | 209 | } |
194 | 210 | } |
@@ -41,6 +41,10 @@ |
||
41 | 41 | print "Testing Done!"; |
42 | 42 | } |
43 | 43 | |
44 | +/** |
|
45 | + * @param resource $connection |
|
46 | + * @param string $format |
|
47 | + */ |
|
44 | 48 | function test_postgis($name, $type, $geom, $connection, $format) { |
45 | 49 | global $table; |
46 | 50 |
@@ -25,18 +25,18 @@ discard block |
||
25 | 25 | // ---------------------------- |
26 | 26 | |
27 | 27 | foreach (scandir('./input') as $file) { |
28 | - $parts = explode('.',$file); |
|
29 | - if ($parts[0]) { |
|
30 | - $name = $parts[0]; |
|
31 | - $format = $parts[1]; |
|
32 | - $value = file_get_contents('./input/'.$file); |
|
33 | - print '---- Testing '.$file."\n"; |
|
34 | - flush(); |
|
35 | - $geometry = geoPHP::load($value, $format); |
|
36 | - test_postgis($name, $format, $geometry, $connection, 'wkb'); |
|
37 | - $geometry->setSRID(4326); |
|
38 | - test_postgis($name, $format, $geometry, $connection, 'ewkb'); |
|
39 | - } |
|
28 | + $parts = explode('.',$file); |
|
29 | + if ($parts[0]) { |
|
30 | + $name = $parts[0]; |
|
31 | + $format = $parts[1]; |
|
32 | + $value = file_get_contents('./input/'.$file); |
|
33 | + print '---- Testing '.$file."\n"; |
|
34 | + flush(); |
|
35 | + $geometry = geoPHP::load($value, $format); |
|
36 | + test_postgis($name, $format, $geometry, $connection, 'wkb'); |
|
37 | + $geometry->setSRID(4326); |
|
38 | + test_postgis($name, $format, $geometry, $connection, 'ewkb'); |
|
39 | + } |
|
40 | 40 | } |
41 | 41 | print "Testing Done!"; |
42 | 42 | } |
@@ -51,32 +51,32 @@ discard block |
||
51 | 51 | // SELECT using asBinary PostGIS |
52 | 52 | $result = pg_fetch_all(pg_query($connection, "SELECT asBinary(geom) as geom FROM $table WHERE name='$name'")); |
53 | 53 | foreach ($result as $item) { |
54 | - $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob |
|
55 | - $geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object |
|
54 | + $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob |
|
55 | + $geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | // SELECT and INSERT directly, with no wrapping functions |
59 | 59 | $result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM $table WHERE name='$name'")); |
60 | 60 | foreach ($result as $item) { |
61 | - $wkb = pack('H*',$item['geom']); // Unpacking the hex blob |
|
62 | - $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry |
|
61 | + $wkb = pack('H*',$item['geom']); // Unpacking the hex blob |
|
62 | + $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry |
|
63 | 63 | |
64 | - // Let's re-insert directly into postGIS |
|
65 | - // We need to unpack the WKB |
|
66 | - $unpacked = unpack('H*', $geom->out($format)); |
|
67 | - $insert_string = $unpacked[1]; |
|
68 | - pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')"); |
|
64 | + // Let's re-insert directly into postGIS |
|
65 | + // We need to unpack the WKB |
|
66 | + $unpacked = unpack('H*', $geom->out($format)); |
|
67 | + $insert_string = $unpacked[1]; |
|
68 | + pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')"); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | // SELECT and INSERT using as EWKT (ST_GeomFromEWKT and ST_AsEWKT) |
72 | 72 | $result = pg_fetch_all(pg_query($connection, "SELECT ST_AsEWKT(geom) as geom FROM $table WHERE name='$name'")); |
73 | 73 | foreach ($result as $item) { |
74 | - $wkt = $item['geom']; // Make sure to unescape the hex blob |
|
75 | - $geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object |
|
74 | + $wkt = $item['geom']; // Make sure to unescape the hex blob |
|
75 | + $geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object |
|
76 | 76 | |
77 | - // Let's re-insert directly into postGIS |
|
78 | - $insert_string = $geom->out('ewkt'); |
|
79 | - pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))"); |
|
77 | + // Let's re-insert directly into postGIS |
|
78 | + $insert_string = $geom->out('ewkt'); |
|
79 | + pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))"); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | include_once('../geoPHP.inc'); |
10 | 10 | |
11 | 11 | // Your database test table should contain 3 columns: name (text), type (text), geom (geometry) |
12 | - $host = 'localhost'; |
|
12 | + $host = 'localhost'; |
|
13 | 13 | $database = 'phayes'; |
14 | - $table = 'test'; |
|
15 | - $column = 'geom'; |
|
16 | - $user = 'phayes'; |
|
17 | - $pass = 'supersecret'; |
|
14 | + $table = 'test'; |
|
15 | + $column = 'geom'; |
|
16 | + $user = 'phayes'; |
|
17 | + $pass = 'supersecret'; |
|
18 | 18 | |
19 | 19 | $connection = pg_connect("host=$host dbname=$database user=$user password=$pass"); |
20 | 20 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | // ---------------------------- |
26 | 26 | |
27 | 27 | foreach (scandir('./input') as $file) { |
28 | - $parts = explode('.',$file); |
|
28 | + $parts = explode('.', $file); |
|
29 | 29 | if ($parts[0]) { |
30 | 30 | $name = $parts[0]; |
31 | 31 | $format = $parts[1]; |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | // SELECT and INSERT directly, with no wrapping functions |
59 | 59 | $result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM $table WHERE name='$name'")); |
60 | 60 | foreach ($result as $item) { |
61 | - $wkb = pack('H*',$item['geom']); // Unpacking the hex blob |
|
61 | + $wkb = pack('H*', $item['geom']); // Unpacking the hex blob |
|
62 | 62 | $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry |
63 | 63 | |
64 | 64 | // Let's re-insert directly into postGIS |
@@ -112,6 +112,9 @@ discard block |
||
112 | 112 | $geometry->m(); |
113 | 113 | } |
114 | 114 | |
115 | +/** |
|
116 | + * @param string $input |
|
117 | + */ |
|
115 | 118 | function test_adapters($geometry, $format, $input) { |
116 | 119 | // Test adapter output and input. Do a round-trip and re-test |
117 | 120 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
@@ -234,6 +237,9 @@ discard block |
||
234 | 237 | } |
235 | 238 | } |
236 | 239 | |
240 | +/** |
|
241 | + * @param string $value |
|
242 | + */ |
|
237 | 243 | function test_detection($value, $format, $file) { |
238 | 244 | $detected = geoPHP::detectFormat($value); |
239 | 245 | if ($detected != $format) { |
@@ -11,24 +11,24 @@ discard block |
||
11 | 11 | include_once('../geoPHP.inc'); |
12 | 12 | |
13 | 13 | if (geoPHP::geosInstalled()) { |
14 | - print "GEOS is installed.\n"; |
|
14 | + print "GEOS is installed.\n"; |
|
15 | 15 | } |
16 | 16 | else { |
17 | - print "GEOS is not installed.\n"; |
|
17 | + print "GEOS is not installed.\n"; |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | foreach (scandir('./input') as $file) { |
21 | - $parts = explode('.',$file); |
|
22 | - if ($parts[0]) { |
|
23 | - $format = $parts[1]; |
|
24 | - $value = file_get_contents('./input/'.$file); |
|
25 | - print '---- Testing '.$file."\n"; |
|
26 | - $geometry = geoPHP::load($value, $format); |
|
27 | - test_adapters($geometry, $format, $value); |
|
28 | - test_methods($geometry); |
|
29 | - test_geometry($geometry); |
|
30 | - test_detection($value, $format, $file); |
|
31 | - } |
|
21 | + $parts = explode('.',$file); |
|
22 | + if ($parts[0]) { |
|
23 | + $format = $parts[1]; |
|
24 | + $value = file_get_contents('./input/'.$file); |
|
25 | + print '---- Testing '.$file."\n"; |
|
26 | + $geometry = geoPHP::load($value, $format); |
|
27 | + test_adapters($geometry, $format, $value); |
|
28 | + test_methods($geometry); |
|
29 | + test_geometry($geometry); |
|
30 | + test_detection($value, $format, $file); |
|
31 | + } |
|
32 | 32 | } |
33 | 33 | print "Testing Done!"; |
34 | 34 | } |
@@ -115,21 +115,21 @@ discard block |
||
115 | 115 | function test_adapters($geometry, $format, $input) { |
116 | 116 | // Test adapter output and input. Do a round-trip and re-test |
117 | 117 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
118 | - if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
119 | - $output = $geometry->out($adapter_key); |
|
120 | - if ($output) { |
|
121 | - $adapter_loader = new $adapter_class(); |
|
122 | - $test_geom_1 = $adapter_loader->read($output); |
|
123 | - $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key)); |
|
124 | - |
|
125 | - if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
126 | - print "Mismatched adapter output in ".$adapter_class."\n"; |
|
127 | - } |
|
128 | - } |
|
129 | - else { |
|
130 | - print "Empty output on " . $adapter_key . "\n"; |
|
131 | - } |
|
132 | - } |
|
118 | + if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
119 | + $output = $geometry->out($adapter_key); |
|
120 | + if ($output) { |
|
121 | + $adapter_loader = new $adapter_class(); |
|
122 | + $test_geom_1 = $adapter_loader->read($output); |
|
123 | + $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key)); |
|
124 | + |
|
125 | + if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
126 | + print "Mismatched adapter output in ".$adapter_class."\n"; |
|
127 | + } |
|
128 | + } |
|
129 | + else { |
|
130 | + print "Empty output on " . $adapter_key . "\n"; |
|
131 | + } |
|
132 | + } |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | // Test to make sure adapter work the same wether GEOS is ON or OFF |
@@ -137,30 +137,30 @@ discard block |
||
137 | 137 | if (!geoPHP::geosInstalled()) return; |
138 | 138 | |
139 | 139 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
140 | - if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
141 | - // Turn GEOS on |
|
142 | - geoPHP::geosInstalled(TRUE); |
|
140 | + if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
141 | + // Turn GEOS on |
|
142 | + geoPHP::geosInstalled(TRUE); |
|
143 | 143 | |
144 | - $output = $geometry->out($adapter_key); |
|
145 | - if ($output) { |
|
146 | - $adapter_loader = new $adapter_class(); |
|
144 | + $output = $geometry->out($adapter_key); |
|
145 | + if ($output) { |
|
146 | + $adapter_loader = new $adapter_class(); |
|
147 | 147 | |
148 | - $test_geom_1 = $adapter_loader->read($output); |
|
148 | + $test_geom_1 = $adapter_loader->read($output); |
|
149 | 149 | |
150 | - // Turn GEOS off |
|
151 | - geoPHP::geosInstalled(FALSE); |
|
150 | + // Turn GEOS off |
|
151 | + geoPHP::geosInstalled(FALSE); |
|
152 | 152 | |
153 | - $test_geom_2 = $adapter_loader->read($output); |
|
153 | + $test_geom_2 = $adapter_loader->read($output); |
|
154 | 154 | |
155 | - // Turn GEOS back On |
|
156 | - geoPHP::geosInstalled(TRUE); |
|
155 | + // Turn GEOS back On |
|
156 | + geoPHP::geosInstalled(TRUE); |
|
157 | 157 | |
158 | - // Check to make sure a both are the same with geos and without |
|
159 | - if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
160 | - print "Mismatched adapter output between GEOS and NORM in ".$adapter_class."\n"; |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
158 | + // Check to make sure a both are the same with geos and without |
|
159 | + if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
160 | + print "Mismatched adapter output between GEOS and NORM in ".$adapter_class."\n"; |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | 164 | } |
165 | 165 | } |
166 | 166 | |
@@ -170,75 +170,75 @@ discard block |
||
170 | 170 | if (!geoPHP::geosInstalled()) return; |
171 | 171 | |
172 | 172 | $methods = array( |
173 | - //'boundary', //@@TODO: Uncomment this and fix errors |
|
174 | - 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
175 | - 'getBBox', |
|
176 | - 'x', |
|
177 | - 'y', |
|
178 | - 'startPoint', |
|
179 | - 'endPoint', |
|
180 | - 'isRing', |
|
181 | - 'isClosed', |
|
182 | - 'numPoints', |
|
173 | + //'boundary', //@@TODO: Uncomment this and fix errors |
|
174 | + 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
175 | + 'getBBox', |
|
176 | + 'x', |
|
177 | + 'y', |
|
178 | + 'startPoint', |
|
179 | + 'endPoint', |
|
180 | + 'isRing', |
|
181 | + 'isClosed', |
|
182 | + 'numPoints', |
|
183 | 183 | ); |
184 | 184 | |
185 | 185 | foreach ($methods as $method) { |
186 | - // Turn GEOS on |
|
187 | - geoPHP::geosInstalled(TRUE); |
|
188 | - $geos_result = $geometry->$method(); |
|
189 | - |
|
190 | - // Turn GEOS off |
|
191 | - geoPHP::geosInstalled(FALSE); |
|
192 | - $norm_result = $geometry->$method(); |
|
193 | - |
|
194 | - // Turn GEOS back On |
|
195 | - geoPHP::geosInstalled(TRUE); |
|
196 | - |
|
197 | - $geos_type = gettype($geos_result); |
|
198 | - $norm_type = gettype($norm_result); |
|
199 | - |
|
200 | - if ($geos_type != $norm_type) { |
|
201 | - print 'Type mismatch on '.$method."\n"; |
|
202 | - continue; |
|
203 | - } |
|
204 | - |
|
205 | - // Now check base on type |
|
206 | - if ($geos_type == 'object') { |
|
207 | - $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
208 | - |
|
209 | - // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
210 | - // Using Pythagorean theorem |
|
211 | - $bb = $geos_result->getBBox(); |
|
212 | - $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
213 | - |
|
214 | - // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
215 | - if ($haus_dist / $scale > 0.5) { |
|
216 | - print 'Output mismatch on '.$method.":\n"; |
|
217 | - print 'GEOS : '.$geos_result->out('wkt')."\n"; |
|
218 | - print 'NORM : '.$norm_result->out('wkt')."\n"; |
|
219 | - continue; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
224 | - if ($geos_result !== $norm_result) { |
|
225 | - print 'Output mismatch on '.$method.":\n"; |
|
226 | - print 'GEOS : '.(string) $geos_result."\n"; |
|
227 | - print 'NORM : '.(string) $norm_result."\n"; |
|
228 | - continue; |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - //@@TODO: Run tests for output of types arrays and float |
|
233 | - //@@TODO: centroid function is non-compliant for collections and strings |
|
186 | + // Turn GEOS on |
|
187 | + geoPHP::geosInstalled(TRUE); |
|
188 | + $geos_result = $geometry->$method(); |
|
189 | + |
|
190 | + // Turn GEOS off |
|
191 | + geoPHP::geosInstalled(FALSE); |
|
192 | + $norm_result = $geometry->$method(); |
|
193 | + |
|
194 | + // Turn GEOS back On |
|
195 | + geoPHP::geosInstalled(TRUE); |
|
196 | + |
|
197 | + $geos_type = gettype($geos_result); |
|
198 | + $norm_type = gettype($norm_result); |
|
199 | + |
|
200 | + if ($geos_type != $norm_type) { |
|
201 | + print 'Type mismatch on '.$method."\n"; |
|
202 | + continue; |
|
203 | + } |
|
204 | + |
|
205 | + // Now check base on type |
|
206 | + if ($geos_type == 'object') { |
|
207 | + $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
208 | + |
|
209 | + // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
210 | + // Using Pythagorean theorem |
|
211 | + $bb = $geos_result->getBBox(); |
|
212 | + $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
213 | + |
|
214 | + // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
215 | + if ($haus_dist / $scale > 0.5) { |
|
216 | + print 'Output mismatch on '.$method.":\n"; |
|
217 | + print 'GEOS : '.$geos_result->out('wkt')."\n"; |
|
218 | + print 'NORM : '.$norm_result->out('wkt')."\n"; |
|
219 | + continue; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
224 | + if ($geos_result !== $norm_result) { |
|
225 | + print 'Output mismatch on '.$method.":\n"; |
|
226 | + print 'GEOS : '.(string) $geos_result."\n"; |
|
227 | + print 'NORM : '.(string) $norm_result."\n"; |
|
228 | + continue; |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + //@@TODO: Run tests for output of types arrays and float |
|
233 | + //@@TODO: centroid function is non-compliant for collections and strings |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | |
237 | 237 | function test_detection($value, $format, $file) { |
238 | 238 | $detected = geoPHP::detectFormat($value); |
239 | 239 | if ($detected != $format) { |
240 | - if ($detected) print 'detected as ' . $detected . "\n"; |
|
241 | - else print "not detected\n"; |
|
240 | + if ($detected) print 'detected as ' . $detected . "\n"; |
|
241 | + else print "not detected\n"; |
|
242 | 242 | } |
243 | 243 | // Make sure it loads using auto-detect |
244 | 244 | geoPHP::load($value); |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | } |
19 | 19 | |
20 | 20 | foreach (scandir('./input') as $file) { |
21 | - $parts = explode('.',$file); |
|
21 | + $parts = explode('.', $file); |
|
22 | 22 | if ($parts[0]) { |
23 | 23 | $format = $parts[1]; |
24 | 24 | $value = file_get_contents('./input/'.$file); |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | $geometry->difference($geometry); |
89 | 89 | $geometry->symDifference($geometry); |
90 | 90 | $geometry->union($geometry); |
91 | - $geometry->simplify(0);// @@TODO: Adjust this once we can deal with empty geometries |
|
91 | + $geometry->simplify(0); // @@TODO: Adjust this once we can deal with empty geometries |
|
92 | 92 | $geometry->disjoint($geometry); |
93 | 93 | $geometry->touches($geometry); |
94 | 94 | $geometry->intersects($geometry); |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } |
128 | 128 | } |
129 | 129 | else { |
130 | - print "Empty output on " . $adapter_key . "\n"; |
|
130 | + print "Empty output on ".$adapter_key."\n"; |
|
131 | 131 | } |
132 | 132 | } |
133 | 133 | } |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | |
172 | 172 | $methods = array( |
173 | 173 | //'boundary', //@@TODO: Uncomment this and fix errors |
174 | - 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
174 | + 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
175 | 175 | 'getBBox', |
176 | 176 | 'x', |
177 | 177 | 'y', |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | |
205 | 205 | // Now check base on type |
206 | 206 | if ($geos_type == 'object') { |
207 | - $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
207 | + $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'), 'wkt')); |
|
208 | 208 | |
209 | 209 | // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
210 | 210 | // Using Pythagorean theorem |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
213 | 213 | |
214 | 214 | // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
215 | - if ($haus_dist / $scale > 0.5) { |
|
215 | + if ($haus_dist/$scale > 0.5) { |
|
216 | 216 | print 'Output mismatch on '.$method.":\n"; |
217 | 217 | print 'GEOS : '.$geos_result->out('wkt')."\n"; |
218 | 218 | print 'NORM : '.$norm_result->out('wkt')."\n"; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | function test_detection($value, $format, $file) { |
238 | 238 | $detected = geoPHP::detectFormat($value); |
239 | 239 | if ($detected != $format) { |
240 | - if ($detected) print 'detected as ' . $detected . "\n"; |
|
240 | + if ($detected) print 'detected as '.$detected."\n"; |
|
241 | 241 | else print "not detected\n"; |
242 | 242 | } |
243 | 243 | // Make sure it loads using auto-detect |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | if (geoPHP::geosInstalled()) { |
14 | 14 | print "GEOS is installed.\n"; |
15 | - } |
|
16 | - else { |
|
15 | + } else { |
|
17 | 16 | print "GEOS is not installed.\n"; |
18 | 17 | } |
19 | 18 | |
@@ -125,8 +124,7 @@ discard block |
||
125 | 124 | if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
126 | 125 | print "Mismatched adapter output in ".$adapter_class."\n"; |
127 | 126 | } |
128 | - } |
|
129 | - else { |
|
127 | + } else { |
|
130 | 128 | print "Empty output on " . $adapter_key . "\n"; |
131 | 129 | } |
132 | 130 | } |
@@ -134,7 +132,9 @@ discard block |
||
134 | 132 | |
135 | 133 | // Test to make sure adapter work the same wether GEOS is ON or OFF |
136 | 134 | // Cannot test methods if GEOS is not intstalled |
137 | - if (!geoPHP::geosInstalled()) return; |
|
135 | + if (!geoPHP::geosInstalled()) { |
|
136 | + return; |
|
137 | + } |
|
138 | 138 | |
139 | 139 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
140 | 140 | if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
@@ -167,7 +167,9 @@ discard block |
||
167 | 167 | |
168 | 168 | function test_methods($geometry) { |
169 | 169 | // Cannot test methods if GEOS is not intstalled |
170 | - if (!geoPHP::geosInstalled()) return; |
|
170 | + if (!geoPHP::geosInstalled()) { |
|
171 | + return; |
|
172 | + } |
|
171 | 173 | |
172 | 174 | $methods = array( |
173 | 175 | //'boundary', //@@TODO: Uncomment this and fix errors |
@@ -237,8 +239,11 @@ discard block |
||
237 | 239 | function test_detection($value, $format, $file) { |
238 | 240 | $detected = geoPHP::detectFormat($value); |
239 | 241 | if ($detected != $format) { |
240 | - if ($detected) print 'detected as ' . $detected . "\n"; |
|
241 | - else print "not detected\n"; |
|
242 | + if ($detected) { |
|
243 | + print 'detected as ' . $detected . "\n"; |
|
244 | + } else { |
|
245 | + print "not detected\n"; |
|
246 | + } |
|
242 | 247 | } |
243 | 248 | // Make sure it loads using auto-detect |
244 | 249 | geoPHP::load($value); |
@@ -58,6 +58,10 @@ |
||
58 | 58 | } |
59 | 59 | } |
60 | 60 | |
61 | + /** |
|
62 | + * @param string $method_name |
|
63 | + * @param string|null $argument |
|
64 | + */ |
|
61 | 65 | function _methods_tester($geometry, $method_name, $argument) { |
62 | 66 | |
63 | 67 | if (!method_exists($geometry, $method_name)) { |
@@ -9,357 +9,357 @@ |
||
9 | 9 | } |
10 | 10 | |
11 | 11 | function testMethods() { |
12 | - foreach (scandir('./input') as $file) { |
|
13 | - $parts = explode('.',$file); |
|
14 | - if ($parts[0]) { |
|
15 | - $format = $parts[1]; |
|
16 | - $value = file_get_contents('./input/'.$file); |
|
17 | - $geometry = geoPHP::load($value, $format); |
|
12 | + foreach (scandir('./input') as $file) { |
|
13 | + $parts = explode('.',$file); |
|
14 | + if ($parts[0]) { |
|
15 | + $format = $parts[1]; |
|
16 | + $value = file_get_contents('./input/'.$file); |
|
17 | + $geometry = geoPHP::load($value, $format); |
|
18 | 18 | |
19 | - $methods = array( |
|
20 | - array('name' => 'area'), |
|
21 | - array('name' => 'boundary'), |
|
22 | - array('name' => 'getBBox'), |
|
23 | - array('name' => 'centroid'), |
|
24 | - array('name' => 'length'), |
|
25 | - array('name' => 'greatCircleLength'), |
|
26 | - array('name' => 'haversineLength'), |
|
27 | - array('name' => 'y'), |
|
28 | - array('name' => 'x'), |
|
29 | - array('name' => 'numGeometries'), |
|
30 | - array('name' => 'geometryN', 'argument' => '1'), |
|
31 | - array('name' => 'startPoint'), |
|
32 | - array('name' => 'endPoint'), |
|
33 | - array('name' => 'isRing'), |
|
34 | - array('name' => 'isClosed'), |
|
35 | - array('name' => 'numPoints'), |
|
36 | - array('name' => 'pointN', 'argument' => '1'), |
|
37 | - array('name' => 'exteriorRing'), |
|
38 | - array('name' => 'numInteriorRings'), |
|
39 | - array('name' => 'interiorRingN', 'argument' => '1'), |
|
40 | - array('name' => 'dimension'), |
|
41 | - array('name' => 'geometryType'), |
|
42 | - array('name' => 'SRID'), |
|
43 | - array('name' => 'setSRID', 'argument' => '4326'), |
|
44 | - ); |
|
19 | + $methods = array( |
|
20 | + array('name' => 'area'), |
|
21 | + array('name' => 'boundary'), |
|
22 | + array('name' => 'getBBox'), |
|
23 | + array('name' => 'centroid'), |
|
24 | + array('name' => 'length'), |
|
25 | + array('name' => 'greatCircleLength'), |
|
26 | + array('name' => 'haversineLength'), |
|
27 | + array('name' => 'y'), |
|
28 | + array('name' => 'x'), |
|
29 | + array('name' => 'numGeometries'), |
|
30 | + array('name' => 'geometryN', 'argument' => '1'), |
|
31 | + array('name' => 'startPoint'), |
|
32 | + array('name' => 'endPoint'), |
|
33 | + array('name' => 'isRing'), |
|
34 | + array('name' => 'isClosed'), |
|
35 | + array('name' => 'numPoints'), |
|
36 | + array('name' => 'pointN', 'argument' => '1'), |
|
37 | + array('name' => 'exteriorRing'), |
|
38 | + array('name' => 'numInteriorRings'), |
|
39 | + array('name' => 'interiorRingN', 'argument' => '1'), |
|
40 | + array('name' => 'dimension'), |
|
41 | + array('name' => 'geometryType'), |
|
42 | + array('name' => 'SRID'), |
|
43 | + array('name' => 'setSRID', 'argument' => '4326'), |
|
44 | + ); |
|
45 | 45 | |
46 | - foreach($methods as $method) { |
|
47 | - $argument = NULL; |
|
48 | - $method_name = $method['name']; |
|
49 | - if (isset($method['argument'])) { |
|
50 | - $argument = $method['argument']; |
|
51 | - } |
|
46 | + foreach($methods as $method) { |
|
47 | + $argument = NULL; |
|
48 | + $method_name = $method['name']; |
|
49 | + if (isset($method['argument'])) { |
|
50 | + $argument = $method['argument']; |
|
51 | + } |
|
52 | 52 | |
53 | - $this->_methods_tester($geometry, $method_name, $argument); |
|
54 | - } |
|
53 | + $this->_methods_tester($geometry, $method_name, $argument); |
|
54 | + } |
|
55 | 55 | |
56 | - $this->_methods_tester_with_geos($geometry); |
|
57 | - } |
|
58 | - } |
|
56 | + $this->_methods_tester_with_geos($geometry); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | function _methods_tester($geometry, $method_name, $argument) { |
62 | 62 | |
63 | - if (!method_exists($geometry, $method_name)) { |
|
64 | - $this->fail("Method ".$method_name.'() doesn\'t exists.'); |
|
65 | - return; |
|
66 | - } |
|
63 | + if (!method_exists($geometry, $method_name)) { |
|
64 | + $this->fail("Method ".$method_name.'() doesn\'t exists.'); |
|
65 | + return; |
|
66 | + } |
|
67 | 67 | |
68 | - switch ($method_name) { |
|
69 | - case 'y': |
|
70 | - case 'x': |
|
71 | - if ($geometry->geometryType() == 'Point') { |
|
72 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
73 | - } |
|
74 | - if ($geometry->geometryType() == 'LineString') { |
|
75 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
76 | - } |
|
77 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
78 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
79 | - } |
|
80 | - break; |
|
81 | - case 'geometryN': |
|
82 | - if ($geometry->geometryType() == 'Point') { |
|
83 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
84 | - } |
|
85 | - if ($geometry->geometryType() == 'LineString') { |
|
86 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
87 | - } |
|
88 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
89 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
90 | - } |
|
91 | - break; |
|
92 | - case 'startPoint': |
|
93 | - if ($geometry->geometryType() == 'Point') { |
|
94 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
95 | - } |
|
96 | - if ($geometry->geometryType() == 'LineString') { |
|
97 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
98 | - } |
|
99 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
100 | - //TODO: Add a method startPoint() to MultiLineString. |
|
101 | - //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
102 | - } |
|
103 | - break; |
|
104 | - case 'endPoint': |
|
105 | - if ($geometry->geometryType() == 'Point') { |
|
106 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
107 | - } |
|
108 | - if ($geometry->geometryType() == 'LineString') { |
|
109 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
110 | - } |
|
111 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
112 | - //TODO: Add a method endPoint() to MultiLineString. |
|
113 | - //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
114 | - } |
|
115 | - break; |
|
116 | - case 'isRing': |
|
117 | - if ($geometry->geometryType() == 'Point') { |
|
118 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
119 | - } |
|
120 | - if ($geometry->geometryType() == 'LineString') { |
|
121 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
122 | - } |
|
123 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
124 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
125 | - } |
|
126 | - break; |
|
127 | - case 'isClosed': |
|
128 | - if ($geometry->geometryType() == 'Point') { |
|
129 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
130 | - } |
|
131 | - if ($geometry->geometryType() == 'LineString') { |
|
132 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
133 | - } |
|
134 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
135 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
136 | - } |
|
137 | - break; |
|
138 | - case 'pointN': |
|
139 | - if ($geometry->geometryType() == 'Point') { |
|
140 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
141 | - } |
|
142 | - if ($geometry->geometryType() == 'LineString') { |
|
143 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
144 | - } |
|
145 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
146 | - //TODO: Add a method pointN() to MultiLineString. |
|
147 | - //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
148 | - } |
|
149 | - break; |
|
150 | - case 'exteriorRing': |
|
151 | - if ($geometry->geometryType() == 'Point') { |
|
152 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
153 | - } |
|
154 | - if ($geometry->geometryType() == 'LineString') { |
|
155 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
156 | - } |
|
157 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
158 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
159 | - } |
|
160 | - break; |
|
161 | - case 'numInteriorRings': |
|
162 | - if ($geometry->geometryType() == 'Point') { |
|
163 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
164 | - } |
|
165 | - if ($geometry->geometryType() == 'LineString') { |
|
166 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
167 | - } |
|
168 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
169 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
170 | - } |
|
171 | - break; |
|
172 | - case 'interiorRingN': |
|
173 | - if ($geometry->geometryType() == 'Point') { |
|
174 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
175 | - } |
|
176 | - if ($geometry->geometryType() == 'LineString') { |
|
177 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
178 | - } |
|
179 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
180 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
181 | - } |
|
182 | - break; |
|
183 | - case 'SRID': |
|
184 | - if ($geometry->geometryType() == 'Point') { |
|
185 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
186 | - } |
|
187 | - if ($geometry->geometryType() == 'LineString') { |
|
188 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
189 | - } |
|
190 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
191 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
192 | - } |
|
193 | - break; |
|
194 | - case 'getBBox': |
|
195 | - if ($geometry->geometryType() == 'Point') { |
|
196 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
197 | - } |
|
198 | - if ($geometry->geometryType() == 'LineString') { |
|
199 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
200 | - } |
|
201 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
202 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
203 | - } |
|
204 | - break; |
|
205 | - case 'centroid': |
|
206 | - if ($geometry->geometryType() == 'Point') { |
|
207 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
208 | - } |
|
209 | - if ($geometry->geometryType() == 'LineString') { |
|
210 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
211 | - } |
|
212 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
213 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
214 | - } |
|
215 | - break; |
|
216 | - case 'length': |
|
217 | - if ($geometry->geometryType() == 'Point') { |
|
218 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
219 | - } |
|
220 | - if ($geometry->geometryType() == 'LineString') { |
|
221 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
222 | - } |
|
223 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
224 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
225 | - } |
|
226 | - break; |
|
227 | - case 'numGeometries': |
|
228 | - if ($geometry->geometryType() == 'Point') { |
|
229 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
230 | - } |
|
231 | - if ($geometry->geometryType() == 'LineString') { |
|
232 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
233 | - } |
|
234 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
235 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
236 | - } |
|
237 | - break; |
|
238 | - case 'numPoints': |
|
239 | - if ($geometry->geometryType() == 'Point') { |
|
240 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
241 | - } |
|
242 | - if ($geometry->geometryType() == 'LineString') { |
|
243 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
244 | - } |
|
245 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
246 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
247 | - } |
|
248 | - break; |
|
249 | - case 'dimension': |
|
250 | - if ($geometry->geometryType() == 'Point') { |
|
251 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
252 | - } |
|
253 | - if ($geometry->geometryType() == 'LineString') { |
|
254 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
255 | - } |
|
256 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
257 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
258 | - } |
|
259 | - break; |
|
260 | - case 'boundary': |
|
261 | - if ($geometry->geometryType() == 'Point') { |
|
262 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
263 | - } |
|
264 | - if ($geometry->geometryType() == 'LineString') { |
|
265 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
266 | - } |
|
267 | - if ($geometry->geometryType() == 'MultiLineString') { |
|
268 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
269 | - } |
|
270 | - break; |
|
271 | - case 'haversineLength': |
|
272 | - //TODO: Check if output is a float >= 0. |
|
273 | - //TODO: Sometimes haversineLength() returns NAN, needs to check why. |
|
274 | - break; |
|
275 | - case 'greatCircleLength': |
|
276 | - case 'area': |
|
277 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
278 | - break; |
|
279 | - case 'geometryType': |
|
280 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
281 | - break; |
|
282 | - case 'setSRID': |
|
283 | - //TODO: The method setSRID() should return TRUE. |
|
284 | - break; |
|
285 | - default: |
|
286 | - $this->assertTrue($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
287 | - } |
|
68 | + switch ($method_name) { |
|
69 | + case 'y': |
|
70 | + case 'x': |
|
71 | + if ($geometry->geometryType() == 'Point') { |
|
72 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
73 | + } |
|
74 | + if ($geometry->geometryType() == 'LineString') { |
|
75 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
76 | + } |
|
77 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
78 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
79 | + } |
|
80 | + break; |
|
81 | + case 'geometryN': |
|
82 | + if ($geometry->geometryType() == 'Point') { |
|
83 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
84 | + } |
|
85 | + if ($geometry->geometryType() == 'LineString') { |
|
86 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
87 | + } |
|
88 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
89 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
90 | + } |
|
91 | + break; |
|
92 | + case 'startPoint': |
|
93 | + if ($geometry->geometryType() == 'Point') { |
|
94 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
95 | + } |
|
96 | + if ($geometry->geometryType() == 'LineString') { |
|
97 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
98 | + } |
|
99 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
100 | + //TODO: Add a method startPoint() to MultiLineString. |
|
101 | + //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
102 | + } |
|
103 | + break; |
|
104 | + case 'endPoint': |
|
105 | + if ($geometry->geometryType() == 'Point') { |
|
106 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
107 | + } |
|
108 | + if ($geometry->geometryType() == 'LineString') { |
|
109 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
110 | + } |
|
111 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
112 | + //TODO: Add a method endPoint() to MultiLineString. |
|
113 | + //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
114 | + } |
|
115 | + break; |
|
116 | + case 'isRing': |
|
117 | + if ($geometry->geometryType() == 'Point') { |
|
118 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
119 | + } |
|
120 | + if ($geometry->geometryType() == 'LineString') { |
|
121 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
122 | + } |
|
123 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
124 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
125 | + } |
|
126 | + break; |
|
127 | + case 'isClosed': |
|
128 | + if ($geometry->geometryType() == 'Point') { |
|
129 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
130 | + } |
|
131 | + if ($geometry->geometryType() == 'LineString') { |
|
132 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
133 | + } |
|
134 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
135 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
136 | + } |
|
137 | + break; |
|
138 | + case 'pointN': |
|
139 | + if ($geometry->geometryType() == 'Point') { |
|
140 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
141 | + } |
|
142 | + if ($geometry->geometryType() == 'LineString') { |
|
143 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
144 | + } |
|
145 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
146 | + //TODO: Add a method pointN() to MultiLineString. |
|
147 | + //$this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
148 | + } |
|
149 | + break; |
|
150 | + case 'exteriorRing': |
|
151 | + if ($geometry->geometryType() == 'Point') { |
|
152 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
153 | + } |
|
154 | + if ($geometry->geometryType() == 'LineString') { |
|
155 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
156 | + } |
|
157 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
158 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
159 | + } |
|
160 | + break; |
|
161 | + case 'numInteriorRings': |
|
162 | + if ($geometry->geometryType() == 'Point') { |
|
163 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
164 | + } |
|
165 | + if ($geometry->geometryType() == 'LineString') { |
|
166 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
167 | + } |
|
168 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
169 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
170 | + } |
|
171 | + break; |
|
172 | + case 'interiorRingN': |
|
173 | + if ($geometry->geometryType() == 'Point') { |
|
174 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
175 | + } |
|
176 | + if ($geometry->geometryType() == 'LineString') { |
|
177 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
178 | + } |
|
179 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
180 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
181 | + } |
|
182 | + break; |
|
183 | + case 'SRID': |
|
184 | + if ($geometry->geometryType() == 'Point') { |
|
185 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
186 | + } |
|
187 | + if ($geometry->geometryType() == 'LineString') { |
|
188 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
189 | + } |
|
190 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
191 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
192 | + } |
|
193 | + break; |
|
194 | + case 'getBBox': |
|
195 | + if ($geometry->geometryType() == 'Point') { |
|
196 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
197 | + } |
|
198 | + if ($geometry->geometryType() == 'LineString') { |
|
199 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
200 | + } |
|
201 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
202 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
203 | + } |
|
204 | + break; |
|
205 | + case 'centroid': |
|
206 | + if ($geometry->geometryType() == 'Point') { |
|
207 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
208 | + } |
|
209 | + if ($geometry->geometryType() == 'LineString') { |
|
210 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
211 | + } |
|
212 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
213 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
214 | + } |
|
215 | + break; |
|
216 | + case 'length': |
|
217 | + if ($geometry->geometryType() == 'Point') { |
|
218 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
219 | + } |
|
220 | + if ($geometry->geometryType() == 'LineString') { |
|
221 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
222 | + } |
|
223 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
224 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
225 | + } |
|
226 | + break; |
|
227 | + case 'numGeometries': |
|
228 | + if ($geometry->geometryType() == 'Point') { |
|
229 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
230 | + } |
|
231 | + if ($geometry->geometryType() == 'LineString') { |
|
232 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
233 | + } |
|
234 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
235 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
236 | + } |
|
237 | + break; |
|
238 | + case 'numPoints': |
|
239 | + if ($geometry->geometryType() == 'Point') { |
|
240 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
241 | + } |
|
242 | + if ($geometry->geometryType() == 'LineString') { |
|
243 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
244 | + } |
|
245 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
246 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
247 | + } |
|
248 | + break; |
|
249 | + case 'dimension': |
|
250 | + if ($geometry->geometryType() == 'Point') { |
|
251 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
252 | + } |
|
253 | + if ($geometry->geometryType() == 'LineString') { |
|
254 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
255 | + } |
|
256 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
257 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
258 | + } |
|
259 | + break; |
|
260 | + case 'boundary': |
|
261 | + if ($geometry->geometryType() == 'Point') { |
|
262 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
263 | + } |
|
264 | + if ($geometry->geometryType() == 'LineString') { |
|
265 | + $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
266 | + } |
|
267 | + if ($geometry->geometryType() == 'MultiLineString') { |
|
268 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
269 | + } |
|
270 | + break; |
|
271 | + case 'haversineLength': |
|
272 | + //TODO: Check if output is a float >= 0. |
|
273 | + //TODO: Sometimes haversineLength() returns NAN, needs to check why. |
|
274 | + break; |
|
275 | + case 'greatCircleLength': |
|
276 | + case 'area': |
|
277 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
278 | + break; |
|
279 | + case 'geometryType': |
|
280 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
281 | + break; |
|
282 | + case 'setSRID': |
|
283 | + //TODO: The method setSRID() should return TRUE. |
|
284 | + break; |
|
285 | + default: |
|
286 | + $this->assertTrue($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
287 | + } |
|
288 | 288 | |
289 | 289 | } |
290 | 290 | |
291 | 291 | function _methods_tester_with_geos($geometry) { |
292 | - // Cannot test methods if GEOS is not intstalled |
|
293 | - if (!geoPHP::geosInstalled()) return; |
|
292 | + // Cannot test methods if GEOS is not intstalled |
|
293 | + if (!geoPHP::geosInstalled()) return; |
|
294 | 294 | |
295 | - $methods = array( |
|
296 | - //'boundary', //@@TODO: Uncomment this and fix errors |
|
297 | - 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
298 | - 'getBBox', |
|
299 | - 'x', |
|
300 | - 'y', |
|
301 | - 'startPoint', |
|
302 | - 'endPoint', |
|
303 | - 'isRing', |
|
304 | - 'isClosed', |
|
305 | - 'numPoints', |
|
306 | - ); |
|
295 | + $methods = array( |
|
296 | + //'boundary', //@@TODO: Uncomment this and fix errors |
|
297 | + 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
298 | + 'getBBox', |
|
299 | + 'x', |
|
300 | + 'y', |
|
301 | + 'startPoint', |
|
302 | + 'endPoint', |
|
303 | + 'isRing', |
|
304 | + 'isClosed', |
|
305 | + 'numPoints', |
|
306 | + ); |
|
307 | 307 | |
308 | - foreach ($methods as $method) { |
|
309 | - // Turn GEOS on |
|
310 | - geoPHP::geosInstalled(TRUE); |
|
311 | - $geos_result = $geometry->$method(); |
|
308 | + foreach ($methods as $method) { |
|
309 | + // Turn GEOS on |
|
310 | + geoPHP::geosInstalled(TRUE); |
|
311 | + $geos_result = $geometry->$method(); |
|
312 | 312 | |
313 | - // Turn GEOS off |
|
314 | - geoPHP::geosInstalled(FALSE); |
|
315 | - $norm_result = $geometry->$method(); |
|
313 | + // Turn GEOS off |
|
314 | + geoPHP::geosInstalled(FALSE); |
|
315 | + $norm_result = $geometry->$method(); |
|
316 | 316 | |
317 | - // Turn GEOS back On |
|
318 | - geoPHP::geosInstalled(TRUE); |
|
317 | + // Turn GEOS back On |
|
318 | + geoPHP::geosInstalled(TRUE); |
|
319 | 319 | |
320 | - $geos_type = gettype($geos_result); |
|
321 | - $norm_type = gettype($norm_result); |
|
320 | + $geos_type = gettype($geos_result); |
|
321 | + $norm_type = gettype($norm_result); |
|
322 | 322 | |
323 | - if ($geos_type != $norm_type) { |
|
324 | - $this->fail('Type mismatch on '.$method); |
|
325 | - $this->dump($geos_type); |
|
326 | - $this->dump($norm_type); |
|
327 | - continue; |
|
328 | - } |
|
323 | + if ($geos_type != $norm_type) { |
|
324 | + $this->fail('Type mismatch on '.$method); |
|
325 | + $this->dump($geos_type); |
|
326 | + $this->dump($norm_type); |
|
327 | + continue; |
|
328 | + } |
|
329 | 329 | |
330 | - // Now check base on type |
|
331 | - if ($geos_type == 'object') { |
|
332 | - $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
330 | + // Now check base on type |
|
331 | + if ($geos_type == 'object') { |
|
332 | + $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
333 | 333 | |
334 | - // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
335 | - // Using Pythagorean theorem |
|
336 | - $bb = $geos_result->getBBox(); |
|
337 | - $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
334 | + // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
335 | + // Using Pythagorean theorem |
|
336 | + $bb = $geos_result->getBBox(); |
|
337 | + $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
338 | 338 | |
339 | - // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
340 | - if ($haus_dist / $scale > 0.5) { |
|
341 | - $this->fail('Output mismatch on '.$method); |
|
342 | - $this->dump('GEOS : '); |
|
343 | - $this->dump($geos_result->out('wkt')); |
|
344 | - $this->dump('NORM : '); |
|
345 | - $this->dump($norm_result->out('wkt')); |
|
346 | - continue; |
|
347 | - } |
|
348 | - } |
|
339 | + // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
340 | + if ($haus_dist / $scale > 0.5) { |
|
341 | + $this->fail('Output mismatch on '.$method); |
|
342 | + $this->dump('GEOS : '); |
|
343 | + $this->dump($geos_result->out('wkt')); |
|
344 | + $this->dump('NORM : '); |
|
345 | + $this->dump($norm_result->out('wkt')); |
|
346 | + continue; |
|
347 | + } |
|
348 | + } |
|
349 | 349 | |
350 | - if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
351 | - if ($geos_result !== $norm_result) { |
|
352 | - $this->fail('Output mismatch on '.$method); |
|
353 | - $this->dump('GEOS : '); |
|
354 | - $this->dump((string) $geos_result); |
|
355 | - $this->dump('NORM : '); |
|
356 | - $this->dump((string) $norm_result); |
|
357 | - continue; |
|
358 | - } |
|
359 | - } |
|
350 | + if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
351 | + if ($geos_result !== $norm_result) { |
|
352 | + $this->fail('Output mismatch on '.$method); |
|
353 | + $this->dump('GEOS : '); |
|
354 | + $this->dump((string) $geos_result); |
|
355 | + $this->dump('NORM : '); |
|
356 | + $this->dump((string) $norm_result); |
|
357 | + continue; |
|
358 | + } |
|
359 | + } |
|
360 | 360 | |
361 | - //@@TODO: Run tests for output of types arrays and float |
|
362 | - //@@TODO: centroid function is non-compliant for collections and strings |
|
363 | - } |
|
361 | + //@@TODO: Run tests for output of types arrays and float |
|
362 | + //@@TODO: centroid function is non-compliant for collections and strings |
|
363 | + } |
|
364 | 364 | } |
365 | 365 | } |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | |
11 | 11 | function testMethods() { |
12 | 12 | foreach (scandir('./input') as $file) { |
13 | - $parts = explode('.',$file); |
|
13 | + $parts = explode('.', $file); |
|
14 | 14 | if ($parts[0]) { |
15 | 15 | $format = $parts[1]; |
16 | 16 | $value = file_get_contents('./input/'.$file); |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | array('name' => 'setSRID', 'argument' => '4326'), |
44 | 44 | ); |
45 | 45 | |
46 | - foreach($methods as $method) { |
|
46 | + foreach ($methods as $method) { |
|
47 | 47 | $argument = NULL; |
48 | 48 | $method_name = $method['name']; |
49 | 49 | if (isset($method['argument'])) { |
@@ -69,32 +69,32 @@ discard block |
||
69 | 69 | case 'y': |
70 | 70 | case 'x': |
71 | 71 | if ($geometry->geometryType() == 'Point') { |
72 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
72 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
73 | 73 | } |
74 | 74 | if ($geometry->geometryType() == 'LineString') { |
75 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
75 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
76 | 76 | } |
77 | 77 | if ($geometry->geometryType() == 'MultiLineString') { |
78 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
78 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
79 | 79 | } |
80 | 80 | break; |
81 | 81 | case 'geometryN': |
82 | 82 | if ($geometry->geometryType() == 'Point') { |
83 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
83 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
84 | 84 | } |
85 | 85 | if ($geometry->geometryType() == 'LineString') { |
86 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
86 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
87 | 87 | } |
88 | 88 | if ($geometry->geometryType() == 'MultiLineString') { |
89 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
89 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
90 | 90 | } |
91 | 91 | break; |
92 | 92 | case 'startPoint': |
93 | 93 | if ($geometry->geometryType() == 'Point') { |
94 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
94 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
95 | 95 | } |
96 | 96 | if ($geometry->geometryType() == 'LineString') { |
97 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
97 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
98 | 98 | } |
99 | 99 | if ($geometry->geometryType() == 'MultiLineString') { |
100 | 100 | //TODO: Add a method startPoint() to MultiLineString. |
@@ -103,10 +103,10 @@ discard block |
||
103 | 103 | break; |
104 | 104 | case 'endPoint': |
105 | 105 | if ($geometry->geometryType() == 'Point') { |
106 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
106 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
107 | 107 | } |
108 | 108 | if ($geometry->geometryType() == 'LineString') { |
109 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
109 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
110 | 110 | } |
111 | 111 | if ($geometry->geometryType() == 'MultiLineString') { |
112 | 112 | //TODO: Add a method endPoint() to MultiLineString. |
@@ -115,32 +115,32 @@ discard block |
||
115 | 115 | break; |
116 | 116 | case 'isRing': |
117 | 117 | if ($geometry->geometryType() == 'Point') { |
118 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
118 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
119 | 119 | } |
120 | 120 | if ($geometry->geometryType() == 'LineString') { |
121 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
121 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
122 | 122 | } |
123 | 123 | if ($geometry->geometryType() == 'MultiLineString') { |
124 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
124 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
125 | 125 | } |
126 | 126 | break; |
127 | 127 | case 'isClosed': |
128 | 128 | if ($geometry->geometryType() == 'Point') { |
129 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
129 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
130 | 130 | } |
131 | 131 | if ($geometry->geometryType() == 'LineString') { |
132 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
132 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
133 | 133 | } |
134 | 134 | if ($geometry->geometryType() == 'MultiLineString') { |
135 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
135 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
136 | 136 | } |
137 | 137 | break; |
138 | 138 | case 'pointN': |
139 | 139 | if ($geometry->geometryType() == 'Point') { |
140 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
140 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
141 | 141 | } |
142 | 142 | if ($geometry->geometryType() == 'LineString') { |
143 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
143 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
144 | 144 | } |
145 | 145 | if ($geometry->geometryType() == 'MultiLineString') { |
146 | 146 | //TODO: Add a method pointN() to MultiLineString. |
@@ -149,123 +149,123 @@ discard block |
||
149 | 149 | break; |
150 | 150 | case 'exteriorRing': |
151 | 151 | if ($geometry->geometryType() == 'Point') { |
152 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
152 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
153 | 153 | } |
154 | 154 | if ($geometry->geometryType() == 'LineString') { |
155 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
155 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
156 | 156 | } |
157 | 157 | if ($geometry->geometryType() == 'MultiLineString') { |
158 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
158 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
159 | 159 | } |
160 | 160 | break; |
161 | 161 | case 'numInteriorRings': |
162 | 162 | if ($geometry->geometryType() == 'Point') { |
163 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
163 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
164 | 164 | } |
165 | 165 | if ($geometry->geometryType() == 'LineString') { |
166 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
166 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
167 | 167 | } |
168 | 168 | if ($geometry->geometryType() == 'MultiLineString') { |
169 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
169 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
170 | 170 | } |
171 | 171 | break; |
172 | 172 | case 'interiorRingN': |
173 | 173 | if ($geometry->geometryType() == 'Point') { |
174 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
174 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
175 | 175 | } |
176 | 176 | if ($geometry->geometryType() == 'LineString') { |
177 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
177 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
178 | 178 | } |
179 | 179 | if ($geometry->geometryType() == 'MultiLineString') { |
180 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
180 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
181 | 181 | } |
182 | 182 | break; |
183 | 183 | case 'SRID': |
184 | 184 | if ($geometry->geometryType() == 'Point') { |
185 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
185 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
186 | 186 | } |
187 | 187 | if ($geometry->geometryType() == 'LineString') { |
188 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
188 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
189 | 189 | } |
190 | 190 | if ($geometry->geometryType() == 'MultiLineString') { |
191 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
191 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
192 | 192 | } |
193 | 193 | break; |
194 | 194 | case 'getBBox': |
195 | 195 | if ($geometry->geometryType() == 'Point') { |
196 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
196 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
197 | 197 | } |
198 | 198 | if ($geometry->geometryType() == 'LineString') { |
199 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
199 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
200 | 200 | } |
201 | 201 | if ($geometry->geometryType() == 'MultiLineString') { |
202 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
202 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
203 | 203 | } |
204 | 204 | break; |
205 | 205 | case 'centroid': |
206 | 206 | if ($geometry->geometryType() == 'Point') { |
207 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
207 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
208 | 208 | } |
209 | 209 | if ($geometry->geometryType() == 'LineString') { |
210 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
210 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
211 | 211 | } |
212 | 212 | if ($geometry->geometryType() == 'MultiLineString') { |
213 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
213 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
214 | 214 | } |
215 | 215 | break; |
216 | 216 | case 'length': |
217 | 217 | if ($geometry->geometryType() == 'Point') { |
218 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
218 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
219 | 219 | } |
220 | 220 | if ($geometry->geometryType() == 'LineString') { |
221 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
221 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
222 | 222 | } |
223 | 223 | if ($geometry->geometryType() == 'MultiLineString') { |
224 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
224 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
225 | 225 | } |
226 | 226 | break; |
227 | 227 | case 'numGeometries': |
228 | 228 | if ($geometry->geometryType() == 'Point') { |
229 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
229 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
230 | 230 | } |
231 | 231 | if ($geometry->geometryType() == 'LineString') { |
232 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
232 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
233 | 233 | } |
234 | 234 | if ($geometry->geometryType() == 'MultiLineString') { |
235 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
235 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
236 | 236 | } |
237 | 237 | break; |
238 | 238 | case 'numPoints': |
239 | 239 | if ($geometry->geometryType() == 'Point') { |
240 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
240 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
241 | 241 | } |
242 | 242 | if ($geometry->geometryType() == 'LineString') { |
243 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
243 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
244 | 244 | } |
245 | 245 | if ($geometry->geometryType() == 'MultiLineString') { |
246 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
246 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
247 | 247 | } |
248 | 248 | break; |
249 | 249 | case 'dimension': |
250 | 250 | if ($geometry->geometryType() == 'Point') { |
251 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
251 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
252 | 252 | } |
253 | 253 | if ($geometry->geometryType() == 'LineString') { |
254 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
254 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
255 | 255 | } |
256 | 256 | if ($geometry->geometryType() == 'MultiLineString') { |
257 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
257 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
258 | 258 | } |
259 | 259 | break; |
260 | 260 | case 'boundary': |
261 | 261 | if ($geometry->geometryType() == 'Point') { |
262 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
262 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
263 | 263 | } |
264 | 264 | if ($geometry->geometryType() == 'LineString') { |
265 | - $this->assertNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
265 | + $this->assertNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
266 | 266 | } |
267 | 267 | if ($geometry->geometryType() == 'MultiLineString') { |
268 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
268 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
269 | 269 | } |
270 | 270 | break; |
271 | 271 | case 'haversineLength': |
@@ -274,16 +274,16 @@ discard block |
||
274 | 274 | break; |
275 | 275 | case 'greatCircleLength': |
276 | 276 | case 'area': |
277 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
277 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
278 | 278 | break; |
279 | 279 | case 'geometryType': |
280 | - $this->assertNotNull($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
280 | + $this->assertNotNull($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
281 | 281 | break; |
282 | 282 | case 'setSRID': |
283 | 283 | //TODO: The method setSRID() should return TRUE. |
284 | 284 | break; |
285 | 285 | default: |
286 | - $this->assertTrue($geometry->$method_name($argument), 'Failed on ' . $method_name); |
|
286 | + $this->assertTrue($geometry->$method_name($argument), 'Failed on '.$method_name); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | } |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | |
295 | 295 | $methods = array( |
296 | 296 | //'boundary', //@@TODO: Uncomment this and fix errors |
297 | - 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
297 | + 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
298 | 298 | 'getBBox', |
299 | 299 | 'x', |
300 | 300 | 'y', |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | |
330 | 330 | // Now check base on type |
331 | 331 | if ($geos_type == 'object') { |
332 | - $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
332 | + $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'), 'wkt')); |
|
333 | 333 | |
334 | 334 | // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
335 | 335 | // Using Pythagorean theorem |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
338 | 338 | |
339 | 339 | // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
340 | - if ($haus_dist / $scale > 0.5) { |
|
340 | + if ($haus_dist/$scale > 0.5) { |
|
341 | 341 | $this->fail('Output mismatch on '.$method); |
342 | 342 | $this->dump('GEOS : '); |
343 | 343 | $this->dump($geos_result->out('wkt')); |
@@ -290,7 +290,9 @@ |
||
290 | 290 | |
291 | 291 | function _methods_tester_with_geos($geometry) { |
292 | 292 | // Cannot test methods if GEOS is not intstalled |
293 | - if (!geoPHP::geosInstalled()) return; |
|
293 | + if (!geoPHP::geosInstalled()) { |
|
294 | + return; |
|
295 | + } |
|
294 | 296 | |
295 | 297 | $methods = array( |
296 | 298 | //'boundary', //@@TODO: Uncomment this and fix errors |
@@ -68,6 +68,9 @@ discard block |
||
68 | 68 | // ----------------------------------------------------------------------------- |
69 | 69 | // get html dom from file |
70 | 70 | // $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1. |
71 | +/** |
|
72 | + * @param string $url |
|
73 | + */ |
|
71 | 74 | function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
72 | 75 | { |
73 | 76 | // We DO force the tags to be terminated. |
@@ -125,6 +128,9 @@ discard block |
||
125 | 128 | public $tag_start = 0; |
126 | 129 | private $dom = null; |
127 | 130 | |
131 | + /** |
|
132 | + * @param simple_html_dom $dom |
|
133 | + */ |
|
128 | 134 | function __construct($dom) |
129 | 135 | { |
130 | 136 | $this->dom = $dom; |
@@ -1501,6 +1507,11 @@ discard block |
||
1501 | 1507 | } |
1502 | 1508 | |
1503 | 1509 | // parse attributes |
1510 | + |
|
1511 | + /** |
|
1512 | + * @param simple_html_dom_node $node |
|
1513 | + * @param string[] $space |
|
1514 | + */ |
|
1504 | 1515 | protected function parse_attr($node, $name, &$space) |
1505 | 1516 | { |
1506 | 1517 | // Per sourceforge: http://sourceforge.net/tracker/?func=detail&aid=3061408&group_id=218559&atid=1044037 |
@@ -1538,6 +1549,11 @@ discard block |
||
1538 | 1549 | } |
1539 | 1550 | |
1540 | 1551 | // link node's parent |
1552 | + |
|
1553 | + /** |
|
1554 | + * @param simple_html_dom_node $node |
|
1555 | + * @param boolean $is_child |
|
1556 | + */ |
|
1541 | 1557 | protected function link_nodes(&$node, $is_child) |
1542 | 1558 | { |
1543 | 1559 | $node->parent = $this->parent; |
@@ -1549,6 +1565,10 @@ discard block |
||
1549 | 1565 | } |
1550 | 1566 | |
1551 | 1567 | // as a text node |
1568 | + |
|
1569 | + /** |
|
1570 | + * @param string $tag |
|
1571 | + */ |
|
1552 | 1572 | protected function as_text_node($tag) |
1553 | 1573 | { |
1554 | 1574 | $node = new simple_html_dom_node($this); |
@@ -1559,12 +1579,18 @@ discard block |
||
1559 | 1579 | return true; |
1560 | 1580 | } |
1561 | 1581 | |
1582 | + /** |
|
1583 | + * @param string $chars |
|
1584 | + */ |
|
1562 | 1585 | protected function skip($chars) |
1563 | 1586 | { |
1564 | 1587 | $this->pos += strspn($this->doc, $chars, $this->pos); |
1565 | 1588 | $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1566 | 1589 | } |
1567 | 1590 | |
1591 | + /** |
|
1592 | + * @param string $chars |
|
1593 | + */ |
|
1568 | 1594 | protected function copy_skip($chars) |
1569 | 1595 | { |
1570 | 1596 | $pos = $this->pos; |
@@ -1575,6 +1601,9 @@ discard block |
||
1575 | 1601 | return substr($this->doc, $pos, $len); |
1576 | 1602 | } |
1577 | 1603 | |
1604 | + /** |
|
1605 | + * @param string $chars |
|
1606 | + */ |
|
1578 | 1607 | protected function copy_until($chars) |
1579 | 1608 | { |
1580 | 1609 | $pos = $this->pos; |
@@ -1584,6 +1613,9 @@ discard block |
||
1584 | 1613 | return substr($this->doc, $pos, $len); |
1585 | 1614 | } |
1586 | 1615 | |
1616 | + /** |
|
1617 | + * @param string $char |
|
1618 | + */ |
|
1587 | 1619 | protected function copy_until_char($char) |
1588 | 1620 | { |
1589 | 1621 | if ($this->char===null) return ''; |
@@ -1602,6 +1634,9 @@ discard block |
||
1602 | 1634 | return substr($this->doc, $pos_old, $pos-$pos_old); |
1603 | 1635 | } |
1604 | 1636 | |
1637 | + /** |
|
1638 | + * @param string $char |
|
1639 | + */ |
|
1605 | 1640 | protected function copy_until_char_escape($char) |
1606 | 1641 | { |
1607 | 1642 | if ($this->char===null) return ''; |
@@ -1633,6 +1668,10 @@ discard block |
||
1633 | 1668 | |
1634 | 1669 | // remove noise from html content |
1635 | 1670 | // save the noise in the $this->noise array. |
1671 | + |
|
1672 | + /** |
|
1673 | + * @param string $pattern |
|
1674 | + */ |
|
1636 | 1675 | protected function remove_noise($pattern, $remove_tag=false) |
1637 | 1676 | { |
1638 | 1677 | global $debug_object; |
@@ -1658,6 +1697,10 @@ discard block |
||
1658 | 1697 | } |
1659 | 1698 | |
1660 | 1699 | // restore noise to html content |
1700 | + |
|
1701 | + /** |
|
1702 | + * @param string|null $text |
|
1703 | + */ |
|
1661 | 1704 | function restore_noise($text) |
1662 | 1705 | { |
1663 | 1706 | global $debug_object; |
@@ -824,11 +824,11 @@ |
||
824 | 824 | } |
825 | 825 | |
826 | 826 | /** |
827 | - * Returns true if $string is valid UTF-8 and false otherwise. |
|
828 | - * |
|
829 | - * @param mixed $str String to be tested |
|
830 | - * @return boolean |
|
831 | - */ |
|
827 | + * Returns true if $string is valid UTF-8 and false otherwise. |
|
828 | + * |
|
829 | + * @param mixed $str String to be tested |
|
830 | + * @return boolean |
|
831 | + */ |
|
832 | 832 | static function is_utf8($str) |
833 | 833 | { |
834 | 834 | $c=0; $b=0; |
@@ -45,21 +45,21 @@ discard block |
||
45 | 45 | */ |
46 | 46 | define('HDOM_TYPE_ELEMENT', 1); |
47 | 47 | define('HDOM_TYPE_COMMENT', 2); |
48 | -define('HDOM_TYPE_TEXT', 3); |
|
49 | -define('HDOM_TYPE_ENDTAG', 4); |
|
50 | -define('HDOM_TYPE_ROOT', 5); |
|
48 | +define('HDOM_TYPE_TEXT', 3); |
|
49 | +define('HDOM_TYPE_ENDTAG', 4); |
|
50 | +define('HDOM_TYPE_ROOT', 5); |
|
51 | 51 | define('HDOM_TYPE_UNKNOWN', 6); |
52 | 52 | define('HDOM_QUOTE_DOUBLE', 0); |
53 | 53 | define('HDOM_QUOTE_SINGLE', 1); |
54 | -define('HDOM_QUOTE_NO', 3); |
|
55 | -define('HDOM_INFO_BEGIN', 0); |
|
56 | -define('HDOM_INFO_END', 1); |
|
57 | -define('HDOM_INFO_QUOTE', 2); |
|
58 | -define('HDOM_INFO_SPACE', 3); |
|
59 | -define('HDOM_INFO_TEXT', 4); |
|
60 | -define('HDOM_INFO_INNER', 5); |
|
61 | -define('HDOM_INFO_OUTER', 6); |
|
62 | -define('HDOM_INFO_ENDSPACE',7); |
|
54 | +define('HDOM_QUOTE_NO', 3); |
|
55 | +define('HDOM_INFO_BEGIN', 0); |
|
56 | +define('HDOM_INFO_END', 1); |
|
57 | +define('HDOM_INFO_QUOTE', 2); |
|
58 | +define('HDOM_INFO_SPACE', 3); |
|
59 | +define('HDOM_INFO_TEXT', 4); |
|
60 | +define('HDOM_INFO_INNER', 5); |
|
61 | +define('HDOM_INFO_OUTER', 6); |
|
62 | +define('HDOM_INFO_ENDSPACE', 7); |
|
63 | 63 | define('DEFAULT_TARGET_CHARSET', 'UTF-8'); |
64 | 64 | define('DEFAULT_BR_TEXT', "\r\n"); |
65 | 65 | define('DEFAULT_SPAN_TEXT', " "); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | // ----------------------------------------------------------------------------- |
69 | 69 | // get html dom from file |
70 | 70 | // $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1. |
71 | -function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
|
71 | +function file_get_html($url, $use_include_path = false, $context = null, $offset = -1, $maxLen = -1, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) |
|
72 | 72 | { |
73 | 73 | // We DO force the tags to be terminated. |
74 | 74 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | } |
87 | 87 | |
88 | 88 | // get html dom from string |
89 | -function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
|
89 | +function str_get_html($str, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) |
|
90 | 90 | { |
91 | 91 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); |
92 | 92 | if (empty($str) || strlen($str) > MAX_FILE_SIZE) |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | } |
100 | 100 | |
101 | 101 | // dump html dom tree |
102 | -function dump_html_tree($node, $show_attr=true, $deep=0) |
|
102 | +function dump_html_tree($node, $show_attr = true, $deep = 0) |
|
103 | 103 | { |
104 | 104 | $node->dump($node); |
105 | 105 | } |
@@ -151,12 +151,12 @@ discard block |
||
151 | 151 | } |
152 | 152 | |
153 | 153 | // dump node's tree |
154 | - function dump($show_attr=true, $deep=0) |
|
154 | + function dump($show_attr = true, $deep = 0) |
|
155 | 155 | { |
156 | 156 | $lead = str_repeat(' ', $deep); |
157 | 157 | |
158 | 158 | echo $lead.$this->tag; |
159 | - if ($show_attr && count($this->attr)>0) |
|
159 | + if ($show_attr && count($this->attr) > 0) |
|
160 | 160 | { |
161 | 161 | echo '('; |
162 | 162 | foreach ($this->attr as $k=>$v) |
@@ -169,18 +169,18 @@ discard block |
||
169 | 169 | { |
170 | 170 | foreach ($this->nodes as $c) |
171 | 171 | { |
172 | - $c->dump($show_attr, $deep+1); |
|
172 | + $c->dump($show_attr, $deep + 1); |
|
173 | 173 | } |
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | 177 | |
178 | 178 | // Debugging function to dump a single dom node with a bunch of information about it. |
179 | - function dump_node($echo=true) |
|
179 | + function dump_node($echo = true) |
|
180 | 180 | { |
181 | 181 | |
182 | 182 | $string = $this->tag; |
183 | - if (count($this->attr)>0) |
|
183 | + if (count($this->attr) > 0) |
|
184 | 184 | { |
185 | 185 | $string .= '('; |
186 | 186 | foreach ($this->attr as $k=>$v) |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | } |
190 | 190 | $string .= ')'; |
191 | 191 | } |
192 | - if (count($this->_)>0) |
|
192 | + if (count($this->_) > 0) |
|
193 | 193 | { |
194 | 194 | $string .= ' $_ ('; |
195 | 195 | foreach ($this->_ as $k=>$v) |
@@ -211,22 +211,22 @@ discard block |
||
211 | 211 | |
212 | 212 | if (isset($this->text)) |
213 | 213 | { |
214 | - $string .= " text: (" . $this->text . ")"; |
|
214 | + $string .= " text: (".$this->text.")"; |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | $string .= " HDOM_INNER_INFO: '"; |
218 | 218 | if (isset($node->_[HDOM_INFO_INNER])) |
219 | 219 | { |
220 | - $string .= $node->_[HDOM_INFO_INNER] . "'"; |
|
220 | + $string .= $node->_[HDOM_INFO_INNER]."'"; |
|
221 | 221 | } |
222 | 222 | else |
223 | 223 | { |
224 | 224 | $string .= ' NULL '; |
225 | 225 | } |
226 | 226 | |
227 | - $string .= " children: " . count($this->children); |
|
228 | - $string .= " nodes: " . count($this->nodes); |
|
229 | - $string .= " tag_start: " . $this->tag_start; |
|
227 | + $string .= " children: ".count($this->children); |
|
228 | + $string .= " nodes: ".count($this->nodes); |
|
229 | + $string .= " tag_start: ".$this->tag_start; |
|
230 | 230 | $string .= "\n"; |
231 | 231 | |
232 | 232 | if ($echo) |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | |
243 | 243 | // returns the parent of node |
244 | 244 | // If a node is passed in, it will reset the parent of the current node to that one. |
245 | - function parent($parent=null) |
|
245 | + function parent($parent = null) |
|
246 | 246 | { |
247 | 247 | // I am SURE that this doesn't work properly. |
248 | 248 | // It fails to unset the current node from it's current parents nodes or children list first. |
@@ -263,9 +263,9 @@ discard block |
||
263 | 263 | } |
264 | 264 | |
265 | 265 | // returns children of node |
266 | - function children($idx=-1) |
|
266 | + function children($idx = -1) |
|
267 | 267 | { |
268 | - if ($idx===-1) |
|
268 | + if ($idx === -1) |
|
269 | 269 | { |
270 | 270 | return $this->children; |
271 | 271 | } |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | // returns the first child of node |
280 | 280 | function first_child() |
281 | 281 | { |
282 | - if (count($this->children)>0) |
|
282 | + if (count($this->children) > 0) |
|
283 | 283 | { |
284 | 284 | return $this->children[0]; |
285 | 285 | } |
@@ -289,9 +289,9 @@ discard block |
||
289 | 289 | // returns the last child of node |
290 | 290 | function last_child() |
291 | 291 | { |
292 | - if (($count=count($this->children))>0) |
|
292 | + if (($count = count($this->children)) > 0) |
|
293 | 293 | { |
294 | - return $this->children[$count-1]; |
|
294 | + return $this->children[$count - 1]; |
|
295 | 295 | } |
296 | 296 | return null; |
297 | 297 | } |
@@ -299,18 +299,18 @@ discard block |
||
299 | 299 | // returns the next sibling of node |
300 | 300 | function next_sibling() |
301 | 301 | { |
302 | - if ($this->parent===null) |
|
302 | + if ($this->parent === null) |
|
303 | 303 | { |
304 | 304 | return null; |
305 | 305 | } |
306 | 306 | |
307 | 307 | $idx = 0; |
308 | 308 | $count = count($this->parent->children); |
309 | - while ($idx<$count && $this!==$this->parent->children[$idx]) |
|
309 | + while ($idx < $count && $this !== $this->parent->children[$idx]) |
|
310 | 310 | { |
311 | 311 | ++$idx; |
312 | 312 | } |
313 | - if (++$idx>=$count) |
|
313 | + if (++$idx >= $count) |
|
314 | 314 | { |
315 | 315 | return null; |
316 | 316 | } |
@@ -320,12 +320,12 @@ discard block |
||
320 | 320 | // returns the previous sibling of node |
321 | 321 | function prev_sibling() |
322 | 322 | { |
323 | - if ($this->parent===null) return null; |
|
323 | + if ($this->parent === null) return null; |
|
324 | 324 | $idx = 0; |
325 | 325 | $count = count($this->parent->children); |
326 | - while ($idx<$count && $this!==$this->parent->children[$idx]) |
|
326 | + while ($idx < $count && $this !== $this->parent->children[$idx]) |
|
327 | 327 | ++$idx; |
328 | - if (--$idx<0) return null; |
|
328 | + if (--$idx < 0) return null; |
|
329 | 329 | return $this->parent->children[$idx]; |
330 | 330 | } |
331 | 331 | |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | |
341 | 341 | while (!is_null($returnDom)) |
342 | 342 | { |
343 | - if (is_object($debug_object)) { $debug_object->debug_log(2, "Current tag is: " . $returnDom->tag); } |
|
343 | + if (is_object($debug_object)) { $debug_object->debug_log(2, "Current tag is: ".$returnDom->tag); } |
|
344 | 344 | |
345 | 345 | if ($returnDom->tag == $tag) |
346 | 346 | { |
@@ -374,16 +374,16 @@ discard block |
||
374 | 374 | { |
375 | 375 | if (!empty($this->text)) |
376 | 376 | { |
377 | - $text = " with text: " . $this->text; |
|
377 | + $text = " with text: ".$this->text; |
|
378 | 378 | } |
379 | 379 | } |
380 | - $debug_object->debug_log(1, 'Innertext of tag: ' . $this->tag . $text); |
|
380 | + $debug_object->debug_log(1, 'Innertext of tag: '.$this->tag.$text); |
|
381 | 381 | } |
382 | 382 | |
383 | - if ($this->tag==='root') return $this->innertext(); |
|
383 | + if ($this->tag === 'root') return $this->innertext(); |
|
384 | 384 | |
385 | 385 | // trigger callback |
386 | - if ($this->dom && $this->dom->callback!==null) |
|
386 | + if ($this->dom && $this->dom->callback !== null) |
|
387 | 387 | { |
388 | 388 | call_user_func_array($this->dom->callback, array($this)); |
389 | 389 | } |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | } |
419 | 419 | |
420 | 420 | // render end tag |
421 | - if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) |
|
421 | + if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END] != 0) |
|
422 | 422 | $ret .= '</'.$this->tag.'>'; |
423 | 423 | return $ret; |
424 | 424 | } |
@@ -433,8 +433,8 @@ discard block |
||
433 | 433 | case HDOM_TYPE_COMMENT: return ''; |
434 | 434 | case HDOM_TYPE_UNKNOWN: return ''; |
435 | 435 | } |
436 | - if (strcasecmp($this->tag, 'script')===0) return ''; |
|
437 | - if (strcasecmp($this->tag, 'style')===0) return ''; |
|
436 | + if (strcasecmp($this->tag, 'script') === 0) return ''; |
|
437 | + if (strcasecmp($this->tag, 'style') === 0) return ''; |
|
438 | 438 | |
439 | 439 | $ret = ''; |
440 | 440 | // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL. |
@@ -480,12 +480,12 @@ discard block |
||
480 | 480 | ++$i; |
481 | 481 | |
482 | 482 | // skip removed attribute |
483 | - if ($val===null || $val===false) |
|
483 | + if ($val === null || $val === false) |
|
484 | 484 | continue; |
485 | 485 | |
486 | 486 | $ret .= $this->_[HDOM_INFO_SPACE][$i][0]; |
487 | 487 | //no value attr: nowrap, checked selected... |
488 | - if ($val===true) |
|
488 | + if ($val === true) |
|
489 | 489 | $ret .= $key; |
490 | 490 | else { |
491 | 491 | switch ($this->_[HDOM_INFO_QUOTE][$i]) |
@@ -498,34 +498,34 @@ discard block |
||
498 | 498 | } |
499 | 499 | } |
500 | 500 | $ret = $this->dom->restore_noise($ret); |
501 | - return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>'; |
|
501 | + return $ret.$this->_[HDOM_INFO_ENDSPACE].'>'; |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | // find elements by css selector |
505 | 505 | //PaperG - added ability for find to lowercase the value of the selector. |
506 | - function find($selector, $idx=null, $lowercase=false) |
|
506 | + function find($selector, $idx = null, $lowercase = false) |
|
507 | 507 | { |
508 | 508 | $selectors = $this->parse_selector($selector); |
509 | - if (($count=count($selectors))===0) return array(); |
|
509 | + if (($count = count($selectors)) === 0) return array(); |
|
510 | 510 | $found_keys = array(); |
511 | 511 | |
512 | 512 | // find each selector |
513 | - for ($c=0; $c<$count; ++$c) |
|
513 | + for ($c = 0; $c < $count; ++$c) |
|
514 | 514 | { |
515 | 515 | // The change on the below line was documented on the sourceforge code tracker id 2788009 |
516 | 516 | // used to be: if (($levle=count($selectors[0]))===0) return array(); |
517 | - if (($levle=count($selectors[$c]))===0) return array(); |
|
517 | + if (($levle = count($selectors[$c])) === 0) return array(); |
|
518 | 518 | if (!isset($this->_[HDOM_INFO_BEGIN])) return array(); |
519 | 519 | |
520 | 520 | $head = array($this->_[HDOM_INFO_BEGIN]=>1); |
521 | 521 | |
522 | 522 | // handle descendant selectors, no recursive! |
523 | - for ($l=0; $l<$levle; ++$l) |
|
523 | + for ($l = 0; $l < $levle; ++$l) |
|
524 | 524 | { |
525 | 525 | $ret = array(); |
526 | 526 | foreach ($head as $k=>$v) |
527 | 527 | { |
528 | - $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k]; |
|
528 | + $n = ($k === -1) ? $this->dom->root : $this->dom->nodes[$k]; |
|
529 | 529 | //PaperG - Pass this optional parameter on to the seek function. |
530 | 530 | $n->seek($selectors[$c][$l], $ret, $lowercase); |
531 | 531 | } |
@@ -550,13 +550,13 @@ discard block |
||
550 | 550 | |
551 | 551 | // return nth-element or array |
552 | 552 | if (is_null($idx)) return $found; |
553 | - else if ($idx<0) $idx = count($found) + $idx; |
|
553 | + else if ($idx < 0) $idx = count($found) + $idx; |
|
554 | 554 | return (isset($found[$idx])) ? $found[$idx] : null; |
555 | 555 | } |
556 | 556 | |
557 | 557 | // seek for given conditions |
558 | 558 | // PaperG - added parameter to allow for case insensitive testing of the value of a selector. |
559 | - protected function seek($selector, &$ret, $lowercase=false) |
|
559 | + protected function seek($selector, &$ret, $lowercase = false) |
|
560 | 560 | { |
561 | 561 | global $debug_object; |
562 | 562 | if (is_object($debug_object)) { $debug_object->debug_log_entry(1); } |
@@ -569,8 +569,8 @@ discard block |
||
569 | 569 | $count = 0; |
570 | 570 | foreach ($this->children as $c) |
571 | 571 | { |
572 | - if ($tag==='*' || $tag===$c->tag) { |
|
573 | - if (++$count==$key) { |
|
572 | + if ($tag === '*' || $tag === $c->tag) { |
|
573 | + if (++$count == $key) { |
|
574 | 574 | $ret[$c->_[HDOM_INFO_BEGIN]] = 1; |
575 | 575 | return; |
576 | 576 | } |
@@ -580,38 +580,38 @@ discard block |
||
580 | 580 | } |
581 | 581 | |
582 | 582 | $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0; |
583 | - if ($end==0) { |
|
583 | + if ($end == 0) { |
|
584 | 584 | $parent = $this->parent; |
585 | - while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) { |
|
585 | + while (!isset($parent->_[HDOM_INFO_END]) && $parent !== null) { |
|
586 | 586 | $end -= 1; |
587 | 587 | $parent = $parent->parent; |
588 | 588 | } |
589 | 589 | $end += $parent->_[HDOM_INFO_END]; |
590 | 590 | } |
591 | 591 | |
592 | - for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) { |
|
592 | + for ($i = $this->_[HDOM_INFO_BEGIN] + 1; $i < $end; ++$i) { |
|
593 | 593 | $node = $this->dom->nodes[$i]; |
594 | 594 | |
595 | 595 | $pass = true; |
596 | 596 | |
597 | - if ($tag==='*' && !$key) { |
|
597 | + if ($tag === '*' && !$key) { |
|
598 | 598 | if (in_array($node, $this->children, true)) |
599 | 599 | $ret[$i] = 1; |
600 | 600 | continue; |
601 | 601 | } |
602 | 602 | |
603 | 603 | // compare tag |
604 | - if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;} |
|
604 | + if ($tag && $tag != $node->tag && $tag !== '*') {$pass = false; } |
|
605 | 605 | // compare key |
606 | 606 | if ($pass && $key) { |
607 | 607 | if ($no_key) { |
608 | - if (isset($node->attr[$key])) $pass=false; |
|
608 | + if (isset($node->attr[$key])) $pass = false; |
|
609 | 609 | } else { |
610 | - if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false; |
|
610 | + if (($key != "plaintext") && !isset($node->attr[$key])) $pass = false; |
|
611 | 611 | } |
612 | 612 | } |
613 | 613 | // compare value |
614 | - if ($pass && $key && $val && $val!=='*') { |
|
614 | + if ($pass && $key && $val && $val !== '*') { |
|
615 | 615 | // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right? |
616 | 616 | if ($key == "plaintext") { |
617 | 617 | // $node->plaintext actually returns $node->text(); |
@@ -620,7 +620,7 @@ discard block |
||
620 | 620 | // this is a normal search, we want the value of that attribute of the tag. |
621 | 621 | $nodeKeyValue = $node->attr[$key]; |
622 | 622 | } |
623 | - if (is_object($debug_object)) {$debug_object->debug_log(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);} |
|
623 | + if (is_object($debug_object)) {$debug_object->debug_log(2, "testing node: ".$node->tag." for attribute: ".$key.$exp.$val." where nodes value is: ".$nodeKeyValue); } |
|
624 | 624 | |
625 | 625 | //PaperG - If lowercase is set, do a case insensitive test of the value of the selector. |
626 | 626 | if ($lowercase) { |
@@ -628,11 +628,11 @@ discard block |
||
628 | 628 | } else { |
629 | 629 | $check = $this->match($exp, $val, $nodeKeyValue); |
630 | 630 | } |
631 | - if (is_object($debug_object)) {$debug_object->debug_log(2, "after match: " . ($check ? "true" : "false"));} |
|
631 | + if (is_object($debug_object)) {$debug_object->debug_log(2, "after match: ".($check ? "true" : "false")); } |
|
632 | 632 | |
633 | 633 | // handle multiple class |
634 | - if (!$check && strcasecmp($key, 'class')===0) { |
|
635 | - foreach (explode(' ',$node->attr[$key]) as $k) { |
|
634 | + if (!$check && strcasecmp($key, 'class') === 0) { |
|
635 | + foreach (explode(' ', $node->attr[$key]) as $k) { |
|
636 | 636 | // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form. |
637 | 637 | if (!empty($k)) { |
638 | 638 | if ($lowercase) { |
@@ -650,24 +650,24 @@ discard block |
||
650 | 650 | unset($node); |
651 | 651 | } |
652 | 652 | // It's passed by reference so this is actually what this function returns. |
653 | - if (is_object($debug_object)) {$debug_object->debug_log(1, "EXIT - ret: ", $ret);} |
|
653 | + if (is_object($debug_object)) {$debug_object->debug_log(1, "EXIT - ret: ", $ret); } |
|
654 | 654 | } |
655 | 655 | |
656 | 656 | protected function match($exp, $pattern, $value) { |
657 | 657 | global $debug_object; |
658 | - if (is_object($debug_object)) {$debug_object->debug_log_entry(1);} |
|
658 | + if (is_object($debug_object)) {$debug_object->debug_log_entry(1); } |
|
659 | 659 | |
660 | 660 | switch ($exp) { |
661 | 661 | case '=': |
662 | - return ($value===$pattern); |
|
662 | + return ($value === $pattern); |
|
663 | 663 | case '!=': |
664 | - return ($value!==$pattern); |
|
664 | + return ($value !== $pattern); |
|
665 | 665 | case '^=': |
666 | - return preg_match("/^".preg_quote($pattern,'/')."/", $value); |
|
666 | + return preg_match("/^".preg_quote($pattern, '/')."/", $value); |
|
667 | 667 | case '$=': |
668 | - return preg_match("/".preg_quote($pattern,'/')."$/", $value); |
|
668 | + return preg_match("/".preg_quote($pattern, '/')."$/", $value); |
|
669 | 669 | case '*=': |
670 | - if ($pattern[0]=='/') { |
|
670 | + if ($pattern[0] == '/') { |
|
671 | 671 | return preg_match($pattern, $value); |
672 | 672 | } |
673 | 673 | return preg_match("/".$pattern."/i", $value); |
@@ -677,7 +677,7 @@ discard block |
||
677 | 677 | |
678 | 678 | protected function parse_selector($selector_string) { |
679 | 679 | global $debug_object; |
680 | - if (is_object($debug_object)) {$debug_object->debug_log_entry(1);} |
|
680 | + if (is_object($debug_object)) {$debug_object->debug_log_entry(1); } |
|
681 | 681 | |
682 | 682 | // pattern of CSS selectors, modified from mootools |
683 | 683 | // Paperg: Add the colon to the attrbute, so that it properly finds <tag attr:ibute="something" > like google does. |
@@ -688,7 +688,7 @@ discard block |
||
688 | 688 | // $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; |
689 | 689 | $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; |
690 | 690 | preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER); |
691 | - if (is_object($debug_object)) {$debug_object->debug_log(2, "Matches Array: ", $matches);} |
|
691 | + if (is_object($debug_object)) {$debug_object->debug_log(2, "Matches Array: ", $matches); } |
|
692 | 692 | |
693 | 693 | $selectors = array(); |
694 | 694 | $result = array(); |
@@ -696,29 +696,29 @@ discard block |
||
696 | 696 | |
697 | 697 | foreach ($matches as $m) { |
698 | 698 | $m[0] = trim($m[0]); |
699 | - if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue; |
|
699 | + if ($m[0] === '' || $m[0] === '/' || $m[0] === '//') continue; |
|
700 | 700 | // for browser generated xpath |
701 | - if ($m[1]==='tbody') continue; |
|
701 | + if ($m[1] === 'tbody') continue; |
|
702 | 702 | |
703 | 703 | list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false); |
704 | - if (!empty($m[2])) {$key='id'; $val=$m[2];} |
|
705 | - if (!empty($m[3])) {$key='class'; $val=$m[3];} |
|
706 | - if (!empty($m[4])) {$key=$m[4];} |
|
707 | - if (!empty($m[5])) {$exp=$m[5];} |
|
708 | - if (!empty($m[6])) {$val=$m[6];} |
|
704 | + if (!empty($m[2])) {$key = 'id'; $val = $m[2]; } |
|
705 | + if (!empty($m[3])) {$key = 'class'; $val = $m[3]; } |
|
706 | + if (!empty($m[4])) {$key = $m[4]; } |
|
707 | + if (!empty($m[5])) {$exp = $m[5]; } |
|
708 | + if (!empty($m[6])) {$val = $m[6]; } |
|
709 | 709 | |
710 | 710 | // convert to lowercase |
711 | - if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);} |
|
711 | + if ($this->dom->lowercase) {$tag = strtolower($tag); $key = strtolower($key); } |
|
712 | 712 | //elements that do NOT have the specified attribute |
713 | - if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;} |
|
713 | + if (isset($key[0]) && $key[0] === '!') {$key = substr($key, 1); $no_key = true; } |
|
714 | 714 | |
715 | 715 | $result[] = array($tag, $key, $val, $exp, $no_key); |
716 | - if (trim($m[7])===',') { |
|
716 | + if (trim($m[7]) === ',') { |
|
717 | 717 | $selectors[] = $result; |
718 | 718 | $result = array(); |
719 | 719 | } |
720 | 720 | } |
721 | - if (count($result)>0) |
|
721 | + if (count($result) > 0) |
|
722 | 722 | $selectors[] = $result; |
723 | 723 | return $selectors; |
724 | 724 | } |
@@ -742,7 +742,7 @@ discard block |
||
742 | 742 | function __set($name, $value) |
743 | 743 | { |
744 | 744 | global $debug_object; |
745 | - if (is_object($debug_object)) {$debug_object->debug_log_entry(1);} |
|
745 | + if (is_object($debug_object)) {$debug_object->debug_log_entry(1); } |
|
746 | 746 | |
747 | 747 | switch ($name) |
748 | 748 | { |
@@ -780,7 +780,7 @@ discard block |
||
780 | 780 | function convert_text($text) |
781 | 781 | { |
782 | 782 | global $debug_object; |
783 | - if (is_object($debug_object)) {$debug_object->debug_log_entry(1);} |
|
783 | + if (is_object($debug_object)) {$debug_object->debug_log_entry(1); } |
|
784 | 784 | |
785 | 785 | $converted_text = $text; |
786 | 786 | |
@@ -792,7 +792,7 @@ discard block |
||
792 | 792 | $sourceCharset = strtoupper($this->dom->_charset); |
793 | 793 | $targetCharset = strtoupper($this->dom->_target_charset); |
794 | 794 | } |
795 | - if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);} |
|
795 | + if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: ".$sourceCharset." target charaset: ".$targetCharset); } |
|
796 | 796 | |
797 | 797 | if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0)) |
798 | 798 | { |
@@ -831,27 +831,27 @@ discard block |
||
831 | 831 | */ |
832 | 832 | static function is_utf8($str) |
833 | 833 | { |
834 | - $c=0; $b=0; |
|
835 | - $bits=0; |
|
836 | - $len=strlen($str); |
|
837 | - for($i=0; $i<$len; $i++) |
|
834 | + $c = 0; $b = 0; |
|
835 | + $bits = 0; |
|
836 | + $len = strlen($str); |
|
837 | + for ($i = 0; $i < $len; $i++) |
|
838 | 838 | { |
839 | - $c=ord($str[$i]); |
|
840 | - if($c > 128) |
|
839 | + $c = ord($str[$i]); |
|
840 | + if ($c > 128) |
|
841 | 841 | { |
842 | - if(($c >= 254)) return false; |
|
843 | - elseif($c >= 252) $bits=6; |
|
844 | - elseif($c >= 248) $bits=5; |
|
845 | - elseif($c >= 240) $bits=4; |
|
846 | - elseif($c >= 224) $bits=3; |
|
847 | - elseif($c >= 192) $bits=2; |
|
842 | + if (($c >= 254)) return false; |
|
843 | + elseif ($c >= 252) $bits = 6; |
|
844 | + elseif ($c >= 248) $bits = 5; |
|
845 | + elseif ($c >= 240) $bits = 4; |
|
846 | + elseif ($c >= 224) $bits = 3; |
|
847 | + elseif ($c >= 192) $bits = 2; |
|
848 | 848 | else return false; |
849 | - if(($i+$bits) > $len) return false; |
|
850 | - while($bits > 1) |
|
849 | + if (($i + $bits) > $len) return false; |
|
850 | + while ($bits > 1) |
|
851 | 851 | { |
852 | 852 | $i++; |
853 | - $b=ord($str[$i]); |
|
854 | - if($b < 128 || $b > 191) return false; |
|
853 | + $b = ord($str[$i]); |
|
854 | + if ($b < 128 || $b > 191) return false; |
|
855 | 855 | $bits--; |
856 | 856 | } |
857 | 857 | } |
@@ -955,24 +955,24 @@ discard block |
||
955 | 955 | } |
956 | 956 | |
957 | 957 | // camel naming conventions |
958 | - function getAllAttributes() {return $this->attr;} |
|
959 | - function getAttribute($name) {return $this->__get($name);} |
|
960 | - function setAttribute($name, $value) {$this->__set($name, $value);} |
|
961 | - function hasAttribute($name) {return $this->__isset($name);} |
|
962 | - function removeAttribute($name) {$this->__set($name, null);} |
|
963 | - function getElementById($id) {return $this->find("#$id", 0);} |
|
964 | - function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);} |
|
965 | - function getElementByTagName($name) {return $this->find($name, 0);} |
|
966 | - function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);} |
|
967 | - function parentNode() {return $this->parent();} |
|
968 | - function childNodes($idx=-1) {return $this->children($idx);} |
|
969 | - function firstChild() {return $this->first_child();} |
|
970 | - function lastChild() {return $this->last_child();} |
|
971 | - function nextSibling() {return $this->next_sibling();} |
|
972 | - function previousSibling() {return $this->prev_sibling();} |
|
973 | - function hasChildNodes() {return $this->has_child();} |
|
974 | - function nodeName() {return $this->tag;} |
|
975 | - function appendChild($node) {$node->parent($this); return $node;} |
|
958 | + function getAllAttributes() {return $this->attr; } |
|
959 | + function getAttribute($name) {return $this->__get($name); } |
|
960 | + function setAttribute($name, $value) {$this->__set($name, $value); } |
|
961 | + function hasAttribute($name) {return $this->__isset($name); } |
|
962 | + function removeAttribute($name) {$this->__set($name, null); } |
|
963 | + function getElementById($id) {return $this->find("#$id", 0); } |
|
964 | + function getElementsById($id, $idx = null) {return $this->find("#$id", $idx); } |
|
965 | + function getElementByTagName($name) {return $this->find($name, 0); } |
|
966 | + function getElementsByTagName($name, $idx = null) {return $this->find($name, $idx); } |
|
967 | + function parentNode() {return $this->parent(); } |
|
968 | + function childNodes($idx = -1) {return $this->children($idx); } |
|
969 | + function firstChild() {return $this->first_child(); } |
|
970 | + function lastChild() {return $this->last_child(); } |
|
971 | + function nextSibling() {return $this->next_sibling(); } |
|
972 | + function previousSibling() {return $this->prev_sibling(); } |
|
973 | + function hasChildNodes() {return $this->has_child(); } |
|
974 | + function nodeName() {return $this->tag; } |
|
975 | + function appendChild($node) {$node->parent($this); return $node; } |
|
976 | 976 | |
977 | 977 | } |
978 | 978 | |
@@ -1028,11 +1028,11 @@ discard block |
||
1028 | 1028 | 'option'=>array('option'=>1), |
1029 | 1029 | ); |
1030 | 1030 | |
1031 | - function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $target_charset=DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
|
1031 | + function __construct($str = null, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) |
|
1032 | 1032 | { |
1033 | 1033 | if ($str) |
1034 | 1034 | { |
1035 | - if (preg_match("/^http:\/\//i",$str) || is_file($str)) |
|
1035 | + if (preg_match("/^http:\/\//i", $str) || is_file($str)) |
|
1036 | 1036 | { |
1037 | 1037 | $this->load_file($str); |
1038 | 1038 | } |
@@ -1043,7 +1043,7 @@ discard block |
||
1043 | 1043 | } |
1044 | 1044 | // Forcing tags to be closed implies that we don't trust the html, but it can lead to parsing errors if we SHOULD trust the html. |
1045 | 1045 | if (!$forceTagsClosed) { |
1046 | - $this->optional_closing_array=array(); |
|
1046 | + $this->optional_closing_array = array(); |
|
1047 | 1047 | } |
1048 | 1048 | $this->_target_charset = $target_charset; |
1049 | 1049 | } |
@@ -1054,7 +1054,7 @@ discard block |
||
1054 | 1054 | } |
1055 | 1055 | |
1056 | 1056 | // load html from string |
1057 | - function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
|
1057 | + function load($str, $lowercase = true, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) |
|
1058 | 1058 | { |
1059 | 1059 | global $debug_object; |
1060 | 1060 | |
@@ -1096,7 +1096,7 @@ discard block |
||
1096 | 1096 | $args = func_get_args(); |
1097 | 1097 | $this->load(call_user_func_array('file_get_contents', $args), true); |
1098 | 1098 | // Throw an error if we can't properly load the dom. |
1099 | - if (($error=error_get_last())!==null) { |
|
1099 | + if (($error = error_get_last()) !== null) { |
|
1100 | 1100 | $this->clear(); |
1101 | 1101 | return false; |
1102 | 1102 | } |
@@ -1115,16 +1115,16 @@ discard block |
||
1115 | 1115 | } |
1116 | 1116 | |
1117 | 1117 | // save dom as string |
1118 | - function save($filepath='') |
|
1118 | + function save($filepath = '') |
|
1119 | 1119 | { |
1120 | 1120 | $ret = $this->root->innertext(); |
1121 | - if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX); |
|
1121 | + if ($filepath !== '') file_put_contents($filepath, $ret, LOCK_EX); |
|
1122 | 1122 | return $ret; |
1123 | 1123 | } |
1124 | 1124 | |
1125 | 1125 | // find dom node by css selector |
1126 | 1126 | // Paperg - allow us to specify that we want case insensitive testing of the value of the selector. |
1127 | - function find($selector, $idx=null, $lowercase=false) |
|
1127 | + function find($selector, $idx = null, $lowercase = false) |
|
1128 | 1128 | { |
1129 | 1129 | return $this->root->find($selector, $idx, $lowercase); |
1130 | 1130 | } |
@@ -1132,22 +1132,22 @@ discard block |
||
1132 | 1132 | // clean up memory due to php5 circular references memory leak... |
1133 | 1133 | function clear() |
1134 | 1134 | { |
1135 | - foreach ($this->nodes as $n) {$n->clear(); $n = null;} |
|
1135 | + foreach ($this->nodes as $n) {$n->clear(); $n = null; } |
|
1136 | 1136 | // This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear. |
1137 | - if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;} |
|
1138 | - if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);} |
|
1139 | - if (isset($this->root)) {$this->root->clear(); unset($this->root);} |
|
1137 | + if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null; } |
|
1138 | + if (isset($this->parent)) {$this->parent->clear(); unset($this->parent); } |
|
1139 | + if (isset($this->root)) {$this->root->clear(); unset($this->root); } |
|
1140 | 1140 | unset($this->doc); |
1141 | 1141 | unset($this->noise); |
1142 | 1142 | } |
1143 | 1143 | |
1144 | - function dump($show_attr=true) |
|
1144 | + function dump($show_attr = true) |
|
1145 | 1145 | { |
1146 | 1146 | $this->root->dump($show_attr); |
1147 | 1147 | } |
1148 | 1148 | |
1149 | 1149 | // prepare HTML data and init everything |
1150 | - protected function prepare($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) |
|
1150 | + protected function prepare($str, $lowercase = true, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) |
|
1151 | 1151 | { |
1152 | 1152 | $this->clear(); |
1153 | 1153 | |
@@ -1178,13 +1178,13 @@ discard block |
||
1178 | 1178 | $this->root->_[HDOM_INFO_BEGIN] = -1; |
1179 | 1179 | $this->root->nodetype = HDOM_TYPE_ROOT; |
1180 | 1180 | $this->parent = $this->root; |
1181 | - if ($this->size>0) $this->char = $this->doc[0]; |
|
1181 | + if ($this->size > 0) $this->char = $this->doc[0]; |
|
1182 | 1182 | } |
1183 | 1183 | |
1184 | 1184 | // parse html content |
1185 | 1185 | protected function parse() |
1186 | 1186 | { |
1187 | - if (($s = $this->copy_until_char('<'))==='') |
|
1187 | + if (($s = $this->copy_until_char('<')) === '') |
|
1188 | 1188 | { |
1189 | 1189 | return $this->read_tag(); |
1190 | 1190 | } |
@@ -1213,18 +1213,18 @@ discard block |
||
1213 | 1213 | if ($success) |
1214 | 1214 | { |
1215 | 1215 | $charset = $matches[1]; |
1216 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'header content-type found charset of: ' . $charset);} |
|
1216 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'header content-type found charset of: '.$charset); } |
|
1217 | 1217 | } |
1218 | 1218 | |
1219 | 1219 | } |
1220 | 1220 | |
1221 | 1221 | if (empty($charset)) |
1222 | 1222 | { |
1223 | - $el = $this->root->find('meta[http-equiv=Content-Type]',0, true); |
|
1223 | + $el = $this->root->find('meta[http-equiv=Content-Type]', 0, true); |
|
1224 | 1224 | if (!empty($el)) |
1225 | 1225 | { |
1226 | 1226 | $fullvalue = $el->content; |
1227 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag found' . $fullvalue);} |
|
1227 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag found'.$fullvalue); } |
|
1228 | 1228 | |
1229 | 1229 | if (!empty($fullvalue)) |
1230 | 1230 | { |
@@ -1236,7 +1236,7 @@ discard block |
||
1236 | 1236 | else |
1237 | 1237 | { |
1238 | 1238 | // If there is a meta tag, and they don't specify the character set, research says that it's typically ISO-8859-1 |
1239 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.');} |
|
1239 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.'); } |
|
1240 | 1240 | $charset = 'ISO-8859-1'; |
1241 | 1241 | } |
1242 | 1242 | } |
@@ -1251,14 +1251,14 @@ discard block |
||
1251 | 1251 | if (function_exists('mb_detect_encoding')) |
1252 | 1252 | { |
1253 | 1253 | // Have php try to detect the encoding from the text given to us. |
1254 | - $charset = mb_detect_encoding($this->root->plaintext . "ascii", $encoding_list = array( "UTF-8", "CP1252" ) ); |
|
1255 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'mb_detect found: ' . $charset);} |
|
1254 | + $charset = mb_detect_encoding($this->root->plaintext."ascii", $encoding_list = array("UTF-8", "CP1252")); |
|
1255 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'mb_detect found: '.$charset); } |
|
1256 | 1256 | } |
1257 | 1257 | |
1258 | 1258 | // and if this doesn't work... then we need to just wrongheadedly assume it's UTF-8 so that we can move on - cause this will usually give us most of what we need... |
1259 | 1259 | if ($charset === false) |
1260 | 1260 | { |
1261 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'since mb_detect failed - using default of utf-8');} |
|
1261 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'since mb_detect failed - using default of utf-8'); } |
|
1262 | 1262 | $charset = 'UTF-8'; |
1263 | 1263 | } |
1264 | 1264 | } |
@@ -1266,11 +1266,11 @@ discard block |
||
1266 | 1266 | // Since CP1252 is a superset, if we get one of it's subsets, we want it instead. |
1267 | 1267 | if ((strtolower($charset) == strtolower('ISO-8859-1')) || (strtolower($charset) == strtolower('Latin1')) || (strtolower($charset) == strtolower('Latin-1'))) |
1268 | 1268 | { |
1269 | - if (is_object($debug_object)) {$debug_object->debug_log(2, 'replacing ' . $charset . ' with CP1252 as its a superset');} |
|
1269 | + if (is_object($debug_object)) {$debug_object->debug_log(2, 'replacing '.$charset.' with CP1252 as its a superset'); } |
|
1270 | 1270 | $charset = 'CP1252'; |
1271 | 1271 | } |
1272 | 1272 | |
1273 | - if (is_object($debug_object)) {$debug_object->debug_log(1, 'EXIT - ' . $charset);} |
|
1273 | + if (is_object($debug_object)) {$debug_object->debug_log(1, 'EXIT - '.$charset); } |
|
1274 | 1274 | |
1275 | 1275 | return $this->_charset = $charset; |
1276 | 1276 | } |
@@ -1278,41 +1278,41 @@ discard block |
||
1278 | 1278 | // read tag info |
1279 | 1279 | protected function read_tag() |
1280 | 1280 | { |
1281 | - if ($this->char!=='<') |
|
1281 | + if ($this->char !== '<') |
|
1282 | 1282 | { |
1283 | 1283 | $this->root->_[HDOM_INFO_END] = $this->cursor; |
1284 | 1284 | return false; |
1285 | 1285 | } |
1286 | 1286 | $begin_tag_pos = $this->pos; |
1287 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1287 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1288 | 1288 | |
1289 | 1289 | // end tag |
1290 | - if ($this->char==='/') |
|
1290 | + if ($this->char === '/') |
|
1291 | 1291 | { |
1292 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1292 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1293 | 1293 | // This represents the change in the simple_html_dom trunk from revision 180 to 181. |
1294 | 1294 | // $this->skip($this->token_blank_t); |
1295 | 1295 | $this->skip($this->token_blank); |
1296 | 1296 | $tag = $this->copy_until_char('>'); |
1297 | 1297 | |
1298 | 1298 | // skip attributes in end tag |
1299 | - if (($pos = strpos($tag, ' '))!==false) |
|
1299 | + if (($pos = strpos($tag, ' ')) !== false) |
|
1300 | 1300 | $tag = substr($tag, 0, $pos); |
1301 | 1301 | |
1302 | 1302 | $parent_lower = strtolower($this->parent->tag); |
1303 | 1303 | $tag_lower = strtolower($tag); |
1304 | 1304 | |
1305 | - if ($parent_lower!==$tag_lower) |
|
1305 | + if ($parent_lower !== $tag_lower) |
|
1306 | 1306 | { |
1307 | 1307 | if (isset($this->optional_closing_tags[$parent_lower]) && isset($this->block_tags[$tag_lower])) |
1308 | 1308 | { |
1309 | 1309 | $this->parent->_[HDOM_INFO_END] = 0; |
1310 | 1310 | $org_parent = $this->parent; |
1311 | 1311 | |
1312 | - while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) |
|
1312 | + while (($this->parent->parent) && strtolower($this->parent->tag) !== $tag_lower) |
|
1313 | 1313 | $this->parent = $this->parent->parent; |
1314 | 1314 | |
1315 | - if (strtolower($this->parent->tag)!==$tag_lower) { |
|
1315 | + if (strtolower($this->parent->tag) !== $tag_lower) { |
|
1316 | 1316 | $this->parent = $org_parent; // restore origonal parent |
1317 | 1317 | if ($this->parent->parent) $this->parent = $this->parent->parent; |
1318 | 1318 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
@@ -1324,17 +1324,17 @@ discard block |
||
1324 | 1324 | $this->parent->_[HDOM_INFO_END] = 0; |
1325 | 1325 | $org_parent = $this->parent; |
1326 | 1326 | |
1327 | - while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) |
|
1327 | + while (($this->parent->parent) && strtolower($this->parent->tag) !== $tag_lower) |
|
1328 | 1328 | $this->parent = $this->parent->parent; |
1329 | 1329 | |
1330 | - if (strtolower($this->parent->tag)!==$tag_lower) |
|
1330 | + if (strtolower($this->parent->tag) !== $tag_lower) |
|
1331 | 1331 | { |
1332 | 1332 | $this->parent = $org_parent; // restore origonal parent |
1333 | 1333 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
1334 | 1334 | return $this->as_text_node($tag); |
1335 | 1335 | } |
1336 | 1336 | } |
1337 | - else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower) |
|
1337 | + else if (($this->parent->parent) && strtolower($this->parent->parent->tag) === $tag_lower) |
|
1338 | 1338 | { |
1339 | 1339 | $this->parent->_[HDOM_INFO_END] = 0; |
1340 | 1340 | $this->parent = $this->parent->parent; |
@@ -1346,7 +1346,7 @@ discard block |
||
1346 | 1346 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
1347 | 1347 | if ($this->parent->parent) $this->parent = $this->parent->parent; |
1348 | 1348 | |
1349 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1349 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1350 | 1350 | return true; |
1351 | 1351 | } |
1352 | 1352 | |
@@ -1357,25 +1357,25 @@ discard block |
||
1357 | 1357 | $node->tag_start = $begin_tag_pos; |
1358 | 1358 | |
1359 | 1359 | // doctype, cdata & comments... |
1360 | - if (isset($tag[0]) && $tag[0]==='!') { |
|
1361 | - $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until_char('>'); |
|
1360 | + if (isset($tag[0]) && $tag[0] === '!') { |
|
1361 | + $node->_[HDOM_INFO_TEXT] = '<'.$tag.$this->copy_until_char('>'); |
|
1362 | 1362 | |
1363 | - if (isset($tag[2]) && $tag[1]==='-' && $tag[2]==='-') { |
|
1363 | + if (isset($tag[2]) && $tag[1] === '-' && $tag[2] === '-') { |
|
1364 | 1364 | $node->nodetype = HDOM_TYPE_COMMENT; |
1365 | 1365 | $node->tag = 'comment'; |
1366 | 1366 | } else { |
1367 | 1367 | $node->nodetype = HDOM_TYPE_UNKNOWN; |
1368 | 1368 | $node->tag = 'unknown'; |
1369 | 1369 | } |
1370 | - if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>'; |
|
1370 | + if ($this->char === '>') $node->_[HDOM_INFO_TEXT] .= '>'; |
|
1371 | 1371 | $this->link_nodes($node, true); |
1372 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1372 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1373 | 1373 | return true; |
1374 | 1374 | } |
1375 | 1375 | |
1376 | 1376 | // text |
1377 | - if ($pos=strpos($tag, '<')!==false) { |
|
1378 | - $tag = '<' . substr($tag, 0, -1); |
|
1377 | + if ($pos = strpos($tag, '<') !== false) { |
|
1378 | + $tag = '<'.substr($tag, 0, -1); |
|
1379 | 1379 | $node->_[HDOM_INFO_TEXT] = $tag; |
1380 | 1380 | $this->link_nodes($node, false); |
1381 | 1381 | $this->char = $this->doc[--$this->pos]; // prev |
@@ -1383,15 +1383,15 @@ discard block |
||
1383 | 1383 | } |
1384 | 1384 | |
1385 | 1385 | if (!preg_match("/^[\w-:]+$/", $tag)) { |
1386 | - $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>'); |
|
1387 | - if ($this->char==='<') { |
|
1386 | + $node->_[HDOM_INFO_TEXT] = '<'.$tag.$this->copy_until('<>'); |
|
1387 | + if ($this->char === '<') { |
|
1388 | 1388 | $this->link_nodes($node, false); |
1389 | 1389 | return true; |
1390 | 1390 | } |
1391 | 1391 | |
1392 | - if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>'; |
|
1392 | + if ($this->char === '>') $node->_[HDOM_INFO_TEXT] .= '>'; |
|
1393 | 1393 | $this->link_nodes($node, false); |
1394 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1394 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1395 | 1395 | return true; |
1396 | 1396 | } |
1397 | 1397 | |
@@ -1401,7 +1401,7 @@ discard block |
||
1401 | 1401 | $node->tag = ($this->lowercase) ? $tag_lower : $tag; |
1402 | 1402 | |
1403 | 1403 | // handle optional closing tags |
1404 | - if (isset($this->optional_closing_tags[$tag_lower]) ) |
|
1404 | + if (isset($this->optional_closing_tags[$tag_lower])) |
|
1405 | 1405 | { |
1406 | 1406 | while (isset($this->optional_closing_tags[$tag_lower][strtolower($this->parent->tag)])) |
1407 | 1407 | { |
@@ -1417,67 +1417,67 @@ discard block |
||
1417 | 1417 | // attributes |
1418 | 1418 | do |
1419 | 1419 | { |
1420 | - if ($this->char!==null && $space[0]==='') |
|
1420 | + if ($this->char !== null && $space[0] === '') |
|
1421 | 1421 | { |
1422 | 1422 | break; |
1423 | 1423 | } |
1424 | 1424 | $name = $this->copy_until($this->token_equal); |
1425 | - if ($guard===$this->pos) |
|
1425 | + if ($guard === $this->pos) |
|
1426 | 1426 | { |
1427 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1427 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1428 | 1428 | continue; |
1429 | 1429 | } |
1430 | 1430 | $guard = $this->pos; |
1431 | 1431 | |
1432 | 1432 | // handle endless '<' |
1433 | - if ($this->pos>=$this->size-1 && $this->char!=='>') { |
|
1433 | + if ($this->pos >= $this->size - 1 && $this->char !== '>') { |
|
1434 | 1434 | $node->nodetype = HDOM_TYPE_TEXT; |
1435 | 1435 | $node->_[HDOM_INFO_END] = 0; |
1436 | - $node->_[HDOM_INFO_TEXT] = '<'.$tag . $space[0] . $name; |
|
1436 | + $node->_[HDOM_INFO_TEXT] = '<'.$tag.$space[0].$name; |
|
1437 | 1437 | $node->tag = 'text'; |
1438 | 1438 | $this->link_nodes($node, false); |
1439 | 1439 | return true; |
1440 | 1440 | } |
1441 | 1441 | |
1442 | 1442 | // handle mismatch '<' |
1443 | - if ($this->doc[$this->pos-1]=='<') { |
|
1443 | + if ($this->doc[$this->pos - 1] == '<') { |
|
1444 | 1444 | $node->nodetype = HDOM_TYPE_TEXT; |
1445 | 1445 | $node->tag = 'text'; |
1446 | 1446 | $node->attr = array(); |
1447 | 1447 | $node->_[HDOM_INFO_END] = 0; |
1448 | - $node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1); |
|
1448 | + $node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos - $begin_tag_pos - 1); |
|
1449 | 1449 | $this->pos -= 2; |
1450 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1450 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1451 | 1451 | $this->link_nodes($node, false); |
1452 | 1452 | return true; |
1453 | 1453 | } |
1454 | 1454 | |
1455 | - if ($name!=='/' && $name!=='') { |
|
1455 | + if ($name !== '/' && $name !== '') { |
|
1456 | 1456 | $space[1] = $this->copy_skip($this->token_blank); |
1457 | 1457 | $name = $this->restore_noise($name); |
1458 | 1458 | if ($this->lowercase) $name = strtolower($name); |
1459 | - if ($this->char==='=') { |
|
1460 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1459 | + if ($this->char === '=') { |
|
1460 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1461 | 1461 | $this->parse_attr($node, $name, $space); |
1462 | 1462 | } |
1463 | 1463 | else { |
1464 | 1464 | //no value attr: nowrap, checked selected... |
1465 | 1465 | $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO; |
1466 | 1466 | $node->attr[$name] = true; |
1467 | - if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev |
|
1467 | + if ($this->char != '>') $this->char = $this->doc[--$this->pos]; // prev |
|
1468 | 1468 | } |
1469 | 1469 | $node->_[HDOM_INFO_SPACE][] = $space; |
1470 | 1470 | $space = array($this->copy_skip($this->token_blank), '', ''); |
1471 | 1471 | } |
1472 | 1472 | else |
1473 | 1473 | break; |
1474 | - } while ($this->char!=='>' && $this->char!=='/'); |
|
1474 | + } while ($this->char !== '>' && $this->char !== '/'); |
|
1475 | 1475 | |
1476 | 1476 | $this->link_nodes($node, true); |
1477 | 1477 | $node->_[HDOM_INFO_ENDSPACE] = $space[0]; |
1478 | 1478 | |
1479 | 1479 | // check self closing |
1480 | - if ($this->copy_until_char_escape('>')==='/') |
|
1480 | + if ($this->copy_until_char_escape('>') === '/') |
|
1481 | 1481 | { |
1482 | 1482 | $node->_[HDOM_INFO_ENDSPACE] .= '/'; |
1483 | 1483 | $node->_[HDOM_INFO_END] = 0; |
@@ -1487,7 +1487,7 @@ discard block |
||
1487 | 1487 | // reset parent |
1488 | 1488 | if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node; |
1489 | 1489 | } |
1490 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1490 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1491 | 1491 | |
1492 | 1492 | // If it's a BR tag, we need to set it's text to the default text. |
1493 | 1493 | // This way when we see it in plaintext, we can generate formatting that the user wants. |
@@ -1514,15 +1514,15 @@ discard block |
||
1514 | 1514 | switch ($this->char) { |
1515 | 1515 | case '"': |
1516 | 1516 | $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE; |
1517 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1517 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1518 | 1518 | $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('"')); |
1519 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1519 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1520 | 1520 | break; |
1521 | 1521 | case '\'': |
1522 | 1522 | $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE; |
1523 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1523 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1524 | 1524 | $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('\'')); |
1525 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1525 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1526 | 1526 | break; |
1527 | 1527 | default: |
1528 | 1528 | $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO; |
@@ -1553,16 +1553,16 @@ discard block |
||
1553 | 1553 | { |
1554 | 1554 | $node = new simple_html_dom_node($this); |
1555 | 1555 | ++$this->cursor; |
1556 | - $node->_[HDOM_INFO_TEXT] = '</' . $tag . '>'; |
|
1556 | + $node->_[HDOM_INFO_TEXT] = '</'.$tag.'>'; |
|
1557 | 1557 | $this->link_nodes($node, false); |
1558 | - $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1558 | + $this->char = (++$this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1559 | 1559 | return true; |
1560 | 1560 | } |
1561 | 1561 | |
1562 | 1562 | protected function skip($chars) |
1563 | 1563 | { |
1564 | 1564 | $this->pos += strspn($this->doc, $chars, $this->pos); |
1565 | - $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1565 | + $this->char = ($this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1566 | 1566 | } |
1567 | 1567 | |
1568 | 1568 | protected function copy_skip($chars) |
@@ -1570,8 +1570,8 @@ discard block |
||
1570 | 1570 | $pos = $this->pos; |
1571 | 1571 | $len = strspn($this->doc, $chars, $pos); |
1572 | 1572 | $this->pos += $len; |
1573 | - $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1574 | - if ($len===0) return ''; |
|
1573 | + $this->char = ($this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1574 | + if ($len === 0) return ''; |
|
1575 | 1575 | return substr($this->doc, $pos, $len); |
1576 | 1576 | } |
1577 | 1577 | |
@@ -1580,70 +1580,70 @@ discard block |
||
1580 | 1580 | $pos = $this->pos; |
1581 | 1581 | $len = strcspn($this->doc, $chars, $pos); |
1582 | 1582 | $this->pos += $len; |
1583 | - $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
|
1583 | + $this->char = ($this->pos < $this->size) ? $this->doc[$this->pos] : null; // next |
|
1584 | 1584 | return substr($this->doc, $pos, $len); |
1585 | 1585 | } |
1586 | 1586 | |
1587 | 1587 | protected function copy_until_char($char) |
1588 | 1588 | { |
1589 | - if ($this->char===null) return ''; |
|
1589 | + if ($this->char === null) return ''; |
|
1590 | 1590 | |
1591 | - if (($pos = strpos($this->doc, $char, $this->pos))===false) { |
|
1592 | - $ret = substr($this->doc, $this->pos, $this->size-$this->pos); |
|
1591 | + if (($pos = strpos($this->doc, $char, $this->pos)) === false) { |
|
1592 | + $ret = substr($this->doc, $this->pos, $this->size - $this->pos); |
|
1593 | 1593 | $this->char = null; |
1594 | 1594 | $this->pos = $this->size; |
1595 | 1595 | return $ret; |
1596 | 1596 | } |
1597 | 1597 | |
1598 | - if ($pos===$this->pos) return ''; |
|
1598 | + if ($pos === $this->pos) return ''; |
|
1599 | 1599 | $pos_old = $this->pos; |
1600 | 1600 | $this->char = $this->doc[$pos]; |
1601 | 1601 | $this->pos = $pos; |
1602 | - return substr($this->doc, $pos_old, $pos-$pos_old); |
|
1602 | + return substr($this->doc, $pos_old, $pos - $pos_old); |
|
1603 | 1603 | } |
1604 | 1604 | |
1605 | 1605 | protected function copy_until_char_escape($char) |
1606 | 1606 | { |
1607 | - if ($this->char===null) return ''; |
|
1607 | + if ($this->char === null) return ''; |
|
1608 | 1608 | |
1609 | 1609 | $start = $this->pos; |
1610 | 1610 | while (1) |
1611 | 1611 | { |
1612 | - if (($pos = strpos($this->doc, $char, $start))===false) |
|
1612 | + if (($pos = strpos($this->doc, $char, $start)) === false) |
|
1613 | 1613 | { |
1614 | - $ret = substr($this->doc, $this->pos, $this->size-$this->pos); |
|
1614 | + $ret = substr($this->doc, $this->pos, $this->size - $this->pos); |
|
1615 | 1615 | $this->char = null; |
1616 | 1616 | $this->pos = $this->size; |
1617 | 1617 | return $ret; |
1618 | 1618 | } |
1619 | 1619 | |
1620 | - if ($pos===$this->pos) return ''; |
|
1620 | + if ($pos === $this->pos) return ''; |
|
1621 | 1621 | |
1622 | - if ($this->doc[$pos-1]==='\\') { |
|
1623 | - $start = $pos+1; |
|
1622 | + if ($this->doc[$pos - 1] === '\\') { |
|
1623 | + $start = $pos + 1; |
|
1624 | 1624 | continue; |
1625 | 1625 | } |
1626 | 1626 | |
1627 | 1627 | $pos_old = $this->pos; |
1628 | 1628 | $this->char = $this->doc[$pos]; |
1629 | 1629 | $this->pos = $pos; |
1630 | - return substr($this->doc, $pos_old, $pos-$pos_old); |
|
1630 | + return substr($this->doc, $pos_old, $pos - $pos_old); |
|
1631 | 1631 | } |
1632 | 1632 | } |
1633 | 1633 | |
1634 | 1634 | // remove noise from html content |
1635 | 1635 | // save the noise in the $this->noise array. |
1636 | - protected function remove_noise($pattern, $remove_tag=false) |
|
1636 | + protected function remove_noise($pattern, $remove_tag = false) |
|
1637 | 1637 | { |
1638 | 1638 | global $debug_object; |
1639 | 1639 | if (is_object($debug_object)) { $debug_object->debug_log_entry(1); } |
1640 | 1640 | |
1641 | 1641 | $count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE); |
1642 | 1642 | |
1643 | - for ($i=$count-1; $i>-1; --$i) |
|
1643 | + for ($i = $count - 1; $i > -1; --$i) |
|
1644 | 1644 | { |
1645 | - $key = '___noise___'.sprintf('% 5d', count($this->noise)+1000); |
|
1646 | - if (is_object($debug_object)) { $debug_object->debug_log(2, 'key is: ' . $key); } |
|
1645 | + $key = '___noise___'.sprintf('% 5d', count($this->noise) + 1000); |
|
1646 | + if (is_object($debug_object)) { $debug_object->debug_log(2, 'key is: '.$key); } |
|
1647 | 1647 | $idx = ($remove_tag) ? 0 : 1; |
1648 | 1648 | $this->noise[$key] = $matches[$i][$idx][0]; |
1649 | 1649 | $this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0])); |
@@ -1651,7 +1651,7 @@ discard block |
||
1651 | 1651 | |
1652 | 1652 | // reset the length of content |
1653 | 1653 | $this->size = strlen($this->doc); |
1654 | - if ($this->size>0) |
|
1654 | + if ($this->size > 0) |
|
1655 | 1655 | { |
1656 | 1656 | $this->char = $this->doc[0]; |
1657 | 1657 | } |
@@ -1663,28 +1663,28 @@ discard block |
||
1663 | 1663 | global $debug_object; |
1664 | 1664 | if (is_object($debug_object)) { $debug_object->debug_log_entry(1); } |
1665 | 1665 | |
1666 | - while (($pos=strpos($text, '___noise___'))!==false) |
|
1666 | + while (($pos = strpos($text, '___noise___')) !== false) |
|
1667 | 1667 | { |
1668 | 1668 | // Sometimes there is a broken piece of markup, and we don't GET the pos+11 etc... token which indicates a problem outside of us... |
1669 | - if (strlen($text) > $pos+15) |
|
1669 | + if (strlen($text) > $pos + 15) |
|
1670 | 1670 | { |
1671 | - $key = '___noise___'.$text[$pos+11].$text[$pos+12].$text[$pos+13].$text[$pos+14].$text[$pos+15]; |
|
1672 | - if (is_object($debug_object)) { $debug_object->debug_log(2, 'located key of: ' . $key); } |
|
1671 | + $key = '___noise___'.$text[$pos + 11].$text[$pos + 12].$text[$pos + 13].$text[$pos + 14].$text[$pos + 15]; |
|
1672 | + if (is_object($debug_object)) { $debug_object->debug_log(2, 'located key of: '.$key); } |
|
1673 | 1673 | |
1674 | 1674 | if (isset($this->noise[$key])) |
1675 | 1675 | { |
1676 | - $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+16); |
|
1676 | + $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos + 16); |
|
1677 | 1677 | } |
1678 | 1678 | else |
1679 | 1679 | { |
1680 | 1680 | // do this to prevent an infinite loop. |
1681 | - $text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key . substr($text, $pos+16); |
|
1681 | + $text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key.substr($text, $pos + 16); |
|
1682 | 1682 | } |
1683 | 1683 | } |
1684 | 1684 | else |
1685 | 1685 | { |
1686 | 1686 | // There is no valid key being given back to us... We must get rid of the ___noise___ or we will have a problem. |
1687 | - $text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY' . substr($text, $pos+11); |
|
1687 | + $text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY'.substr($text, $pos + 11); |
|
1688 | 1688 | } |
1689 | 1689 | } |
1690 | 1690 | return $text; |
@@ -1696,9 +1696,9 @@ discard block |
||
1696 | 1696 | global $debug_object; |
1697 | 1697 | if (is_object($debug_object)) { $debug_object->debug_log_entry(1); } |
1698 | 1698 | |
1699 | - foreach($this->noise as $noiseElement) |
|
1699 | + foreach ($this->noise as $noiseElement) |
|
1700 | 1700 | { |
1701 | - if (strpos($noiseElement, $text)!==false) |
|
1701 | + if (strpos($noiseElement, $text) !== false) |
|
1702 | 1702 | { |
1703 | 1703 | return $noiseElement; |
1704 | 1704 | } |
@@ -1727,16 +1727,16 @@ discard block |
||
1727 | 1727 | } |
1728 | 1728 | |
1729 | 1729 | // camel naming conventions |
1730 | - function childNodes($idx=-1) {return $this->root->childNodes($idx);} |
|
1731 | - function firstChild() {return $this->root->first_child();} |
|
1732 | - function lastChild() {return $this->root->last_child();} |
|
1733 | - function createElement($name, $value=null) {return @str_get_html("<$name>$value</$name>")->first_child();} |
|
1734 | - function createTextNode($value) {return @end(str_get_html($value)->nodes);} |
|
1735 | - function getElementById($id) {return $this->find("#$id", 0);} |
|
1736 | - function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);} |
|
1737 | - function getElementByTagName($name) {return $this->find($name, 0);} |
|
1738 | - function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);} |
|
1739 | - function loadFile() {$args = func_get_args();$this->load_file($args);} |
|
1730 | + function childNodes($idx = -1) {return $this->root->childNodes($idx); } |
|
1731 | + function firstChild() {return $this->root->first_child(); } |
|
1732 | + function lastChild() {return $this->root->last_child(); } |
|
1733 | + function createElement($name, $value = null) {return @str_get_html("<$name>$value</$name>")->first_child(); } |
|
1734 | + function createTextNode($value) {return @end(str_get_html($value)->nodes); } |
|
1735 | + function getElementById($id) {return $this->find("#$id", 0); } |
|
1736 | + function getElementsById($id, $idx = null) {return $this->find("#$id", $idx); } |
|
1737 | + function getElementByTagName($name) {return $this->find($name, 0); } |
|
1738 | + function getElementsByTagName($name, $idx = -1) {return $this->find($name, $idx); } |
|
1739 | + function loadFile() {$args = func_get_args(); $this->load_file($args); } |
|
1740 | 1740 | } |
1741 | 1741 | |
1742 | 1742 | ?> |
1743 | 1743 | \ No newline at end of file |
@@ -159,8 +159,9 @@ discard block |
||
159 | 159 | if ($show_attr && count($this->attr)>0) |
160 | 160 | { |
161 | 161 | echo '('; |
162 | - foreach ($this->attr as $k=>$v) |
|
163 | - echo "[$k]=>\"".$this->$k.'", '; |
|
162 | + foreach ($this->attr as $k=>$v) { |
|
163 | + echo "[$k]=>\"".$this->$k.'", '; |
|
164 | + } |
|
164 | 165 | echo ')'; |
165 | 166 | } |
166 | 167 | echo "\n"; |
@@ -218,8 +219,7 @@ discard block |
||
218 | 219 | if (isset($node->_[HDOM_INFO_INNER])) |
219 | 220 | { |
220 | 221 | $string .= $node->_[HDOM_INFO_INNER] . "'"; |
221 | - } |
|
222 | - else |
|
222 | + } else |
|
223 | 223 | { |
224 | 224 | $string .= ' NULL '; |
225 | 225 | } |
@@ -233,8 +233,7 @@ discard block |
||
233 | 233 | { |
234 | 234 | echo $string; |
235 | 235 | return; |
236 | - } |
|
237 | - else |
|
236 | + } else |
|
238 | 237 | { |
239 | 238 | return $string; |
240 | 239 | } |
@@ -320,12 +319,17 @@ discard block |
||
320 | 319 | // returns the previous sibling of node |
321 | 320 | function prev_sibling() |
322 | 321 | { |
323 | - if ($this->parent===null) return null; |
|
322 | + if ($this->parent===null) { |
|
323 | + return null; |
|
324 | + } |
|
324 | 325 | $idx = 0; |
325 | 326 | $count = count($this->parent->children); |
326 | - while ($idx<$count && $this!==$this->parent->children[$idx]) |
|
327 | - ++$idx; |
|
328 | - if (--$idx<0) return null; |
|
327 | + while ($idx<$count && $this!==$this->parent->children[$idx]) { |
|
328 | + ++$idx; |
|
329 | + } |
|
330 | + if (--$idx<0) { |
|
331 | + return null; |
|
332 | + } |
|
329 | 333 | return $this->parent->children[$idx]; |
330 | 334 | } |
331 | 335 | |
@@ -354,12 +358,17 @@ discard block |
||
354 | 358 | // get dom node's inner html |
355 | 359 | function innertext() |
356 | 360 | { |
357 | - if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; |
|
358 | - if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
361 | + if (isset($this->_[HDOM_INFO_INNER])) { |
|
362 | + return $this->_[HDOM_INFO_INNER]; |
|
363 | + } |
|
364 | + if (isset($this->_[HDOM_INFO_TEXT])) { |
|
365 | + return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
366 | + } |
|
359 | 367 | |
360 | 368 | $ret = ''; |
361 | - foreach ($this->nodes as $n) |
|
362 | - $ret .= $n->outertext(); |
|
369 | + foreach ($this->nodes as $n) { |
|
370 | + $ret .= $n->outertext(); |
|
371 | + } |
|
363 | 372 | return $ret; |
364 | 373 | } |
365 | 374 | |
@@ -380,7 +389,9 @@ discard block |
||
380 | 389 | $debug_object->debug_log(1, 'Innertext of tag: ' . $this->tag . $text); |
381 | 390 | } |
382 | 391 | |
383 | - if ($this->tag==='root') return $this->innertext(); |
|
392 | + if ($this->tag==='root') { |
|
393 | + return $this->innertext(); |
|
394 | + } |
|
384 | 395 | |
385 | 396 | // trigger callback |
386 | 397 | if ($this->dom && $this->dom->callback!==null) |
@@ -388,8 +399,12 @@ discard block |
||
388 | 399 | call_user_func_array($this->dom->callback, array($this)); |
389 | 400 | } |
390 | 401 | |
391 | - if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER]; |
|
392 | - if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
402 | + if (isset($this->_[HDOM_INFO_OUTER])) { |
|
403 | + return $this->_[HDOM_INFO_OUTER]; |
|
404 | + } |
|
405 | + if (isset($this->_[HDOM_INFO_TEXT])) { |
|
406 | + return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
407 | + } |
|
393 | 408 | |
394 | 409 | // render begin tag |
395 | 410 | if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]) |
@@ -418,23 +433,30 @@ discard block |
||
418 | 433 | } |
419 | 434 | |
420 | 435 | // render end tag |
421 | - if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) |
|
422 | - $ret .= '</'.$this->tag.'>'; |
|
436 | + if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) { |
|
437 | + $ret .= '</'.$this->tag.'>'; |
|
438 | + } |
|
423 | 439 | return $ret; |
424 | 440 | } |
425 | 441 | |
426 | 442 | // get dom node's plain text |
427 | 443 | function text() |
428 | 444 | { |
429 | - if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; |
|
445 | + if (isset($this->_[HDOM_INFO_INNER])) { |
|
446 | + return $this->_[HDOM_INFO_INNER]; |
|
447 | + } |
|
430 | 448 | switch ($this->nodetype) |
431 | 449 | { |
432 | 450 | case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
433 | 451 | case HDOM_TYPE_COMMENT: return ''; |
434 | 452 | case HDOM_TYPE_UNKNOWN: return ''; |
435 | 453 | } |
436 | - if (strcasecmp($this->tag, 'script')===0) return ''; |
|
437 | - if (strcasecmp($this->tag, 'style')===0) return ''; |
|
454 | + if (strcasecmp($this->tag, 'script')===0) { |
|
455 | + return ''; |
|
456 | + } |
|
457 | + if (strcasecmp($this->tag, 'style')===0) { |
|
458 | + return ''; |
|
459 | + } |
|
438 | 460 | |
439 | 461 | $ret = ''; |
440 | 462 | // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL. |
@@ -470,7 +492,9 @@ discard block |
||
470 | 492 | function makeup() |
471 | 493 | { |
472 | 494 | // text, comment, unknown |
473 | - if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
495 | + if (isset($this->_[HDOM_INFO_TEXT])) { |
|
496 | + return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
|
497 | + } |
|
474 | 498 | |
475 | 499 | $ret = '<'.$this->tag; |
476 | 500 | $i = -1; |
@@ -480,14 +504,15 @@ discard block |
||
480 | 504 | ++$i; |
481 | 505 | |
482 | 506 | // skip removed attribute |
483 | - if ($val===null || $val===false) |
|
484 | - continue; |
|
507 | + if ($val===null || $val===false) { |
|
508 | + continue; |
|
509 | + } |
|
485 | 510 | |
486 | 511 | $ret .= $this->_[HDOM_INFO_SPACE][$i][0]; |
487 | 512 | //no value attr: nowrap, checked selected... |
488 | - if ($val===true) |
|
489 | - $ret .= $key; |
|
490 | - else { |
|
513 | + if ($val===true) { |
|
514 | + $ret .= $key; |
|
515 | + } else { |
|
491 | 516 | switch ($this->_[HDOM_INFO_QUOTE][$i]) |
492 | 517 | { |
493 | 518 | case HDOM_QUOTE_DOUBLE: $quote = '"'; break; |
@@ -506,7 +531,9 @@ discard block |
||
506 | 531 | function find($selector, $idx=null, $lowercase=false) |
507 | 532 | { |
508 | 533 | $selectors = $this->parse_selector($selector); |
509 | - if (($count=count($selectors))===0) return array(); |
|
534 | + if (($count=count($selectors))===0) { |
|
535 | + return array(); |
|
536 | + } |
|
510 | 537 | $found_keys = array(); |
511 | 538 | |
512 | 539 | // find each selector |
@@ -514,8 +541,12 @@ discard block |
||
514 | 541 | { |
515 | 542 | // The change on the below line was documented on the sourceforge code tracker id 2788009 |
516 | 543 | // used to be: if (($levle=count($selectors[0]))===0) return array(); |
517 | - if (($levle=count($selectors[$c]))===0) return array(); |
|
518 | - if (!isset($this->_[HDOM_INFO_BEGIN])) return array(); |
|
544 | + if (($levle=count($selectors[$c]))===0) { |
|
545 | + return array(); |
|
546 | + } |
|
547 | + if (!isset($this->_[HDOM_INFO_BEGIN])) { |
|
548 | + return array(); |
|
549 | + } |
|
519 | 550 | |
520 | 551 | $head = array($this->_[HDOM_INFO_BEGIN]=>1); |
521 | 552 | |
@@ -545,12 +576,16 @@ discard block |
||
545 | 576 | ksort($found_keys); |
546 | 577 | |
547 | 578 | $found = array(); |
548 | - foreach ($found_keys as $k=>$v) |
|
549 | - $found[] = $this->dom->nodes[$k]; |
|
579 | + foreach ($found_keys as $k=>$v) { |
|
580 | + $found[] = $this->dom->nodes[$k]; |
|
581 | + } |
|
550 | 582 | |
551 | 583 | // return nth-element or array |
552 | - if (is_null($idx)) return $found; |
|
553 | - else if ($idx<0) $idx = count($found) + $idx; |
|
584 | + if (is_null($idx)) { |
|
585 | + return $found; |
|
586 | + } else if ($idx<0) { |
|
587 | + $idx = count($found) + $idx; |
|
588 | + } |
|
554 | 589 | return (isset($found[$idx])) ? $found[$idx] : null; |
555 | 590 | } |
556 | 591 | |
@@ -595,8 +630,9 @@ discard block |
||
595 | 630 | $pass = true; |
596 | 631 | |
597 | 632 | if ($tag==='*' && !$key) { |
598 | - if (in_array($node, $this->children, true)) |
|
599 | - $ret[$i] = 1; |
|
633 | + if (in_array($node, $this->children, true)) { |
|
634 | + $ret[$i] = 1; |
|
635 | + } |
|
600 | 636 | continue; |
601 | 637 | } |
602 | 638 | |
@@ -605,9 +641,13 @@ discard block |
||
605 | 641 | // compare key |
606 | 642 | if ($pass && $key) { |
607 | 643 | if ($no_key) { |
608 | - if (isset($node->attr[$key])) $pass=false; |
|
644 | + if (isset($node->attr[$key])) { |
|
645 | + $pass=false; |
|
646 | + } |
|
609 | 647 | } else { |
610 | - if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false; |
|
648 | + if (($key != "plaintext") && !isset($node->attr[$key])) { |
|
649 | + $pass=false; |
|
650 | + } |
|
611 | 651 | } |
612 | 652 | } |
613 | 653 | // compare value |
@@ -640,13 +680,19 @@ discard block |
||
640 | 680 | } else { |
641 | 681 | $check = $this->match($exp, $val, $k); |
642 | 682 | } |
643 | - if ($check) break; |
|
683 | + if ($check) { |
|
684 | + break; |
|
685 | + } |
|
644 | 686 | } |
645 | 687 | } |
646 | 688 | } |
647 | - if (!$check) $pass = false; |
|
689 | + if (!$check) { |
|
690 | + $pass = false; |
|
691 | + } |
|
692 | + } |
|
693 | + if ($pass) { |
|
694 | + $ret[$i] = 1; |
|
648 | 695 | } |
649 | - if ($pass) $ret[$i] = 1; |
|
650 | 696 | unset($node); |
651 | 697 | } |
652 | 698 | // It's passed by reference so this is actually what this function returns. |
@@ -696,9 +742,13 @@ discard block |
||
696 | 742 | |
697 | 743 | foreach ($matches as $m) { |
698 | 744 | $m[0] = trim($m[0]); |
699 | - if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue; |
|
745 | + if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') { |
|
746 | + continue; |
|
747 | + } |
|
700 | 748 | // for browser generated xpath |
701 | - if ($m[1]==='tbody') continue; |
|
749 | + if ($m[1]==='tbody') { |
|
750 | + continue; |
|
751 | + } |
|
702 | 752 | |
703 | 753 | list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false); |
704 | 754 | if (!empty($m[2])) {$key='id'; $val=$m[2];} |
@@ -718,8 +768,9 @@ discard block |
||
718 | 768 | $result = array(); |
719 | 769 | } |
720 | 770 | } |
721 | - if (count($result)>0) |
|
722 | - $selectors[] = $result; |
|
771 | + if (count($result)>0) { |
|
772 | + $selectors[] = $result; |
|
773 | + } |
|
723 | 774 | return $selectors; |
724 | 775 | } |
725 | 776 | |
@@ -748,7 +799,9 @@ discard block |
||
748 | 799 | { |
749 | 800 | case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value; |
750 | 801 | case 'innertext': |
751 | - if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value; |
|
802 | + if (isset($this->_[HDOM_INFO_TEXT])) { |
|
803 | + return $this->_[HDOM_INFO_TEXT] = $value; |
|
804 | + } |
|
752 | 805 | return $this->_[HDOM_INFO_INNER] = $value; |
753 | 806 | } |
754 | 807 | if (!isset($this->attr[$name])) |
@@ -772,8 +825,9 @@ discard block |
||
772 | 825 | } |
773 | 826 | |
774 | 827 | function __unset($name) { |
775 | - if (isset($this->attr[$name])) |
|
776 | - unset($this->attr[$name]); |
|
828 | + if (isset($this->attr[$name])) { |
|
829 | + unset($this->attr[$name]); |
|
830 | + } |
|
777 | 831 | } |
778 | 832 | |
779 | 833 | // PaperG - Function to convert the text from one character set to another if the two sets are not the same. |
@@ -800,8 +854,7 @@ discard block |
||
800 | 854 | if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text))) |
801 | 855 | { |
802 | 856 | $converted_text = $text; |
803 | - } |
|
804 | - else |
|
857 | + } else |
|
805 | 858 | { |
806 | 859 | $converted_text = iconv($sourceCharset, $targetCharset, $text); |
807 | 860 | } |
@@ -839,19 +892,31 @@ discard block |
||
839 | 892 | $c=ord($str[$i]); |
840 | 893 | if($c > 128) |
841 | 894 | { |
842 | - if(($c >= 254)) return false; |
|
843 | - elseif($c >= 252) $bits=6; |
|
844 | - elseif($c >= 248) $bits=5; |
|
845 | - elseif($c >= 240) $bits=4; |
|
846 | - elseif($c >= 224) $bits=3; |
|
847 | - elseif($c >= 192) $bits=2; |
|
848 | - else return false; |
|
849 | - if(($i+$bits) > $len) return false; |
|
895 | + if(($c >= 254)) { |
|
896 | + return false; |
|
897 | + } elseif($c >= 252) { |
|
898 | + $bits=6; |
|
899 | + } elseif($c >= 248) { |
|
900 | + $bits=5; |
|
901 | + } elseif($c >= 240) { |
|
902 | + $bits=4; |
|
903 | + } elseif($c >= 224) { |
|
904 | + $bits=3; |
|
905 | + } elseif($c >= 192) { |
|
906 | + $bits=2; |
|
907 | + } else { |
|
908 | + return false; |
|
909 | + } |
|
910 | + if(($i+$bits) > $len) { |
|
911 | + return false; |
|
912 | + } |
|
850 | 913 | while($bits > 1) |
851 | 914 | { |
852 | 915 | $i++; |
853 | 916 | $b=ord($str[$i]); |
854 | - if($b < 128 || $b > 191) return false; |
|
917 | + if($b < 128 || $b > 191) { |
|
918 | + return false; |
|
919 | + } |
|
855 | 920 | $bits--; |
856 | 921 | } |
857 | 922 | } |
@@ -1035,8 +1100,7 @@ discard block |
||
1035 | 1100 | if (preg_match("/^http:\/\//i",$str) || is_file($str)) |
1036 | 1101 | { |
1037 | 1102 | $this->load_file($str); |
1038 | - } |
|
1039 | - else |
|
1103 | + } else |
|
1040 | 1104 | { |
1041 | 1105 | $this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText); |
1042 | 1106 | } |
@@ -1118,7 +1182,9 @@ discard block |
||
1118 | 1182 | function save($filepath='') |
1119 | 1183 | { |
1120 | 1184 | $ret = $this->root->innertext(); |
1121 | - if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX); |
|
1185 | + if ($filepath!=='') { |
|
1186 | + file_put_contents($filepath, $ret, LOCK_EX); |
|
1187 | + } |
|
1122 | 1188 | return $ret; |
1123 | 1189 | } |
1124 | 1190 | |
@@ -1134,7 +1200,10 @@ discard block |
||
1134 | 1200 | { |
1135 | 1201 | foreach ($this->nodes as $n) {$n->clear(); $n = null;} |
1136 | 1202 | // This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear. |
1137 | - if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;} |
|
1203 | + if (isset($this->children)) { |
|
1204 | + foreach ($this->children as $n) {$n->clear(); |
|
1205 | + } |
|
1206 | + $n = null;} |
|
1138 | 1207 | if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);} |
1139 | 1208 | if (isset($this->root)) {$this->root->clear(); unset($this->root);} |
1140 | 1209 | unset($this->doc); |
@@ -1178,7 +1247,9 @@ discard block |
||
1178 | 1247 | $this->root->_[HDOM_INFO_BEGIN] = -1; |
1179 | 1248 | $this->root->nodetype = HDOM_TYPE_ROOT; |
1180 | 1249 | $this->parent = $this->root; |
1181 | - if ($this->size>0) $this->char = $this->doc[0]; |
|
1250 | + if ($this->size>0) { |
|
1251 | + $this->char = $this->doc[0]; |
|
1252 | + } |
|
1182 | 1253 | } |
1183 | 1254 | |
1184 | 1255 | // parse html content |
@@ -1232,8 +1303,7 @@ discard block |
||
1232 | 1303 | if ($success) |
1233 | 1304 | { |
1234 | 1305 | $charset = $matches[1]; |
1235 | - } |
|
1236 | - else |
|
1306 | + } else |
|
1237 | 1307 | { |
1238 | 1308 | // If there is a meta tag, and they don't specify the character set, research says that it's typically ISO-8859-1 |
1239 | 1309 | if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.');} |
@@ -1296,8 +1366,9 @@ discard block |
||
1296 | 1366 | $tag = $this->copy_until_char('>'); |
1297 | 1367 | |
1298 | 1368 | // skip attributes in end tag |
1299 | - if (($pos = strpos($tag, ' '))!==false) |
|
1300 | - $tag = substr($tag, 0, $pos); |
|
1369 | + if (($pos = strpos($tag, ' '))!==false) { |
|
1370 | + $tag = substr($tag, 0, $pos); |
|
1371 | + } |
|
1301 | 1372 | |
1302 | 1373 | $parent_lower = strtolower($this->parent->tag); |
1303 | 1374 | $tag_lower = strtolower($tag); |
@@ -1309,23 +1380,26 @@ discard block |
||
1309 | 1380 | $this->parent->_[HDOM_INFO_END] = 0; |
1310 | 1381 | $org_parent = $this->parent; |
1311 | 1382 | |
1312 | - while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) |
|
1313 | - $this->parent = $this->parent->parent; |
|
1383 | + while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) { |
|
1384 | + $this->parent = $this->parent->parent; |
|
1385 | + } |
|
1314 | 1386 | |
1315 | 1387 | if (strtolower($this->parent->tag)!==$tag_lower) { |
1316 | 1388 | $this->parent = $org_parent; // restore origonal parent |
1317 | - if ($this->parent->parent) $this->parent = $this->parent->parent; |
|
1389 | + if ($this->parent->parent) { |
|
1390 | + $this->parent = $this->parent->parent; |
|
1391 | + } |
|
1318 | 1392 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
1319 | 1393 | return $this->as_text_node($tag); |
1320 | 1394 | } |
1321 | - } |
|
1322 | - else if (($this->parent->parent) && isset($this->block_tags[$tag_lower])) |
|
1395 | + } else if (($this->parent->parent) && isset($this->block_tags[$tag_lower])) |
|
1323 | 1396 | { |
1324 | 1397 | $this->parent->_[HDOM_INFO_END] = 0; |
1325 | 1398 | $org_parent = $this->parent; |
1326 | 1399 | |
1327 | - while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) |
|
1328 | - $this->parent = $this->parent->parent; |
|
1400 | + while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower) { |
|
1401 | + $this->parent = $this->parent->parent; |
|
1402 | + } |
|
1329 | 1403 | |
1330 | 1404 | if (strtolower($this->parent->tag)!==$tag_lower) |
1331 | 1405 | { |
@@ -1333,18 +1407,19 @@ discard block |
||
1333 | 1407 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
1334 | 1408 | return $this->as_text_node($tag); |
1335 | 1409 | } |
1336 | - } |
|
1337 | - else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower) |
|
1410 | + } else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower) |
|
1338 | 1411 | { |
1339 | 1412 | $this->parent->_[HDOM_INFO_END] = 0; |
1340 | 1413 | $this->parent = $this->parent->parent; |
1414 | + } else { |
|
1415 | + return $this->as_text_node($tag); |
|
1341 | 1416 | } |
1342 | - else |
|
1343 | - return $this->as_text_node($tag); |
|
1344 | 1417 | } |
1345 | 1418 | |
1346 | 1419 | $this->parent->_[HDOM_INFO_END] = $this->cursor; |
1347 | - if ($this->parent->parent) $this->parent = $this->parent->parent; |
|
1420 | + if ($this->parent->parent) { |
|
1421 | + $this->parent = $this->parent->parent; |
|
1422 | + } |
|
1348 | 1423 | |
1349 | 1424 | $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1350 | 1425 | return true; |
@@ -1367,7 +1442,9 @@ discard block |
||
1367 | 1442 | $node->nodetype = HDOM_TYPE_UNKNOWN; |
1368 | 1443 | $node->tag = 'unknown'; |
1369 | 1444 | } |
1370 | - if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>'; |
|
1445 | + if ($this->char==='>') { |
|
1446 | + $node->_[HDOM_INFO_TEXT].='>'; |
|
1447 | + } |
|
1371 | 1448 | $this->link_nodes($node, true); |
1372 | 1449 | $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1373 | 1450 | return true; |
@@ -1389,7 +1466,9 @@ discard block |
||
1389 | 1466 | return true; |
1390 | 1467 | } |
1391 | 1468 | |
1392 | - if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>'; |
|
1469 | + if ($this->char==='>') { |
|
1470 | + $node->_[HDOM_INFO_TEXT].='>'; |
|
1471 | + } |
|
1393 | 1472 | $this->link_nodes($node, false); |
1394 | 1473 | $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1395 | 1474 | return true; |
@@ -1455,22 +1534,26 @@ discard block |
||
1455 | 1534 | if ($name!=='/' && $name!=='') { |
1456 | 1535 | $space[1] = $this->copy_skip($this->token_blank); |
1457 | 1536 | $name = $this->restore_noise($name); |
1458 | - if ($this->lowercase) $name = strtolower($name); |
|
1537 | + if ($this->lowercase) { |
|
1538 | + $name = strtolower($name); |
|
1539 | + } |
|
1459 | 1540 | if ($this->char==='=') { |
1460 | 1541 | $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1461 | 1542 | $this->parse_attr($node, $name, $space); |
1462 | - } |
|
1463 | - else { |
|
1543 | + } else { |
|
1464 | 1544 | //no value attr: nowrap, checked selected... |
1465 | 1545 | $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO; |
1466 | 1546 | $node->attr[$name] = true; |
1467 | - if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev |
|
1547 | + if ($this->char!='>') { |
|
1548 | + $this->char = $this->doc[--$this->pos]; |
|
1549 | + } |
|
1550 | + // prev |
|
1468 | 1551 | } |
1469 | 1552 | $node->_[HDOM_INFO_SPACE][] = $space; |
1470 | 1553 | $space = array($this->copy_skip($this->token_blank), '', ''); |
1554 | + } else { |
|
1555 | + break; |
|
1471 | 1556 | } |
1472 | - else |
|
1473 | - break; |
|
1474 | 1557 | } while ($this->char!=='>' && $this->char!=='/'); |
1475 | 1558 | |
1476 | 1559 | $this->link_nodes($node, true); |
@@ -1481,11 +1564,12 @@ discard block |
||
1481 | 1564 | { |
1482 | 1565 | $node->_[HDOM_INFO_ENDSPACE] .= '/'; |
1483 | 1566 | $node->_[HDOM_INFO_END] = 0; |
1484 | - } |
|
1485 | - else |
|
1567 | + } else |
|
1486 | 1568 | { |
1487 | 1569 | // reset parent |
1488 | - if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node; |
|
1570 | + if (!isset($this->self_closing_tags[strtolower($node->tag)])) { |
|
1571 | + $this->parent = $node; |
|
1572 | + } |
|
1489 | 1573 | } |
1490 | 1574 | $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1491 | 1575 | |
@@ -1571,7 +1655,9 @@ discard block |
||
1571 | 1655 | $len = strspn($this->doc, $chars, $pos); |
1572 | 1656 | $this->pos += $len; |
1573 | 1657 | $this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next |
1574 | - if ($len===0) return ''; |
|
1658 | + if ($len===0) { |
|
1659 | + return ''; |
|
1660 | + } |
|
1575 | 1661 | return substr($this->doc, $pos, $len); |
1576 | 1662 | } |
1577 | 1663 | |
@@ -1586,7 +1672,9 @@ discard block |
||
1586 | 1672 | |
1587 | 1673 | protected function copy_until_char($char) |
1588 | 1674 | { |
1589 | - if ($this->char===null) return ''; |
|
1675 | + if ($this->char===null) { |
|
1676 | + return ''; |
|
1677 | + } |
|
1590 | 1678 | |
1591 | 1679 | if (($pos = strpos($this->doc, $char, $this->pos))===false) { |
1592 | 1680 | $ret = substr($this->doc, $this->pos, $this->size-$this->pos); |
@@ -1595,7 +1683,9 @@ discard block |
||
1595 | 1683 | return $ret; |
1596 | 1684 | } |
1597 | 1685 | |
1598 | - if ($pos===$this->pos) return ''; |
|
1686 | + if ($pos===$this->pos) { |
|
1687 | + return ''; |
|
1688 | + } |
|
1599 | 1689 | $pos_old = $this->pos; |
1600 | 1690 | $this->char = $this->doc[$pos]; |
1601 | 1691 | $this->pos = $pos; |
@@ -1604,7 +1694,9 @@ discard block |
||
1604 | 1694 | |
1605 | 1695 | protected function copy_until_char_escape($char) |
1606 | 1696 | { |
1607 | - if ($this->char===null) return ''; |
|
1697 | + if ($this->char===null) { |
|
1698 | + return ''; |
|
1699 | + } |
|
1608 | 1700 | |
1609 | 1701 | $start = $this->pos; |
1610 | 1702 | while (1) |
@@ -1617,7 +1709,9 @@ discard block |
||
1617 | 1709 | return $ret; |
1618 | 1710 | } |
1619 | 1711 | |
1620 | - if ($pos===$this->pos) return ''; |
|
1712 | + if ($pos===$this->pos) { |
|
1713 | + return ''; |
|
1714 | + } |
|
1621 | 1715 | |
1622 | 1716 | if ($this->doc[$pos-1]==='\\') { |
1623 | 1717 | $start = $pos+1; |
@@ -1674,14 +1768,12 @@ discard block |
||
1674 | 1768 | if (isset($this->noise[$key])) |
1675 | 1769 | { |
1676 | 1770 | $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+16); |
1677 | - } |
|
1678 | - else |
|
1771 | + } else |
|
1679 | 1772 | { |
1680 | 1773 | // do this to prevent an infinite loop. |
1681 | 1774 | $text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key . substr($text, $pos+16); |
1682 | 1775 | } |
1683 | - } |
|
1684 | - else |
|
1776 | + } else |
|
1685 | 1777 | { |
1686 | 1778 | // There is no valid key being given back to us... We must get rid of the ___noise___ or we will have a problem. |
1687 | 1779 | $text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY' . substr($text, $pos+11); |
@@ -122,6 +122,9 @@ |
||
122 | 122 | return self::array_random( empty( $lang ) ? self::$languages : $lang ); |
123 | 123 | } |
124 | 124 | |
125 | + /** |
|
126 | + * @param string $os |
|
127 | + */ |
|
125 | 128 | private static function get_processor( $os ) |
126 | 129 | { |
127 | 130 | return self::array_random( self::$processors[$os] ); |
@@ -8,347 +8,347 @@ |
||
8 | 8 | |
9 | 9 | class UAgent |
10 | 10 | { |
11 | - // General token that says the browser is Mozilla compatible, |
|
12 | - // and is common to almost every browser today. |
|
13 | - const MOZILLA = 'Mozilla/5.0 '; |
|
14 | - |
|
15 | - /** |
|
16 | - * Processors by Arch. |
|
17 | - */ |
|
18 | - public static $processors = array( |
|
19 | - 'lin' => array( 'i686', 'x86_64' ), |
|
20 | - 'mac' => array( 'Intel', 'PPC', 'U; Intel', 'U; PPC' ), |
|
21 | - 'win' => array( 'foo' ) |
|
22 | - ); |
|
23 | - |
|
24 | - /** |
|
25 | - * Browsers |
|
26 | - * |
|
27 | - * Weighting is based on market share to determine frequency. |
|
28 | - */ |
|
29 | - public static $browsers = array( |
|
30 | - 34 => array( |
|
31 | - 89 => array( 'chrome', 'win' ), |
|
32 | - 9 => array( 'chrome', 'mac' ), |
|
33 | - 2 => array( 'chrome', 'lin' ) |
|
34 | - ), |
|
35 | - 32 => array( |
|
36 | - 100 => array( 'iexplorer', 'win' ) |
|
37 | - ), |
|
38 | - 25 => array( |
|
39 | - 83 => array( 'firefox', 'win' ), |
|
40 | - 16 => array( 'firefox', 'mac' ), |
|
41 | - 1 => array( 'firefox', 'lin' ) |
|
42 | - ), |
|
43 | - 7 => array( |
|
44 | - 95 => array( 'safari', 'mac' ), |
|
45 | - 4 => array( 'safari', 'win' ), |
|
46 | - 1 => array( 'safari', 'lin' ) |
|
47 | - ), |
|
48 | - 2 => array( |
|
49 | - 91 => array( 'opera', 'win' ), |
|
50 | - 6 => array( 'opera', 'lin' ), |
|
51 | - 3 => array( 'opera', 'mac' ) |
|
52 | - ) |
|
53 | - ); |
|
54 | - |
|
55 | - /** |
|
56 | - * List of Lanuge Culture Codes (ISO 639-1) |
|
57 | - * |
|
58 | - * @see: http://msdn.microsoft.com/en-gb/library/ee825488(v=cs.20).aspx |
|
59 | - */ |
|
60 | - public static $languages = array( |
|
61 | - 'af-ZA', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', |
|
62 | - 'ar-LY', 'ar-MA', 'ar-OM', 'ar-QA', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'be-BY', |
|
63 | - 'bg-BG', 'ca-ES', 'cs-CZ', 'Cy-az-AZ', 'Cy-sr-SP', 'Cy-uz-UZ', 'da-DK', 'de-AT', |
|
64 | - 'de-CH', 'de-DE', 'de-LI', 'de-LU', 'div-MV', 'el-GR', 'en-AU', 'en-BZ', 'en-CA', |
|
65 | - 'en-CB', 'en-GB', 'en-IE', 'en-JM', 'en-NZ', 'en-PH', 'en-TT', 'en-US', 'en-ZA', |
|
66 | - 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-ES', |
|
67 | - 'es-GT', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PR', 'es-PY', 'es-SV', |
|
68 | - 'es-UY', 'es-VE', 'et-EE', 'eu-ES', 'fa-IR', 'fi-FI', 'fo-FO', 'fr-BE', 'fr-CA', |
|
69 | - 'fr-CH', 'fr-FR', 'fr-LU', 'fr-MC', 'gl-ES', 'gu-IN', 'he-IL', 'hi-IN', 'hr-HR', |
|
70 | - 'hu-HU', 'hy-AM', 'id-ID', 'is-IS', 'it-CH', 'it-IT', 'ja-JP', 'ka-GE', 'kk-KZ', |
|
71 | - 'kn-IN', 'kok-IN', 'ko-KR', 'ky-KZ', 'Lt-az-AZ', 'lt-LT', 'Lt-sr-SP', 'Lt-uz-UZ', |
|
72 | - 'lv-LV', 'mk-MK', 'mn-MN', 'mr-IN', 'ms-BN', 'ms-MY', 'nb-NO', 'nl-BE', 'nl-NL', |
|
73 | - 'nn-NO', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'ru-RU', 'sa-IN', 'sk-SK', |
|
74 | - 'sl-SI', 'sq-AL', 'sv-FI', 'sv-SE', 'sw-KE', 'syr-SY', 'ta-IN', 'te-IN', 'th-TH', |
|
75 | - 'tr-TR', 'tt-RU', 'uk-UA', 'ur-PK', 'vi-VN', 'zh-CHS', 'zh-CHT', 'zh-CN', 'zh-HK', |
|
76 | - 'zh-MO', 'zh-SG', 'zh-TW', |
|
77 | - ); |
|
78 | - |
|
79 | - /** |
|
80 | - * Generate Device Platform |
|
81 | - * |
|
82 | - * Uses a random result with a weighting related to frequencies. |
|
83 | - */ |
|
84 | - public static function generate_platform() |
|
85 | - { |
|
86 | - $rand = mt_rand( 1, 100 ); |
|
87 | - $sum = 0; |
|
88 | - |
|
89 | - foreach ( self::$browsers as $share => $freq_os ) |
|
90 | - { |
|
91 | - $sum += $share; |
|
92 | - |
|
93 | - if ( $rand <= $sum ) |
|
94 | - { |
|
95 | - $rand = mt_rand( 1, 100 ); |
|
96 | - $sum = 0; |
|
97 | - |
|
98 | - foreach ( $freq_os as $share => $choice ) |
|
99 | - { |
|
100 | - $sum += $share; |
|
101 | - |
|
102 | - if ( $rand <= $sum ) |
|
103 | - { |
|
104 | - return $choice; |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - throw new Exception( 'Sum of $browsers frequency is not 100.' ); |
|
111 | - } |
|
112 | - |
|
113 | - private static function array_random( $array ) |
|
114 | - { |
|
115 | - $i = array_rand( $array, 1 ); |
|
116 | - |
|
117 | - return $array[$i]; |
|
118 | - } |
|
119 | - |
|
120 | - private static function get_language( $lang = array() ) |
|
121 | - { |
|
122 | - return self::array_random( empty( $lang ) ? self::$languages : $lang ); |
|
123 | - } |
|
124 | - |
|
125 | - private static function get_processor( $os ) |
|
126 | - { |
|
127 | - return self::array_random( self::$processors[$os] ); |
|
128 | - } |
|
129 | - |
|
130 | - private static function get_version_nt() |
|
131 | - { |
|
132 | - // Win2k (5.0) to Win 7 (6.1). |
|
133 | - return mt_rand( 5, 6 ) . '.' . mt_rand( 0, 1 ); |
|
134 | - } |
|
135 | - |
|
136 | - private static function get_version_osx() |
|
137 | - { |
|
138 | - return '10_' . mt_rand( 5, 7 ) . '_' . mt_rand( 0, 9 ); |
|
139 | - } |
|
140 | - |
|
141 | - private static function get_version_webkit() |
|
142 | - { |
|
143 | - return mt_rand( 531, 536 ) . mt_rand( 0, 2 ); |
|
144 | - } |
|
145 | - |
|
146 | - private static function get_verison_chrome() |
|
147 | - { |
|
148 | - return mt_rand( 13, 15 ) . '.0.' . mt_rand( 800, 899 ) . '.0'; |
|
149 | - } |
|
150 | - |
|
151 | - private static function get_version_gecko() |
|
152 | - { |
|
153 | - return mt_rand( 17, 31 ) . '.0'; |
|
154 | - } |
|
155 | - |
|
156 | - private static function get_version_ie() |
|
157 | - { |
|
158 | - return mt_rand( 7, 9 ) . '.0'; |
|
159 | - } |
|
160 | - |
|
161 | - private static function get_version_trident() |
|
162 | - { |
|
163 | - // IE8 (4.0) to IE11 (7.0). |
|
164 | - return mt_rand( 4, 7 ) . '.0'; |
|
165 | - } |
|
166 | - |
|
167 | - private static function get_version_net() |
|
168 | - { |
|
169 | - // generic .NET Framework common language run time (CLR) version numbers. |
|
170 | - $frameworks = array( |
|
171 | - '2.0.50727', |
|
172 | - '3.0.4506', |
|
173 | - '3.5.30729', |
|
174 | - ); |
|
175 | - |
|
176 | - $rev = '.' . mt_rand( 26, 648 ); |
|
177 | - |
|
178 | - return self::array_random( $frameworks ) . $rev; |
|
179 | - } |
|
180 | - |
|
181 | - private static function get_version_safari() |
|
182 | - { |
|
183 | - if ( mt_rand( 0, 1 ) == 0 ) |
|
184 | - { |
|
185 | - $ver = mt_rand( 4, 5 ) . '.' . mt_rand( 0, 1 ); |
|
186 | - } |
|
187 | - else |
|
188 | - { |
|
189 | - $ver = mt_rand( 4, 5 ) . '.0.' . mt_rand( 1, 5 ); |
|
190 | - } |
|
191 | - |
|
192 | - return $ver; |
|
193 | - } |
|
194 | - |
|
195 | - private static function get_version_opera() |
|
196 | - { |
|
197 | - return mt_rand( 15, 19 ) . '.0.' . mt_rand( 1147, 1284 ) . mt_rand( 49, 100 ); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Opera |
|
202 | - * |
|
203 | - * @see: http://dev.opera.com/blog/opera-user-agent-strings-opera-15-and-beyond/ |
|
204 | - */ |
|
205 | - public static function opera( $arch ) |
|
206 | - { |
|
207 | - $opera = ' OPR/' . self::get_version_opera(); |
|
208 | - |
|
209 | - // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
210 | - $engine = self::get_version_webkit(); |
|
211 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
212 | - $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
213 | - $safari = ' Safari/' . $engine; |
|
214 | - |
|
215 | - switch ( $arch ) |
|
216 | - { |
|
217 | - case 'lin': |
|
218 | - return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari . $opera; |
|
219 | - case 'mac': |
|
220 | - $osx = self::get_version_osx(); |
|
221 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari . $opera; |
|
222 | - case 'win': |
|
223 | - // fall through. |
|
224 | - default: |
|
225 | - $nt = self::get_version_nt(); |
|
226 | - return '(Windows NT ' . $nt . '; WOW64) ' . $webkit . $chrome . $safari . $opera; |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Safari |
|
232 | - * |
|
233 | - */ |
|
234 | - public static function safari( $arch ) |
|
235 | - { |
|
236 | - $version = ' Version/' . self::get_version_safari(); |
|
237 | - |
|
238 | - // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
239 | - $engine = self::get_version_webkit(); |
|
240 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
241 | - $safari = ' Safari/' . $engine; |
|
242 | - |
|
243 | - switch ( $arch ) |
|
244 | - { |
|
245 | - case 'mac': |
|
246 | - $osx = self::get_version_osx(); |
|
247 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . '; {lang})' . $webkit . $version . $safari; |
|
248 | - case 'win': |
|
249 | - // fall through. |
|
250 | - default: |
|
251 | - $nt = self::get_version_nt(); |
|
252 | - return '(Windows; U; Windows NT ' . $nt . ')' . $webkit . $version . $safari; |
|
253 | - } |
|
254 | - |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Internet Explorer |
|
259 | - * |
|
260 | - * @see: http://msdn.microsoft.com/en-gb/library/ms537503(v=vs.85).aspx |
|
261 | - */ |
|
262 | - public static function iexplorer( $arch ) |
|
263 | - { |
|
264 | - $nt = self::get_version_nt(); |
|
265 | - $ie = self::get_version_ie(); |
|
266 | - $trident = self::get_version_trident(); |
|
267 | - $net = self::get_version_net(); |
|
268 | - |
|
269 | - return '(compatible' |
|
270 | - . '; MSIE ' . $ie |
|
271 | - . '; Windows NT ' . $nt |
|
272 | - . '; WOW64' // A 32-bit version of Internet Explorer is running on a 64-bit processor. |
|
273 | - . '; Trident/' . $trident |
|
274 | - . '; .NET CLR ' . $net |
|
275 | - . ')'; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Firefox User-Agent |
|
280 | - * |
|
281 | - * @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
|
282 | - */ |
|
283 | - public static function firefox( $arch ) |
|
284 | - { |
|
285 | - // The release version of Gecko. |
|
286 | - $gecko = self::get_version_gecko(); |
|
287 | - |
|
288 | - // On desktop, the gecko trail is fixed. |
|
289 | - $trail = '20100101'; |
|
290 | - |
|
291 | - $release = 'rv:' . $gecko; |
|
292 | - $version = 'Gecko/' . $trail . ' Firefox/' . $gecko; |
|
293 | - |
|
294 | - switch ( $arch ) |
|
295 | - { |
|
296 | - case 'lin': |
|
297 | - return '(X11; Linux {proc}; ' . $release . ') ' . $version; |
|
298 | - case 'mac': |
|
299 | - $osx = self::get_version_osx(); |
|
300 | - return '(Macintosh; {proc} Mac OS X ' . $osx . '; ' . $release . ') ' . $version; |
|
301 | - case 'win': |
|
302 | - // fall through. |
|
303 | - default: |
|
304 | - $nt = self::get_version_nt(); |
|
305 | - return '(Windows NT ' . $nt . '; {lang}; ' . $release . ') ' . $version; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - public static function chrome( $arch ) |
|
310 | - { |
|
311 | - $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
312 | - |
|
313 | - // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
314 | - $engine = self::get_version_webkit(); |
|
315 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
316 | - $safari = ' Safari/' . $engine; |
|
317 | - |
|
318 | - switch ( $arch ) |
|
319 | - { |
|
320 | - case 'lin': |
|
321 | - return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari; |
|
322 | - case 'mac': |
|
323 | - $osx = self::get_version_osx(); |
|
324 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari; |
|
325 | - case 'win': |
|
326 | - // fall through. |
|
327 | - default: |
|
328 | - $nt = self::get_version_nt(); |
|
329 | - return '(Windows NT ' . $nt . ') ' . $webkit . $chrome . $safari; |
|
330 | - } |
|
331 | - } |
|
332 | - |
|
333 | - public static function random( $lang = array( 'en-US' ) ) |
|
334 | - { |
|
335 | - list( $browser, $os ) = self::generate_platform(); |
|
336 | - |
|
337 | - return self::generate( $browser, $os, $lang ); |
|
338 | - } |
|
339 | - |
|
340 | - public static function generate( $browser = 'chrome', $os = 'win', $lang = array( 'en-US' ) ) |
|
341 | - { |
|
342 | - $ua = self::MOZILLA . call_user_func( 'UAgent::' . $browser, $os ); |
|
343 | - |
|
344 | - $tags = array( |
|
345 | - '{proc}' => self::get_processor( $os ), |
|
346 | - '{lang}' => self::get_language( $lang ), |
|
347 | - ); |
|
348 | - |
|
349 | - $ua = str_replace( array_keys( $tags ), array_values( $tags ), $ua ); |
|
350 | - |
|
351 | - return $ua; |
|
352 | - } |
|
11 | + // General token that says the browser is Mozilla compatible, |
|
12 | + // and is common to almost every browser today. |
|
13 | + const MOZILLA = 'Mozilla/5.0 '; |
|
14 | + |
|
15 | + /** |
|
16 | + * Processors by Arch. |
|
17 | + */ |
|
18 | + public static $processors = array( |
|
19 | + 'lin' => array( 'i686', 'x86_64' ), |
|
20 | + 'mac' => array( 'Intel', 'PPC', 'U; Intel', 'U; PPC' ), |
|
21 | + 'win' => array( 'foo' ) |
|
22 | + ); |
|
23 | + |
|
24 | + /** |
|
25 | + * Browsers |
|
26 | + * |
|
27 | + * Weighting is based on market share to determine frequency. |
|
28 | + */ |
|
29 | + public static $browsers = array( |
|
30 | + 34 => array( |
|
31 | + 89 => array( 'chrome', 'win' ), |
|
32 | + 9 => array( 'chrome', 'mac' ), |
|
33 | + 2 => array( 'chrome', 'lin' ) |
|
34 | + ), |
|
35 | + 32 => array( |
|
36 | + 100 => array( 'iexplorer', 'win' ) |
|
37 | + ), |
|
38 | + 25 => array( |
|
39 | + 83 => array( 'firefox', 'win' ), |
|
40 | + 16 => array( 'firefox', 'mac' ), |
|
41 | + 1 => array( 'firefox', 'lin' ) |
|
42 | + ), |
|
43 | + 7 => array( |
|
44 | + 95 => array( 'safari', 'mac' ), |
|
45 | + 4 => array( 'safari', 'win' ), |
|
46 | + 1 => array( 'safari', 'lin' ) |
|
47 | + ), |
|
48 | + 2 => array( |
|
49 | + 91 => array( 'opera', 'win' ), |
|
50 | + 6 => array( 'opera', 'lin' ), |
|
51 | + 3 => array( 'opera', 'mac' ) |
|
52 | + ) |
|
53 | + ); |
|
54 | + |
|
55 | + /** |
|
56 | + * List of Lanuge Culture Codes (ISO 639-1) |
|
57 | + * |
|
58 | + * @see: http://msdn.microsoft.com/en-gb/library/ee825488(v=cs.20).aspx |
|
59 | + */ |
|
60 | + public static $languages = array( |
|
61 | + 'af-ZA', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', |
|
62 | + 'ar-LY', 'ar-MA', 'ar-OM', 'ar-QA', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'be-BY', |
|
63 | + 'bg-BG', 'ca-ES', 'cs-CZ', 'Cy-az-AZ', 'Cy-sr-SP', 'Cy-uz-UZ', 'da-DK', 'de-AT', |
|
64 | + 'de-CH', 'de-DE', 'de-LI', 'de-LU', 'div-MV', 'el-GR', 'en-AU', 'en-BZ', 'en-CA', |
|
65 | + 'en-CB', 'en-GB', 'en-IE', 'en-JM', 'en-NZ', 'en-PH', 'en-TT', 'en-US', 'en-ZA', |
|
66 | + 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-ES', |
|
67 | + 'es-GT', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PR', 'es-PY', 'es-SV', |
|
68 | + 'es-UY', 'es-VE', 'et-EE', 'eu-ES', 'fa-IR', 'fi-FI', 'fo-FO', 'fr-BE', 'fr-CA', |
|
69 | + 'fr-CH', 'fr-FR', 'fr-LU', 'fr-MC', 'gl-ES', 'gu-IN', 'he-IL', 'hi-IN', 'hr-HR', |
|
70 | + 'hu-HU', 'hy-AM', 'id-ID', 'is-IS', 'it-CH', 'it-IT', 'ja-JP', 'ka-GE', 'kk-KZ', |
|
71 | + 'kn-IN', 'kok-IN', 'ko-KR', 'ky-KZ', 'Lt-az-AZ', 'lt-LT', 'Lt-sr-SP', 'Lt-uz-UZ', |
|
72 | + 'lv-LV', 'mk-MK', 'mn-MN', 'mr-IN', 'ms-BN', 'ms-MY', 'nb-NO', 'nl-BE', 'nl-NL', |
|
73 | + 'nn-NO', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'ru-RU', 'sa-IN', 'sk-SK', |
|
74 | + 'sl-SI', 'sq-AL', 'sv-FI', 'sv-SE', 'sw-KE', 'syr-SY', 'ta-IN', 'te-IN', 'th-TH', |
|
75 | + 'tr-TR', 'tt-RU', 'uk-UA', 'ur-PK', 'vi-VN', 'zh-CHS', 'zh-CHT', 'zh-CN', 'zh-HK', |
|
76 | + 'zh-MO', 'zh-SG', 'zh-TW', |
|
77 | + ); |
|
78 | + |
|
79 | + /** |
|
80 | + * Generate Device Platform |
|
81 | + * |
|
82 | + * Uses a random result with a weighting related to frequencies. |
|
83 | + */ |
|
84 | + public static function generate_platform() |
|
85 | + { |
|
86 | + $rand = mt_rand( 1, 100 ); |
|
87 | + $sum = 0; |
|
88 | + |
|
89 | + foreach ( self::$browsers as $share => $freq_os ) |
|
90 | + { |
|
91 | + $sum += $share; |
|
92 | + |
|
93 | + if ( $rand <= $sum ) |
|
94 | + { |
|
95 | + $rand = mt_rand( 1, 100 ); |
|
96 | + $sum = 0; |
|
97 | + |
|
98 | + foreach ( $freq_os as $share => $choice ) |
|
99 | + { |
|
100 | + $sum += $share; |
|
101 | + |
|
102 | + if ( $rand <= $sum ) |
|
103 | + { |
|
104 | + return $choice; |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + throw new Exception( 'Sum of $browsers frequency is not 100.' ); |
|
111 | + } |
|
112 | + |
|
113 | + private static function array_random( $array ) |
|
114 | + { |
|
115 | + $i = array_rand( $array, 1 ); |
|
116 | + |
|
117 | + return $array[$i]; |
|
118 | + } |
|
119 | + |
|
120 | + private static function get_language( $lang = array() ) |
|
121 | + { |
|
122 | + return self::array_random( empty( $lang ) ? self::$languages : $lang ); |
|
123 | + } |
|
124 | + |
|
125 | + private static function get_processor( $os ) |
|
126 | + { |
|
127 | + return self::array_random( self::$processors[$os] ); |
|
128 | + } |
|
129 | + |
|
130 | + private static function get_version_nt() |
|
131 | + { |
|
132 | + // Win2k (5.0) to Win 7 (6.1). |
|
133 | + return mt_rand( 5, 6 ) . '.' . mt_rand( 0, 1 ); |
|
134 | + } |
|
135 | + |
|
136 | + private static function get_version_osx() |
|
137 | + { |
|
138 | + return '10_' . mt_rand( 5, 7 ) . '_' . mt_rand( 0, 9 ); |
|
139 | + } |
|
140 | + |
|
141 | + private static function get_version_webkit() |
|
142 | + { |
|
143 | + return mt_rand( 531, 536 ) . mt_rand( 0, 2 ); |
|
144 | + } |
|
145 | + |
|
146 | + private static function get_verison_chrome() |
|
147 | + { |
|
148 | + return mt_rand( 13, 15 ) . '.0.' . mt_rand( 800, 899 ) . '.0'; |
|
149 | + } |
|
150 | + |
|
151 | + private static function get_version_gecko() |
|
152 | + { |
|
153 | + return mt_rand( 17, 31 ) . '.0'; |
|
154 | + } |
|
155 | + |
|
156 | + private static function get_version_ie() |
|
157 | + { |
|
158 | + return mt_rand( 7, 9 ) . '.0'; |
|
159 | + } |
|
160 | + |
|
161 | + private static function get_version_trident() |
|
162 | + { |
|
163 | + // IE8 (4.0) to IE11 (7.0). |
|
164 | + return mt_rand( 4, 7 ) . '.0'; |
|
165 | + } |
|
166 | + |
|
167 | + private static function get_version_net() |
|
168 | + { |
|
169 | + // generic .NET Framework common language run time (CLR) version numbers. |
|
170 | + $frameworks = array( |
|
171 | + '2.0.50727', |
|
172 | + '3.0.4506', |
|
173 | + '3.5.30729', |
|
174 | + ); |
|
175 | + |
|
176 | + $rev = '.' . mt_rand( 26, 648 ); |
|
177 | + |
|
178 | + return self::array_random( $frameworks ) . $rev; |
|
179 | + } |
|
180 | + |
|
181 | + private static function get_version_safari() |
|
182 | + { |
|
183 | + if ( mt_rand( 0, 1 ) == 0 ) |
|
184 | + { |
|
185 | + $ver = mt_rand( 4, 5 ) . '.' . mt_rand( 0, 1 ); |
|
186 | + } |
|
187 | + else |
|
188 | + { |
|
189 | + $ver = mt_rand( 4, 5 ) . '.0.' . mt_rand( 1, 5 ); |
|
190 | + } |
|
191 | + |
|
192 | + return $ver; |
|
193 | + } |
|
194 | + |
|
195 | + private static function get_version_opera() |
|
196 | + { |
|
197 | + return mt_rand( 15, 19 ) . '.0.' . mt_rand( 1147, 1284 ) . mt_rand( 49, 100 ); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Opera |
|
202 | + * |
|
203 | + * @see: http://dev.opera.com/blog/opera-user-agent-strings-opera-15-and-beyond/ |
|
204 | + */ |
|
205 | + public static function opera( $arch ) |
|
206 | + { |
|
207 | + $opera = ' OPR/' . self::get_version_opera(); |
|
208 | + |
|
209 | + // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
210 | + $engine = self::get_version_webkit(); |
|
211 | + $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
212 | + $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
213 | + $safari = ' Safari/' . $engine; |
|
214 | + |
|
215 | + switch ( $arch ) |
|
216 | + { |
|
217 | + case 'lin': |
|
218 | + return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari . $opera; |
|
219 | + case 'mac': |
|
220 | + $osx = self::get_version_osx(); |
|
221 | + return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari . $opera; |
|
222 | + case 'win': |
|
223 | + // fall through. |
|
224 | + default: |
|
225 | + $nt = self::get_version_nt(); |
|
226 | + return '(Windows NT ' . $nt . '; WOW64) ' . $webkit . $chrome . $safari . $opera; |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Safari |
|
232 | + * |
|
233 | + */ |
|
234 | + public static function safari( $arch ) |
|
235 | + { |
|
236 | + $version = ' Version/' . self::get_version_safari(); |
|
237 | + |
|
238 | + // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
239 | + $engine = self::get_version_webkit(); |
|
240 | + $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
241 | + $safari = ' Safari/' . $engine; |
|
242 | + |
|
243 | + switch ( $arch ) |
|
244 | + { |
|
245 | + case 'mac': |
|
246 | + $osx = self::get_version_osx(); |
|
247 | + return '(Macintosh; U; {proc} Mac OS X ' . $osx . '; {lang})' . $webkit . $version . $safari; |
|
248 | + case 'win': |
|
249 | + // fall through. |
|
250 | + default: |
|
251 | + $nt = self::get_version_nt(); |
|
252 | + return '(Windows; U; Windows NT ' . $nt . ')' . $webkit . $version . $safari; |
|
253 | + } |
|
254 | + |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Internet Explorer |
|
259 | + * |
|
260 | + * @see: http://msdn.microsoft.com/en-gb/library/ms537503(v=vs.85).aspx |
|
261 | + */ |
|
262 | + public static function iexplorer( $arch ) |
|
263 | + { |
|
264 | + $nt = self::get_version_nt(); |
|
265 | + $ie = self::get_version_ie(); |
|
266 | + $trident = self::get_version_trident(); |
|
267 | + $net = self::get_version_net(); |
|
268 | + |
|
269 | + return '(compatible' |
|
270 | + . '; MSIE ' . $ie |
|
271 | + . '; Windows NT ' . $nt |
|
272 | + . '; WOW64' // A 32-bit version of Internet Explorer is running on a 64-bit processor. |
|
273 | + . '; Trident/' . $trident |
|
274 | + . '; .NET CLR ' . $net |
|
275 | + . ')'; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Firefox User-Agent |
|
280 | + * |
|
281 | + * @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
|
282 | + */ |
|
283 | + public static function firefox( $arch ) |
|
284 | + { |
|
285 | + // The release version of Gecko. |
|
286 | + $gecko = self::get_version_gecko(); |
|
287 | + |
|
288 | + // On desktop, the gecko trail is fixed. |
|
289 | + $trail = '20100101'; |
|
290 | + |
|
291 | + $release = 'rv:' . $gecko; |
|
292 | + $version = 'Gecko/' . $trail . ' Firefox/' . $gecko; |
|
293 | + |
|
294 | + switch ( $arch ) |
|
295 | + { |
|
296 | + case 'lin': |
|
297 | + return '(X11; Linux {proc}; ' . $release . ') ' . $version; |
|
298 | + case 'mac': |
|
299 | + $osx = self::get_version_osx(); |
|
300 | + return '(Macintosh; {proc} Mac OS X ' . $osx . '; ' . $release . ') ' . $version; |
|
301 | + case 'win': |
|
302 | + // fall through. |
|
303 | + default: |
|
304 | + $nt = self::get_version_nt(); |
|
305 | + return '(Windows NT ' . $nt . '; {lang}; ' . $release . ') ' . $version; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + public static function chrome( $arch ) |
|
310 | + { |
|
311 | + $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
312 | + |
|
313 | + // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
|
314 | + $engine = self::get_version_webkit(); |
|
315 | + $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
316 | + $safari = ' Safari/' . $engine; |
|
317 | + |
|
318 | + switch ( $arch ) |
|
319 | + { |
|
320 | + case 'lin': |
|
321 | + return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari; |
|
322 | + case 'mac': |
|
323 | + $osx = self::get_version_osx(); |
|
324 | + return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari; |
|
325 | + case 'win': |
|
326 | + // fall through. |
|
327 | + default: |
|
328 | + $nt = self::get_version_nt(); |
|
329 | + return '(Windows NT ' . $nt . ') ' . $webkit . $chrome . $safari; |
|
330 | + } |
|
331 | + } |
|
332 | + |
|
333 | + public static function random( $lang = array( 'en-US' ) ) |
|
334 | + { |
|
335 | + list( $browser, $os ) = self::generate_platform(); |
|
336 | + |
|
337 | + return self::generate( $browser, $os, $lang ); |
|
338 | + } |
|
339 | + |
|
340 | + public static function generate( $browser = 'chrome', $os = 'win', $lang = array( 'en-US' ) ) |
|
341 | + { |
|
342 | + $ua = self::MOZILLA . call_user_func( 'UAgent::' . $browser, $os ); |
|
343 | + |
|
344 | + $tags = array( |
|
345 | + '{proc}' => self::get_processor( $os ), |
|
346 | + '{lang}' => self::get_language( $lang ), |
|
347 | + ); |
|
348 | + |
|
349 | + $ua = str_replace( array_keys( $tags ), array_values( $tags ), $ua ); |
|
350 | + |
|
351 | + return $ua; |
|
352 | + } |
|
353 | 353 | } |
354 | 354 | ?> |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | * Processors by Arch. |
17 | 17 | */ |
18 | 18 | public static $processors = array( |
19 | - 'lin' => array( 'i686', 'x86_64' ), |
|
20 | - 'mac' => array( 'Intel', 'PPC', 'U; Intel', 'U; PPC' ), |
|
21 | - 'win' => array( 'foo' ) |
|
19 | + 'lin' => array('i686', 'x86_64'), |
|
20 | + 'mac' => array('Intel', 'PPC', 'U; Intel', 'U; PPC'), |
|
21 | + 'win' => array('foo') |
|
22 | 22 | ); |
23 | 23 | |
24 | 24 | /** |
@@ -28,27 +28,27 @@ discard block |
||
28 | 28 | */ |
29 | 29 | public static $browsers = array( |
30 | 30 | 34 => array( |
31 | - 89 => array( 'chrome', 'win' ), |
|
32 | - 9 => array( 'chrome', 'mac' ), |
|
33 | - 2 => array( 'chrome', 'lin' ) |
|
31 | + 89 => array('chrome', 'win'), |
|
32 | + 9 => array('chrome', 'mac'), |
|
33 | + 2 => array('chrome', 'lin') |
|
34 | 34 | ), |
35 | 35 | 32 => array( |
36 | - 100 => array( 'iexplorer', 'win' ) |
|
36 | + 100 => array('iexplorer', 'win') |
|
37 | 37 | ), |
38 | 38 | 25 => array( |
39 | - 83 => array( 'firefox', 'win' ), |
|
40 | - 16 => array( 'firefox', 'mac' ), |
|
41 | - 1 => array( 'firefox', 'lin' ) |
|
39 | + 83 => array('firefox', 'win'), |
|
40 | + 16 => array('firefox', 'mac'), |
|
41 | + 1 => array('firefox', 'lin') |
|
42 | 42 | ), |
43 | 43 | 7 => array( |
44 | - 95 => array( 'safari', 'mac' ), |
|
45 | - 4 => array( 'safari', 'win' ), |
|
46 | - 1 => array( 'safari', 'lin' ) |
|
44 | + 95 => array('safari', 'mac'), |
|
45 | + 4 => array('safari', 'win'), |
|
46 | + 1 => array('safari', 'lin') |
|
47 | 47 | ), |
48 | 48 | 2 => array( |
49 | - 91 => array( 'opera', 'win' ), |
|
50 | - 6 => array( 'opera', 'lin' ), |
|
51 | - 3 => array( 'opera', 'mac' ) |
|
49 | + 91 => array('opera', 'win'), |
|
50 | + 6 => array('opera', 'lin'), |
|
51 | + 3 => array('opera', 'mac') |
|
52 | 52 | ) |
53 | 53 | ); |
54 | 54 | |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | 'bg-BG', 'ca-ES', 'cs-CZ', 'Cy-az-AZ', 'Cy-sr-SP', 'Cy-uz-UZ', 'da-DK', 'de-AT', |
64 | 64 | 'de-CH', 'de-DE', 'de-LI', 'de-LU', 'div-MV', 'el-GR', 'en-AU', 'en-BZ', 'en-CA', |
65 | 65 | 'en-CB', 'en-GB', 'en-IE', 'en-JM', 'en-NZ', 'en-PH', 'en-TT', 'en-US', 'en-ZA', |
66 | - 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-ES', |
|
66 | + 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-ES', |
|
67 | 67 | 'es-GT', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PR', 'es-PY', 'es-SV', |
68 | 68 | 'es-UY', 'es-VE', 'et-EE', 'eu-ES', 'fa-IR', 'fi-FI', 'fo-FO', 'fr-BE', 'fr-CA', |
69 | 69 | 'fr-CH', 'fr-FR', 'fr-LU', 'fr-MC', 'gl-ES', 'gu-IN', 'he-IL', 'hi-IN', 'hr-HR', |
@@ -83,23 +83,23 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public static function generate_platform() |
85 | 85 | { |
86 | - $rand = mt_rand( 1, 100 ); |
|
86 | + $rand = mt_rand(1, 100); |
|
87 | 87 | $sum = 0; |
88 | 88 | |
89 | - foreach ( self::$browsers as $share => $freq_os ) |
|
89 | + foreach (self::$browsers as $share => $freq_os) |
|
90 | 90 | { |
91 | 91 | $sum += $share; |
92 | 92 | |
93 | - if ( $rand <= $sum ) |
|
93 | + if ($rand <= $sum) |
|
94 | 94 | { |
95 | - $rand = mt_rand( 1, 100 ); |
|
95 | + $rand = mt_rand(1, 100); |
|
96 | 96 | $sum = 0; |
97 | 97 | |
98 | - foreach ( $freq_os as $share => $choice ) |
|
98 | + foreach ($freq_os as $share => $choice) |
|
99 | 99 | { |
100 | 100 | $sum += $share; |
101 | 101 | |
102 | - if ( $rand <= $sum ) |
|
102 | + if ($rand <= $sum) |
|
103 | 103 | { |
104 | 104 | return $choice; |
105 | 105 | } |
@@ -107,61 +107,61 @@ discard block |
||
107 | 107 | } |
108 | 108 | } |
109 | 109 | |
110 | - throw new Exception( 'Sum of $browsers frequency is not 100.' ); |
|
110 | + throw new Exception('Sum of $browsers frequency is not 100.'); |
|
111 | 111 | } |
112 | 112 | |
113 | - private static function array_random( $array ) |
|
113 | + private static function array_random($array) |
|
114 | 114 | { |
115 | - $i = array_rand( $array, 1 ); |
|
115 | + $i = array_rand($array, 1); |
|
116 | 116 | |
117 | 117 | return $array[$i]; |
118 | 118 | } |
119 | 119 | |
120 | - private static function get_language( $lang = array() ) |
|
120 | + private static function get_language($lang = array()) |
|
121 | 121 | { |
122 | - return self::array_random( empty( $lang ) ? self::$languages : $lang ); |
|
122 | + return self::array_random(empty($lang) ? self::$languages : $lang); |
|
123 | 123 | } |
124 | 124 | |
125 | - private static function get_processor( $os ) |
|
125 | + private static function get_processor($os) |
|
126 | 126 | { |
127 | - return self::array_random( self::$processors[$os] ); |
|
127 | + return self::array_random(self::$processors[$os]); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | private static function get_version_nt() |
131 | 131 | { |
132 | 132 | // Win2k (5.0) to Win 7 (6.1). |
133 | - return mt_rand( 5, 6 ) . '.' . mt_rand( 0, 1 ); |
|
133 | + return mt_rand(5, 6).'.'.mt_rand(0, 1); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | private static function get_version_osx() |
137 | 137 | { |
138 | - return '10_' . mt_rand( 5, 7 ) . '_' . mt_rand( 0, 9 ); |
|
138 | + return '10_'.mt_rand(5, 7).'_'.mt_rand(0, 9); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | private static function get_version_webkit() |
142 | 142 | { |
143 | - return mt_rand( 531, 536 ) . mt_rand( 0, 2 ); |
|
143 | + return mt_rand(531, 536).mt_rand(0, 2); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | private static function get_verison_chrome() |
147 | 147 | { |
148 | - return mt_rand( 13, 15 ) . '.0.' . mt_rand( 800, 899 ) . '.0'; |
|
148 | + return mt_rand(13, 15).'.0.'.mt_rand(800, 899).'.0'; |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | private static function get_version_gecko() |
152 | 152 | { |
153 | - return mt_rand( 17, 31 ) . '.0'; |
|
153 | + return mt_rand(17, 31).'.0'; |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | private static function get_version_ie() |
157 | 157 | { |
158 | - return mt_rand( 7, 9 ) . '.0'; |
|
158 | + return mt_rand(7, 9).'.0'; |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | private static function get_version_trident() |
162 | 162 | { |
163 | 163 | // IE8 (4.0) to IE11 (7.0). |
164 | - return mt_rand( 4, 7 ) . '.0'; |
|
164 | + return mt_rand(4, 7).'.0'; |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | private static function get_version_net() |
@@ -173,20 +173,20 @@ discard block |
||
173 | 173 | '3.5.30729', |
174 | 174 | ); |
175 | 175 | |
176 | - $rev = '.' . mt_rand( 26, 648 ); |
|
176 | + $rev = '.'.mt_rand(26, 648); |
|
177 | 177 | |
178 | - return self::array_random( $frameworks ) . $rev; |
|
178 | + return self::array_random($frameworks).$rev; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | private static function get_version_safari() |
182 | 182 | { |
183 | - if ( mt_rand( 0, 1 ) == 0 ) |
|
183 | + if (mt_rand(0, 1) == 0) |
|
184 | 184 | { |
185 | - $ver = mt_rand( 4, 5 ) . '.' . mt_rand( 0, 1 ); |
|
185 | + $ver = mt_rand(4, 5).'.'.mt_rand(0, 1); |
|
186 | 186 | } |
187 | 187 | else |
188 | 188 | { |
189 | - $ver = mt_rand( 4, 5 ) . '.0.' . mt_rand( 1, 5 ); |
|
189 | + $ver = mt_rand(4, 5).'.0.'.mt_rand(1, 5); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | return $ver; |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | |
195 | 195 | private static function get_version_opera() |
196 | 196 | { |
197 | - return mt_rand( 15, 19 ) . '.0.' . mt_rand( 1147, 1284 ) . mt_rand( 49, 100 ); |
|
197 | + return mt_rand(15, 19).'.0.'.mt_rand(1147, 1284).mt_rand(49, 100); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | /** |
@@ -202,28 +202,28 @@ discard block |
||
202 | 202 | * |
203 | 203 | * @see: http://dev.opera.com/blog/opera-user-agent-strings-opera-15-and-beyond/ |
204 | 204 | */ |
205 | - public static function opera( $arch ) |
|
205 | + public static function opera($arch) |
|
206 | 206 | { |
207 | - $opera = ' OPR/' . self::get_version_opera(); |
|
207 | + $opera = ' OPR/'.self::get_version_opera(); |
|
208 | 208 | |
209 | 209 | // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
210 | 210 | $engine = self::get_version_webkit(); |
211 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
212 | - $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
213 | - $safari = ' Safari/' . $engine; |
|
211 | + $webkit = ' AppleWebKit/'.$engine.' (KHTML, like Gecko)'; |
|
212 | + $chrome = ' Chrome/'.self::get_verison_chrome(); |
|
213 | + $safari = ' Safari/'.$engine; |
|
214 | 214 | |
215 | - switch ( $arch ) |
|
215 | + switch ($arch) |
|
216 | 216 | { |
217 | 217 | case 'lin': |
218 | - return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari . $opera; |
|
218 | + return '(X11; Linux {proc}) '.$webkit.$chrome.$safari.$opera; |
|
219 | 219 | case 'mac': |
220 | 220 | $osx = self::get_version_osx(); |
221 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari . $opera; |
|
221 | + return '(Macintosh; U; {proc} Mac OS X '.$osx.')'.$webkit.$chrome.$safari.$opera; |
|
222 | 222 | case 'win': |
223 | 223 | // fall through. |
224 | 224 | default: |
225 | 225 | $nt = self::get_version_nt(); |
226 | - return '(Windows NT ' . $nt . '; WOW64) ' . $webkit . $chrome . $safari . $opera; |
|
226 | + return '(Windows NT '.$nt.'; WOW64) '.$webkit.$chrome.$safari.$opera; |
|
227 | 227 | } |
228 | 228 | } |
229 | 229 | |
@@ -231,25 +231,25 @@ discard block |
||
231 | 231 | * Safari |
232 | 232 | * |
233 | 233 | */ |
234 | - public static function safari( $arch ) |
|
234 | + public static function safari($arch) |
|
235 | 235 | { |
236 | - $version = ' Version/' . self::get_version_safari(); |
|
236 | + $version = ' Version/'.self::get_version_safari(); |
|
237 | 237 | |
238 | 238 | // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
239 | 239 | $engine = self::get_version_webkit(); |
240 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
241 | - $safari = ' Safari/' . $engine; |
|
240 | + $webkit = ' AppleWebKit/'.$engine.' (KHTML, like Gecko)'; |
|
241 | + $safari = ' Safari/'.$engine; |
|
242 | 242 | |
243 | - switch ( $arch ) |
|
243 | + switch ($arch) |
|
244 | 244 | { |
245 | 245 | case 'mac': |
246 | 246 | $osx = self::get_version_osx(); |
247 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . '; {lang})' . $webkit . $version . $safari; |
|
247 | + return '(Macintosh; U; {proc} Mac OS X '.$osx.'; {lang})'.$webkit.$version.$safari; |
|
248 | 248 | case 'win': |
249 | 249 | // fall through. |
250 | 250 | default: |
251 | 251 | $nt = self::get_version_nt(); |
252 | - return '(Windows; U; Windows NT ' . $nt . ')' . $webkit . $version . $safari; |
|
252 | + return '(Windows; U; Windows NT '.$nt.')'.$webkit.$version.$safari; |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | } |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | * |
260 | 260 | * @see: http://msdn.microsoft.com/en-gb/library/ms537503(v=vs.85).aspx |
261 | 261 | */ |
262 | - public static function iexplorer( $arch ) |
|
262 | + public static function iexplorer($arch) |
|
263 | 263 | { |
264 | 264 | $nt = self::get_version_nt(); |
265 | 265 | $ie = self::get_version_ie(); |
@@ -267,11 +267,11 @@ discard block |
||
267 | 267 | $net = self::get_version_net(); |
268 | 268 | |
269 | 269 | return '(compatible' |
270 | - . '; MSIE ' . $ie |
|
271 | - . '; Windows NT ' . $nt |
|
270 | + . '; MSIE '.$ie |
|
271 | + . '; Windows NT '.$nt |
|
272 | 272 | . '; WOW64' // A 32-bit version of Internet Explorer is running on a 64-bit processor. |
273 | - . '; Trident/' . $trident |
|
274 | - . '; .NET CLR ' . $net |
|
273 | + . '; Trident/'.$trident |
|
274 | + . '; .NET CLR '.$net |
|
275 | 275 | . ')'; |
276 | 276 | } |
277 | 277 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | * |
281 | 281 | * @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
282 | 282 | */ |
283 | - public static function firefox( $arch ) |
|
283 | + public static function firefox($arch) |
|
284 | 284 | { |
285 | 285 | // The release version of Gecko. |
286 | 286 | $gecko = self::get_version_gecko(); |
@@ -288,65 +288,65 @@ discard block |
||
288 | 288 | // On desktop, the gecko trail is fixed. |
289 | 289 | $trail = '20100101'; |
290 | 290 | |
291 | - $release = 'rv:' . $gecko; |
|
292 | - $version = 'Gecko/' . $trail . ' Firefox/' . $gecko; |
|
291 | + $release = 'rv:'.$gecko; |
|
292 | + $version = 'Gecko/'.$trail.' Firefox/'.$gecko; |
|
293 | 293 | |
294 | - switch ( $arch ) |
|
294 | + switch ($arch) |
|
295 | 295 | { |
296 | 296 | case 'lin': |
297 | - return '(X11; Linux {proc}; ' . $release . ') ' . $version; |
|
297 | + return '(X11; Linux {proc}; '.$release.') '.$version; |
|
298 | 298 | case 'mac': |
299 | 299 | $osx = self::get_version_osx(); |
300 | - return '(Macintosh; {proc} Mac OS X ' . $osx . '; ' . $release . ') ' . $version; |
|
300 | + return '(Macintosh; {proc} Mac OS X '.$osx.'; '.$release.') '.$version; |
|
301 | 301 | case 'win': |
302 | 302 | // fall through. |
303 | 303 | default: |
304 | 304 | $nt = self::get_version_nt(); |
305 | - return '(Windows NT ' . $nt . '; {lang}; ' . $release . ') ' . $version; |
|
305 | + return '(Windows NT '.$nt.'; {lang}; '.$release.') '.$version; |
|
306 | 306 | } |
307 | 307 | } |
308 | 308 | |
309 | - public static function chrome( $arch ) |
|
309 | + public static function chrome($arch) |
|
310 | 310 | { |
311 | - $chrome = ' Chrome/' . self::get_verison_chrome(); |
|
311 | + $chrome = ' Chrome/'.self::get_verison_chrome(); |
|
312 | 312 | |
313 | 313 | // WebKit Rendering Engine (WebKit = Backend, Safari = Frontend). |
314 | 314 | $engine = self::get_version_webkit(); |
315 | - $webkit = ' AppleWebKit/' . $engine . ' (KHTML, like Gecko)'; |
|
316 | - $safari = ' Safari/' . $engine; |
|
315 | + $webkit = ' AppleWebKit/'.$engine.' (KHTML, like Gecko)'; |
|
316 | + $safari = ' Safari/'.$engine; |
|
317 | 317 | |
318 | - switch ( $arch ) |
|
318 | + switch ($arch) |
|
319 | 319 | { |
320 | 320 | case 'lin': |
321 | - return '(X11; Linux {proc}) ' . $webkit . $chrome . $safari; |
|
321 | + return '(X11; Linux {proc}) '.$webkit.$chrome.$safari; |
|
322 | 322 | case 'mac': |
323 | 323 | $osx = self::get_version_osx(); |
324 | - return '(Macintosh; U; {proc} Mac OS X ' . $osx . ')' . $webkit . $chrome . $safari; |
|
324 | + return '(Macintosh; U; {proc} Mac OS X '.$osx.')'.$webkit.$chrome.$safari; |
|
325 | 325 | case 'win': |
326 | 326 | // fall through. |
327 | 327 | default: |
328 | 328 | $nt = self::get_version_nt(); |
329 | - return '(Windows NT ' . $nt . ') ' . $webkit . $chrome . $safari; |
|
329 | + return '(Windows NT '.$nt.') '.$webkit.$chrome.$safari; |
|
330 | 330 | } |
331 | 331 | } |
332 | 332 | |
333 | - public static function random( $lang = array( 'en-US' ) ) |
|
333 | + public static function random($lang = array('en-US')) |
|
334 | 334 | { |
335 | - list( $browser, $os ) = self::generate_platform(); |
|
335 | + list($browser, $os) = self::generate_platform(); |
|
336 | 336 | |
337 | - return self::generate( $browser, $os, $lang ); |
|
337 | + return self::generate($browser, $os, $lang); |
|
338 | 338 | } |
339 | 339 | |
340 | - public static function generate( $browser = 'chrome', $os = 'win', $lang = array( 'en-US' ) ) |
|
340 | + public static function generate($browser = 'chrome', $os = 'win', $lang = array('en-US')) |
|
341 | 341 | { |
342 | - $ua = self::MOZILLA . call_user_func( 'UAgent::' . $browser, $os ); |
|
342 | + $ua = self::MOZILLA.call_user_func('UAgent::'.$browser, $os); |
|
343 | 343 | |
344 | 344 | $tags = array( |
345 | - '{proc}' => self::get_processor( $os ), |
|
346 | - '{lang}' => self::get_language( $lang ), |
|
345 | + '{proc}' => self::get_processor($os), |
|
346 | + '{lang}' => self::get_language($lang), |
|
347 | 347 | ); |
348 | 348 | |
349 | - $ua = str_replace( array_keys( $tags ), array_values( $tags ), $ua ); |
|
349 | + $ua = str_replace(array_keys($tags), array_values($tags), $ua); |
|
350 | 350 | |
351 | 351 | return $ua; |
352 | 352 | } |
@@ -183,8 +183,7 @@ |
||
183 | 183 | if ( mt_rand( 0, 1 ) == 0 ) |
184 | 184 | { |
185 | 185 | $ver = mt_rand( 4, 5 ) . '.' . mt_rand( 0, 1 ); |
186 | - } |
|
187 | - else |
|
186 | + } else |
|
188 | 187 | { |
189 | 188 | $ver = mt_rand( 4, 5 ) . '.0.' . mt_rand( 1, 5 ); |
190 | 189 | } |