@@ -20,184 +20,184 @@ |
||
20 | 20 | abstract class AbstractObject |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $typeCheck = []; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $required = []; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - protected $errors = []; |
|
37 | - |
|
38 | - /** |
|
39 | - * AbstractObject constructor. |
|
40 | - * |
|
41 | - * @param array $args |
|
42 | - */ |
|
43 | - public function __construct(?array $args = []) |
|
44 | - { |
|
45 | - |
|
46 | - if (is_null($args)) { |
|
47 | - $args = []; |
|
48 | - } |
|
49 | - |
|
50 | - $this->setArgs($args); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param array $args |
|
55 | - * |
|
56 | - * @throws Exception |
|
57 | - */ |
|
58 | - protected function setArgs(array $args) |
|
59 | - { |
|
60 | - |
|
61 | - foreach ($this->typeCheck as $field_name => $field_type) { |
|
62 | - if (empty($args[$field_name]) || is_null($args[$field_name])) { |
|
63 | - if ($this->isFieldRequired($field_name)) { |
|
64 | - $this->addError('Missing "' . $field_name . '" in ' . static::class); |
|
65 | - } |
|
66 | - } else { |
|
67 | - $this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
|
68 | - } |
|
69 | - } |
|
70 | - $this->throwErrors(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param string $field_name |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - protected function isFieldRequired(string $field_name): bool |
|
79 | - { |
|
80 | - |
|
81 | - return in_array($field_name, $this->required); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $error |
|
86 | - * |
|
87 | - * @return array |
|
88 | - */ |
|
89 | - protected function addError(string $error): array |
|
90 | - { |
|
91 | - |
|
92 | - array_push($this->errors, $error); |
|
93 | - |
|
94 | - return $this->errors; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $field_type |
|
99 | - * @param string $field_value |
|
100 | - * |
|
101 | - * @return mixed |
|
102 | - */ |
|
103 | - protected function parseFieldValue(string $field_type, $field_value) |
|
104 | - { |
|
105 | - |
|
106 | - switch ($field_type) { |
|
107 | - case 'string': |
|
108 | - case 'int': |
|
109 | - case 'float': |
|
110 | - case 'array': |
|
111 | - case 'json': |
|
112 | - return $field_value; |
|
113 | - default: |
|
114 | - return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @throws Exception |
|
120 | - */ |
|
121 | - protected function throwErrors() |
|
122 | - { |
|
123 | - |
|
124 | - if (count($this->errors)) { |
|
125 | - throw new Exception(implode(', ', $this->errors)); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public function toJson(): string |
|
133 | - { |
|
134 | - |
|
135 | - return json_encode($this->toArray()); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function toArray(): array |
|
142 | - { |
|
143 | - |
|
144 | - $fields = get_object_vars($this); |
|
145 | - |
|
146 | - foreach ($fields as $field_name => $field_value) { |
|
147 | - |
|
148 | - if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
|
149 | - $fields[$field_name] = $field_value->toArray(); |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - return $fields; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public function __toString(): string |
|
160 | - { |
|
161 | - |
|
162 | - return implode(',', $this->toArray()); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param $name |
|
167 | - * @param $arguments |
|
168 | - * |
|
169 | - * @return mixed |
|
170 | - */ |
|
171 | - public function __call($name, $arguments) |
|
172 | - { |
|
173 | - |
|
174 | - preg_match('/(?<=(g|s)et)([A-Za-z0-9])\w+/', $name, $match); |
|
175 | - |
|
176 | - $camel_field = (empty($match[0])) ? '' : $match[0]; |
|
177 | - |
|
178 | - $snake_field = $this->getFieldName($camel_field); |
|
179 | - |
|
180 | - $field_type = (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
|
181 | - |
|
182 | - if (!empty($match[1]) && $field_type) { |
|
183 | - switch ($match[1]) { |
|
184 | - case 's': |
|
185 | - return $this->$snake_field = $this->parseFieldValue($field_type, current($arguments)); |
|
186 | - case 'g': |
|
187 | - return $this->$snake_field; |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @param string $initial_field_name |
|
194 | - * |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - protected function getFieldName(string $initial_field_name): string |
|
198 | - { |
|
199 | - |
|
200 | - return camel2Snake($initial_field_name); |
|
201 | - } |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $typeCheck = []; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $required = []; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + protected $errors = []; |
|
37 | + |
|
38 | + /** |
|
39 | + * AbstractObject constructor. |
|
40 | + * |
|
41 | + * @param array $args |
|
42 | + */ |
|
43 | + public function __construct(?array $args = []) |
|
44 | + { |
|
45 | + |
|
46 | + if (is_null($args)) { |
|
47 | + $args = []; |
|
48 | + } |
|
49 | + |
|
50 | + $this->setArgs($args); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param array $args |
|
55 | + * |
|
56 | + * @throws Exception |
|
57 | + */ |
|
58 | + protected function setArgs(array $args) |
|
59 | + { |
|
60 | + |
|
61 | + foreach ($this->typeCheck as $field_name => $field_type) { |
|
62 | + if (empty($args[$field_name]) || is_null($args[$field_name])) { |
|
63 | + if ($this->isFieldRequired($field_name)) { |
|
64 | + $this->addError('Missing "' . $field_name . '" in ' . static::class); |
|
65 | + } |
|
66 | + } else { |
|
67 | + $this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
|
68 | + } |
|
69 | + } |
|
70 | + $this->throwErrors(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param string $field_name |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + protected function isFieldRequired(string $field_name): bool |
|
79 | + { |
|
80 | + |
|
81 | + return in_array($field_name, $this->required); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $error |
|
86 | + * |
|
87 | + * @return array |
|
88 | + */ |
|
89 | + protected function addError(string $error): array |
|
90 | + { |
|
91 | + |
|
92 | + array_push($this->errors, $error); |
|
93 | + |
|
94 | + return $this->errors; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $field_type |
|
99 | + * @param string $field_value |
|
100 | + * |
|
101 | + * @return mixed |
|
102 | + */ |
|
103 | + protected function parseFieldValue(string $field_type, $field_value) |
|
104 | + { |
|
105 | + |
|
106 | + switch ($field_type) { |
|
107 | + case 'string': |
|
108 | + case 'int': |
|
109 | + case 'float': |
|
110 | + case 'array': |
|
111 | + case 'json': |
|
112 | + return $field_value; |
|
113 | + default: |
|
114 | + return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @throws Exception |
|
120 | + */ |
|
121 | + protected function throwErrors() |
|
122 | + { |
|
123 | + |
|
124 | + if (count($this->errors)) { |
|
125 | + throw new Exception(implode(', ', $this->errors)); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public function toJson(): string |
|
133 | + { |
|
134 | + |
|
135 | + return json_encode($this->toArray()); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function toArray(): array |
|
142 | + { |
|
143 | + |
|
144 | + $fields = get_object_vars($this); |
|
145 | + |
|
146 | + foreach ($fields as $field_name => $field_value) { |
|
147 | + |
|
148 | + if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
|
149 | + $fields[$field_name] = $field_value->toArray(); |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + return $fields; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public function __toString(): string |
|
160 | + { |
|
161 | + |
|
162 | + return implode(',', $this->toArray()); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param $name |
|
167 | + * @param $arguments |
|
168 | + * |
|
169 | + * @return mixed |
|
170 | + */ |
|
171 | + public function __call($name, $arguments) |
|
172 | + { |
|
173 | + |
|
174 | + preg_match('/(?<=(g|s)et)([A-Za-z0-9])\w+/', $name, $match); |
|
175 | + |
|
176 | + $camel_field = (empty($match[0])) ? '' : $match[0]; |
|
177 | + |
|
178 | + $snake_field = $this->getFieldName($camel_field); |
|
179 | + |
|
180 | + $field_type = (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
|
181 | + |
|
182 | + if (!empty($match[1]) && $field_type) { |
|
183 | + switch ($match[1]) { |
|
184 | + case 's': |
|
185 | + return $this->$snake_field = $this->parseFieldValue($field_type, current($arguments)); |
|
186 | + case 'g': |
|
187 | + return $this->$snake_field; |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @param string $initial_field_name |
|
194 | + * |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + protected function getFieldName(string $initial_field_name): string |
|
198 | + { |
|
199 | + |
|
200 | + return camel2Snake($initial_field_name); |
|
201 | + } |
|
202 | 202 | |
203 | 203 | } |
204 | 204 | \ No newline at end of file |
@@ -27,35 +27,35 @@ |
||
27 | 27 | class TimeZone extends Api |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - const SERVICE_ENDPOINT = 'timezone'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $result_type = TimeZoneResult::class; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param Location $location |
|
42 | - * @param int $timestamp |
|
43 | - * @param string|null $language |
|
44 | - * |
|
45 | - * @return GoogleMapsResult |
|
46 | - */ |
|
47 | - public function get(Location $location, int $timestamp, string $language = null): GoogleMapsResult |
|
48 | - { |
|
49 | - |
|
50 | - $params = [ |
|
51 | - GoogleMapsRequestFields::LOCATION => $location, |
|
52 | - GoogleMapsRequestFields::TIMESTAMP => $timestamp, |
|
53 | - ]; |
|
54 | - |
|
55 | - if ($language) { |
|
56 | - $params[GoogleMapsRequestFields::LANGUAGE] = $language; |
|
57 | - } |
|
58 | - |
|
59 | - return $this->callApi($params); |
|
60 | - } |
|
30 | + /** |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + const SERVICE_ENDPOINT = 'timezone'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $result_type = TimeZoneResult::class; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param Location $location |
|
42 | + * @param int $timestamp |
|
43 | + * @param string|null $language |
|
44 | + * |
|
45 | + * @return GoogleMapsResult |
|
46 | + */ |
|
47 | + public function get(Location $location, int $timestamp, string $language = null): GoogleMapsResult |
|
48 | + { |
|
49 | + |
|
50 | + $params = [ |
|
51 | + GoogleMapsRequestFields::LOCATION => $location, |
|
52 | + GoogleMapsRequestFields::TIMESTAMP => $timestamp, |
|
53 | + ]; |
|
54 | + |
|
55 | + if ($language) { |
|
56 | + $params[GoogleMapsRequestFields::LANGUAGE] = $language; |
|
57 | + } |
|
58 | + |
|
59 | + return $this->callApi($params); |
|
60 | + } |
|
61 | 61 | } |
62 | 62 | \ No newline at end of file |
@@ -17,186 +17,186 @@ |
||
17 | 17 | class GoogleMapsResultFields |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string address_components |
|
22 | - */ |
|
23 | - const ADDRESS_COMPONENTS = 'address_components'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string formatted_address |
|
27 | - */ |
|
28 | - const FORMATTED_ADDRESS = 'formatted_address'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string geometry |
|
32 | - */ |
|
33 | - const GEOMETRY = 'geometry'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string viewport |
|
37 | - */ |
|
38 | - const VIEWPORT = 'viewport'; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string place_id |
|
42 | - */ |
|
43 | - const PLACE_ID = 'place_id'; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string location_type |
|
47 | - */ |
|
48 | - const LOCATION_TYPE = 'location_type'; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string types |
|
52 | - */ |
|
53 | - const TYPES = 'types'; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string location |
|
57 | - */ |
|
58 | - const LOCATION = 'location'; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var string elevation |
|
62 | - */ |
|
63 | - const ELEVATION = 'elevation'; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var string resolution |
|
67 | - */ |
|
68 | - const RESOLUTION = 'resolution'; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var string icon |
|
72 | - * @since 0.5.0 |
|
73 | - */ |
|
74 | - const ICON = 'icon'; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var string id |
|
78 | - * @since 0.5.0 |
|
79 | - */ |
|
80 | - const ID = 'id'; |
|
81 | - |
|
82 | - /** |
|
83 | - * @var string name |
|
84 | - * @since 0.5.0 |
|
85 | - */ |
|
86 | - const NAME = 'name'; |
|
87 | - |
|
88 | - /** |
|
89 | - * @var string photos |
|
90 | - * @since 0.5.0 |
|
91 | - */ |
|
92 | - const PHOTOS = 'photos'; |
|
93 | - |
|
94 | - /** |
|
95 | - * @var string reference |
|
96 | - * @since 0.5.0 |
|
97 | - */ |
|
98 | - const REFERENCE = 'reference'; |
|
99 | - |
|
100 | - /** |
|
101 | - * @var string vicinity |
|
102 | - * @since 0.5.0 |
|
103 | - */ |
|
104 | - const VICINITY = 'vicinity'; |
|
105 | - |
|
106 | - /** |
|
107 | - * @var string opening_hours |
|
108 | - * @since 0.5.0 |
|
109 | - */ |
|
110 | - const OPENING_HOURS = 'opening_hours'; |
|
111 | - |
|
112 | - /** |
|
113 | - * @var string price_level |
|
114 | - * @since 0.5.0 |
|
115 | - */ |
|
116 | - const PRICE_LEVEL = 'price_level'; |
|
117 | - |
|
118 | - /** |
|
119 | - * @var string rating |
|
120 | - * @since 0.5.0 |
|
121 | - */ |
|
122 | - const RATING = 'rating'; |
|
123 | - |
|
124 | - /** |
|
125 | - * @var string permanently_closed |
|
126 | - * @since 0.5.0 |
|
127 | - */ |
|
128 | - const PERMANENTLY_CLOSED = 'permanently_closed'; |
|
129 | - |
|
130 | - /** |
|
131 | - * @var string plus_code |
|
132 | - * @since 0.5.0 |
|
133 | - */ |
|
134 | - const PLUS_CODE = 'plus_code'; |
|
135 | - |
|
136 | - /** |
|
137 | - * @var string next_page_token |
|
138 | - * @since 0.5.0 |
|
139 | - */ |
|
140 | - const NEXT_PAGE_TOKEN = 'next_page_token'; |
|
141 | - |
|
142 | - /** |
|
143 | - * @var string reviews |
|
144 | - * @since 0.6.0 |
|
145 | - */ |
|
146 | - const REVIEWS = 'reviews'; |
|
147 | - |
|
148 | - /** |
|
149 | - * @var string utc_offset |
|
150 | - * @since 0.6.0 |
|
151 | - */ |
|
152 | - const UTC_OFFSET = 'utc_offset'; |
|
153 | - |
|
154 | - /** |
|
155 | - * @var string website |
|
156 | - * @since 0.6.0 |
|
157 | - */ |
|
158 | - const WEBSITE = 'website'; |
|
159 | - |
|
160 | - /** |
|
161 | - * @var string international_phone_number |
|
162 | - * @since 0.6.0 |
|
163 | - */ |
|
164 | - const INTERNATIONAL_PHONE_NUMBER = 'international_phone_number'; |
|
165 | - |
|
166 | - /** |
|
167 | - * @var string formatted_phone_number |
|
168 | - * @since 0.6.0 |
|
169 | - */ |
|
170 | - const FORMATTED_PHONE_NUMBER = 'formatted_phone_number'; |
|
171 | - |
|
172 | - /** |
|
173 | - * @var string adr_address |
|
174 | - * @since 0.6.0 |
|
175 | - */ |
|
176 | - const ADR_ADDRESS = 'adr_address'; |
|
177 | - |
|
178 | - /** |
|
179 | - * @var string dstOffset |
|
180 | - * @since 0.7.0 |
|
181 | - */ |
|
182 | - const DST_OFFSET = 'dstOffset'; |
|
183 | - |
|
184 | - /** |
|
185 | - * @var string rawOffset |
|
186 | - * @since 0.7.0 |
|
187 | - */ |
|
188 | - const RAW_OFFSET = 'rawOffset'; |
|
189 | - |
|
190 | - /** |
|
191 | - * @var string timeZoneId |
|
192 | - * @since 0.7.0 |
|
193 | - */ |
|
194 | - const TIMEZONE_ID = 'timeZoneId'; |
|
195 | - |
|
196 | - /** |
|
197 | - * @var string timeZoneName |
|
198 | - * @since 0.7.0 |
|
199 | - */ |
|
200 | - const TIMEZONE_NAME = 'timeZoneName'; |
|
20 | + /** |
|
21 | + * @var string address_components |
|
22 | + */ |
|
23 | + const ADDRESS_COMPONENTS = 'address_components'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string formatted_address |
|
27 | + */ |
|
28 | + const FORMATTED_ADDRESS = 'formatted_address'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string geometry |
|
32 | + */ |
|
33 | + const GEOMETRY = 'geometry'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string viewport |
|
37 | + */ |
|
38 | + const VIEWPORT = 'viewport'; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string place_id |
|
42 | + */ |
|
43 | + const PLACE_ID = 'place_id'; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string location_type |
|
47 | + */ |
|
48 | + const LOCATION_TYPE = 'location_type'; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string types |
|
52 | + */ |
|
53 | + const TYPES = 'types'; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string location |
|
57 | + */ |
|
58 | + const LOCATION = 'location'; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var string elevation |
|
62 | + */ |
|
63 | + const ELEVATION = 'elevation'; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var string resolution |
|
67 | + */ |
|
68 | + const RESOLUTION = 'resolution'; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var string icon |
|
72 | + * @since 0.5.0 |
|
73 | + */ |
|
74 | + const ICON = 'icon'; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var string id |
|
78 | + * @since 0.5.0 |
|
79 | + */ |
|
80 | + const ID = 'id'; |
|
81 | + |
|
82 | + /** |
|
83 | + * @var string name |
|
84 | + * @since 0.5.0 |
|
85 | + */ |
|
86 | + const NAME = 'name'; |
|
87 | + |
|
88 | + /** |
|
89 | + * @var string photos |
|
90 | + * @since 0.5.0 |
|
91 | + */ |
|
92 | + const PHOTOS = 'photos'; |
|
93 | + |
|
94 | + /** |
|
95 | + * @var string reference |
|
96 | + * @since 0.5.0 |
|
97 | + */ |
|
98 | + const REFERENCE = 'reference'; |
|
99 | + |
|
100 | + /** |
|
101 | + * @var string vicinity |
|
102 | + * @since 0.5.0 |
|
103 | + */ |
|
104 | + const VICINITY = 'vicinity'; |
|
105 | + |
|
106 | + /** |
|
107 | + * @var string opening_hours |
|
108 | + * @since 0.5.0 |
|
109 | + */ |
|
110 | + const OPENING_HOURS = 'opening_hours'; |
|
111 | + |
|
112 | + /** |
|
113 | + * @var string price_level |
|
114 | + * @since 0.5.0 |
|
115 | + */ |
|
116 | + const PRICE_LEVEL = 'price_level'; |
|
117 | + |
|
118 | + /** |
|
119 | + * @var string rating |
|
120 | + * @since 0.5.0 |
|
121 | + */ |
|
122 | + const RATING = 'rating'; |
|
123 | + |
|
124 | + /** |
|
125 | + * @var string permanently_closed |
|
126 | + * @since 0.5.0 |
|
127 | + */ |
|
128 | + const PERMANENTLY_CLOSED = 'permanently_closed'; |
|
129 | + |
|
130 | + /** |
|
131 | + * @var string plus_code |
|
132 | + * @since 0.5.0 |
|
133 | + */ |
|
134 | + const PLUS_CODE = 'plus_code'; |
|
135 | + |
|
136 | + /** |
|
137 | + * @var string next_page_token |
|
138 | + * @since 0.5.0 |
|
139 | + */ |
|
140 | + const NEXT_PAGE_TOKEN = 'next_page_token'; |
|
141 | + |
|
142 | + /** |
|
143 | + * @var string reviews |
|
144 | + * @since 0.6.0 |
|
145 | + */ |
|
146 | + const REVIEWS = 'reviews'; |
|
147 | + |
|
148 | + /** |
|
149 | + * @var string utc_offset |
|
150 | + * @since 0.6.0 |
|
151 | + */ |
|
152 | + const UTC_OFFSET = 'utc_offset'; |
|
153 | + |
|
154 | + /** |
|
155 | + * @var string website |
|
156 | + * @since 0.6.0 |
|
157 | + */ |
|
158 | + const WEBSITE = 'website'; |
|
159 | + |
|
160 | + /** |
|
161 | + * @var string international_phone_number |
|
162 | + * @since 0.6.0 |
|
163 | + */ |
|
164 | + const INTERNATIONAL_PHONE_NUMBER = 'international_phone_number'; |
|
165 | + |
|
166 | + /** |
|
167 | + * @var string formatted_phone_number |
|
168 | + * @since 0.6.0 |
|
169 | + */ |
|
170 | + const FORMATTED_PHONE_NUMBER = 'formatted_phone_number'; |
|
171 | + |
|
172 | + /** |
|
173 | + * @var string adr_address |
|
174 | + * @since 0.6.0 |
|
175 | + */ |
|
176 | + const ADR_ADDRESS = 'adr_address'; |
|
177 | + |
|
178 | + /** |
|
179 | + * @var string dstOffset |
|
180 | + * @since 0.7.0 |
|
181 | + */ |
|
182 | + const DST_OFFSET = 'dstOffset'; |
|
183 | + |
|
184 | + /** |
|
185 | + * @var string rawOffset |
|
186 | + * @since 0.7.0 |
|
187 | + */ |
|
188 | + const RAW_OFFSET = 'rawOffset'; |
|
189 | + |
|
190 | + /** |
|
191 | + * @var string timeZoneId |
|
192 | + * @since 0.7.0 |
|
193 | + */ |
|
194 | + const TIMEZONE_ID = 'timeZoneId'; |
|
195 | + |
|
196 | + /** |
|
197 | + * @var string timeZoneName |
|
198 | + * @since 0.7.0 |
|
199 | + */ |
|
200 | + const TIMEZONE_NAME = 'timeZoneName'; |
|
201 | 201 | |
202 | 202 | } |
203 | 203 | \ No newline at end of file |
@@ -17,169 +17,169 @@ |
||
17 | 17 | class GoogleMapsRequestFields |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string key |
|
22 | - */ |
|
23 | - const KEY = 'key'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string sensor |
|
27 | - */ |
|
28 | - const SENSOR = 'sensor'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string latlng |
|
32 | - */ |
|
33 | - const LATLNG = 'latlng'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string address |
|
37 | - */ |
|
38 | - const ADDRESS = 'address'; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string place_id |
|
42 | - */ |
|
43 | - const PLACE_ID = 'place_id'; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string location |
|
47 | - * @since 0.5.0 |
|
48 | - */ |
|
49 | - const LOCATION = 'location'; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string locations |
|
53 | - */ |
|
54 | - const LOCATIONS = 'locations'; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string path |
|
58 | - */ |
|
59 | - const PATH = 'path'; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string samples |
|
63 | - */ |
|
64 | - const SAMPLES = 'samples'; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string input |
|
68 | - * @since 0.5.0 |
|
69 | - */ |
|
70 | - const INPUT = 'input'; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var string inputtype |
|
74 | - * @since 0.5.0 |
|
75 | - */ |
|
76 | - const INPUTTYPE = 'inputtype'; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var string language |
|
80 | - * @since 0.5.0 |
|
81 | - */ |
|
82 | - const LANGUAGE = 'language'; |
|
83 | - |
|
84 | - /** |
|
85 | - * @var string fields |
|
86 | - * @since 0.5.0 |
|
87 | - */ |
|
88 | - const FIELDS = 'fields'; |
|
89 | - |
|
90 | - /** |
|
91 | - * @var string locationbias |
|
92 | - * @since 0.5.0 |
|
93 | - */ |
|
94 | - const LOCATIONBIAS = 'locationbias'; |
|
95 | - |
|
96 | - /** |
|
97 | - * @var string radius |
|
98 | - * @since 0.5.0 |
|
99 | - */ |
|
100 | - const RADIUS = 'radius'; |
|
101 | - |
|
102 | - /** |
|
103 | - * @var string rankby |
|
104 | - * @since 0.5.0 |
|
105 | - */ |
|
106 | - const RANKBY = 'rankby'; |
|
107 | - |
|
108 | - /** |
|
109 | - * @var string keyword |
|
110 | - * @since 0.5.0 |
|
111 | - */ |
|
112 | - const KEYWORD = 'keyword'; |
|
113 | - |
|
114 | - /** |
|
115 | - * @var string minprice |
|
116 | - * @since 0.5.0 |
|
117 | - */ |
|
118 | - const MINPRICE = 'minprice'; |
|
119 | - |
|
120 | - /** |
|
121 | - * @var string maxprice |
|
122 | - * @since 0.5.0 |
|
123 | - */ |
|
124 | - const MAXPRICE = 'maxprice'; |
|
125 | - |
|
126 | - /** |
|
127 | - * @var string name |
|
128 | - * @since 0.5.0 |
|
129 | - */ |
|
130 | - const NAME = 'name'; |
|
131 | - |
|
132 | - /** |
|
133 | - * @var string opennow |
|
134 | - * @since 0.5.0 |
|
135 | - */ |
|
136 | - const OPENNOW = 'opennow'; |
|
137 | - |
|
138 | - /** |
|
139 | - * @var string pagetoken |
|
140 | - * @since 0.5.0 |
|
141 | - */ |
|
142 | - const PAGETOKEN = 'pagetoken'; |
|
143 | - |
|
144 | - /** |
|
145 | - * @var string type |
|
146 | - * @since 0.5.0 |
|
147 | - */ |
|
148 | - const TYPE = 'type'; |
|
149 | - |
|
150 | - /** |
|
151 | - * @var string query |
|
152 | - * @since 0.5.0 |
|
153 | - */ |
|
154 | - const QUERY = 'query'; |
|
155 | - |
|
156 | - /** |
|
157 | - * @var string next_page_token |
|
158 | - * @since 0.5.0 |
|
159 | - */ |
|
160 | - const NEXT_PAGE_TOKEN = 'next_page_token'; |
|
161 | - |
|
162 | - /** |
|
163 | - * @var string photoreference |
|
164 | - * @since 0.6.0 |
|
165 | - */ |
|
166 | - const PHOTOREFERENCE = 'photoreference'; |
|
167 | - |
|
168 | - /** |
|
169 | - * @var string maxwidth |
|
170 | - * @since 0.6.0 |
|
171 | - */ |
|
172 | - const MAXWIDTH = 'maxwidth'; |
|
173 | - |
|
174 | - /** |
|
175 | - * @var string maxheight |
|
176 | - * @since 0.6.0 |
|
177 | - */ |
|
178 | - const MAXHEIGHT = 'maxheight'; |
|
179 | - |
|
180 | - /** |
|
181 | - * @var string timestamp |
|
182 | - * @since 0.7.0 |
|
183 | - */ |
|
184 | - const TIMESTAMP = 'timestamp'; |
|
20 | + /** |
|
21 | + * @var string key |
|
22 | + */ |
|
23 | + const KEY = 'key'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string sensor |
|
27 | + */ |
|
28 | + const SENSOR = 'sensor'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string latlng |
|
32 | + */ |
|
33 | + const LATLNG = 'latlng'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string address |
|
37 | + */ |
|
38 | + const ADDRESS = 'address'; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string place_id |
|
42 | + */ |
|
43 | + const PLACE_ID = 'place_id'; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string location |
|
47 | + * @since 0.5.0 |
|
48 | + */ |
|
49 | + const LOCATION = 'location'; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string locations |
|
53 | + */ |
|
54 | + const LOCATIONS = 'locations'; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string path |
|
58 | + */ |
|
59 | + const PATH = 'path'; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string samples |
|
63 | + */ |
|
64 | + const SAMPLES = 'samples'; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string input |
|
68 | + * @since 0.5.0 |
|
69 | + */ |
|
70 | + const INPUT = 'input'; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var string inputtype |
|
74 | + * @since 0.5.0 |
|
75 | + */ |
|
76 | + const INPUTTYPE = 'inputtype'; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var string language |
|
80 | + * @since 0.5.0 |
|
81 | + */ |
|
82 | + const LANGUAGE = 'language'; |
|
83 | + |
|
84 | + /** |
|
85 | + * @var string fields |
|
86 | + * @since 0.5.0 |
|
87 | + */ |
|
88 | + const FIELDS = 'fields'; |
|
89 | + |
|
90 | + /** |
|
91 | + * @var string locationbias |
|
92 | + * @since 0.5.0 |
|
93 | + */ |
|
94 | + const LOCATIONBIAS = 'locationbias'; |
|
95 | + |
|
96 | + /** |
|
97 | + * @var string radius |
|
98 | + * @since 0.5.0 |
|
99 | + */ |
|
100 | + const RADIUS = 'radius'; |
|
101 | + |
|
102 | + /** |
|
103 | + * @var string rankby |
|
104 | + * @since 0.5.0 |
|
105 | + */ |
|
106 | + const RANKBY = 'rankby'; |
|
107 | + |
|
108 | + /** |
|
109 | + * @var string keyword |
|
110 | + * @since 0.5.0 |
|
111 | + */ |
|
112 | + const KEYWORD = 'keyword'; |
|
113 | + |
|
114 | + /** |
|
115 | + * @var string minprice |
|
116 | + * @since 0.5.0 |
|
117 | + */ |
|
118 | + const MINPRICE = 'minprice'; |
|
119 | + |
|
120 | + /** |
|
121 | + * @var string maxprice |
|
122 | + * @since 0.5.0 |
|
123 | + */ |
|
124 | + const MAXPRICE = 'maxprice'; |
|
125 | + |
|
126 | + /** |
|
127 | + * @var string name |
|
128 | + * @since 0.5.0 |
|
129 | + */ |
|
130 | + const NAME = 'name'; |
|
131 | + |
|
132 | + /** |
|
133 | + * @var string opennow |
|
134 | + * @since 0.5.0 |
|
135 | + */ |
|
136 | + const OPENNOW = 'opennow'; |
|
137 | + |
|
138 | + /** |
|
139 | + * @var string pagetoken |
|
140 | + * @since 0.5.0 |
|
141 | + */ |
|
142 | + const PAGETOKEN = 'pagetoken'; |
|
143 | + |
|
144 | + /** |
|
145 | + * @var string type |
|
146 | + * @since 0.5.0 |
|
147 | + */ |
|
148 | + const TYPE = 'type'; |
|
149 | + |
|
150 | + /** |
|
151 | + * @var string query |
|
152 | + * @since 0.5.0 |
|
153 | + */ |
|
154 | + const QUERY = 'query'; |
|
155 | + |
|
156 | + /** |
|
157 | + * @var string next_page_token |
|
158 | + * @since 0.5.0 |
|
159 | + */ |
|
160 | + const NEXT_PAGE_TOKEN = 'next_page_token'; |
|
161 | + |
|
162 | + /** |
|
163 | + * @var string photoreference |
|
164 | + * @since 0.6.0 |
|
165 | + */ |
|
166 | + const PHOTOREFERENCE = 'photoreference'; |
|
167 | + |
|
168 | + /** |
|
169 | + * @var string maxwidth |
|
170 | + * @since 0.6.0 |
|
171 | + */ |
|
172 | + const MAXWIDTH = 'maxwidth'; |
|
173 | + |
|
174 | + /** |
|
175 | + * @var string maxheight |
|
176 | + * @since 0.6.0 |
|
177 | + */ |
|
178 | + const MAXHEIGHT = 'maxheight'; |
|
179 | + |
|
180 | + /** |
|
181 | + * @var string timestamp |
|
182 | + * @since 0.7.0 |
|
183 | + */ |
|
184 | + const TIMESTAMP = 'timestamp'; |
|
185 | 185 | } |
186 | 186 | \ No newline at end of file |
@@ -23,343 +23,343 @@ |
||
23 | 23 | class GoogleMapsResponse |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var Response |
|
28 | - */ |
|
29 | - protected $response = null; |
|
30 | - |
|
31 | - /** |
|
32 | - * Single result |
|
33 | - * When the Places service returns results from a details request, it places them within a single result |
|
34 | - * |
|
35 | - * @var array |
|
36 | - * @see https://developers.google.com/places/web-service/details#PlaceDetailsResults |
|
37 | - */ |
|
38 | - protected $result = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * contains an array of places, with information about each. |
|
42 | - * The Places API returns up to 20 establishment results per query. |
|
43 | - * Additionally, political results may be returned which serve to identify the area of the request. |
|
44 | - * |
|
45 | - * @var array |
|
46 | - * @see https://developers.google.com/places/web-service/search#PlaceSearchResults |
|
47 | - */ |
|
48 | - protected $results = null; |
|
49 | - |
|
50 | - /** |
|
51 | - * contains metadata on the request. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - * @see https://developers.google.com/places/web-service/search#PlaceSearchStatusCodes |
|
55 | - */ |
|
56 | - protected $status = null; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - protected $error_message = null; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var array |
|
65 | - */ |
|
66 | - protected $array_response = null; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var null|array |
|
70 | - */ |
|
71 | - protected $http_status_code = null; |
|
72 | - |
|
73 | - /** |
|
74 | - * may contain a set of attributions about this listing which must be displayed to the user (some listings may not have attribution). |
|
75 | - * |
|
76 | - * @var null|array |
|
77 | - * @since 0.5.0 |
|
78 | - */ |
|
79 | - protected $html_attributions = null; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var null|string |
|
83 | - * @since 0.5.0 |
|
84 | - */ |
|
85 | - protected $next_page_token = null; |
|
86 | - |
|
87 | - /** |
|
88 | - * GoogleMapsResponse constructor. |
|
89 | - * |
|
90 | - * @param Response $response |
|
91 | - */ |
|
92 | - public function __construct(Response $response) |
|
93 | - { |
|
94 | - |
|
95 | - $this->setResponse($response); |
|
96 | - |
|
97 | - $this->parseResponse(); |
|
98 | - |
|
99 | - $this->checkHttpStatusCode(); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param Response $response |
|
104 | - * |
|
105 | - * @return GoogleMapsResponse |
|
106 | - */ |
|
107 | - public function setResponse(Response $response): GoogleMapsResponse |
|
108 | - { |
|
109 | - |
|
110 | - $this->response = $response; |
|
111 | - |
|
112 | - return $this; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @return GoogleMapsResponse |
|
117 | - * |
|
118 | - * @throws RequestException |
|
119 | - * @throws ResponseException |
|
120 | - */ |
|
121 | - protected function parseResponse(): GoogleMapsResponse |
|
122 | - { |
|
123 | - |
|
124 | - $json_response = $this->response->getBody()->getContents(); |
|
125 | - $array_response = $this->toArray($json_response); |
|
126 | - $result = null; |
|
127 | - $results = null; |
|
128 | - |
|
129 | - if (empty($array_response[GoogleMapsResponseFields::STATUS])) { |
|
130 | - throw new ResponseException('Missing "status" in GoogleMapsApi Response'); |
|
131 | - } |
|
132 | - $this->setStatus($array_response[GoogleMapsResponseFields::STATUS]); |
|
133 | - |
|
134 | - if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) { |
|
135 | - $error_message = 'Something went wrong'; |
|
136 | - if($this->getStatus() === 'INVALID_REQUEST'){ |
|
137 | - print_r($array_response);die();} |
|
138 | - if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) { |
|
139 | - $error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE]; |
|
140 | - $this->setErrorMessage($error_message); |
|
141 | - } elseif (!empty($array_response[GoogleMapsResponseFields::STATUS])) { |
|
142 | - $error_message .= ': ' . $array_response[GoogleMapsResponseFields::STATUS]; |
|
143 | - $this->setErrorMessage($error_message); |
|
144 | - } |
|
145 | - throw new RequestException($error_message); |
|
146 | - |
|
147 | - } |
|
148 | - |
|
149 | - if (!empty($array_response[GoogleMapsResponseFields::RESULTS])) { |
|
150 | - $results = $array_response[GoogleMapsResponseFields::RESULTS]; |
|
151 | - |
|
152 | - } elseif (!empty($array_response[GoogleMapsResponseFields::CANDIDATES])) { |
|
153 | - $results = $array_response[GoogleMapsResponseFields::CANDIDATES]; |
|
154 | - |
|
155 | - }elseif (!empty($array_response[GoogleMapsResponseFields::RESULT])) { |
|
156 | - $result = $array_response[GoogleMapsResponseFields::RESULT]; |
|
157 | - |
|
158 | - } else { |
|
159 | - $result = $array_response; |
|
160 | - } |
|
161 | - $this->setResult($result); |
|
162 | - $this->setResults($results); |
|
163 | - |
|
164 | - if (!empty($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS])) { |
|
165 | - $this->setHtmlAttributions($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS]); |
|
166 | - } |
|
167 | - |
|
168 | - if (!empty($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN])) { |
|
169 | - $this->setNextPageToken($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN]); |
|
170 | - } |
|
171 | - |
|
172 | - return $this; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $json_response |
|
177 | - * |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function toArray(string $json_response): array |
|
181 | - { |
|
182 | - |
|
183 | - $this->array_response = json_decode($json_response, true); |
|
184 | - |
|
185 | - return $this->array_response; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return string |
|
190 | - */ |
|
191 | - public function getStatus(): string |
|
192 | - { |
|
193 | - |
|
194 | - return $this->status; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param string $status |
|
199 | - * |
|
200 | - * @return GoogleMapsResponse |
|
201 | - */ |
|
202 | - public function setStatus(string $status) |
|
203 | - { |
|
204 | - |
|
205 | - $this->status = $status; |
|
206 | - |
|
207 | - return $this; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Check HTTP status code (silent/No exceptions!) |
|
212 | - * @return int |
|
213 | - */ |
|
214 | - protected function checkHttpStatusCode(): int |
|
215 | - { |
|
216 | - |
|
217 | - $this->http_status_code = $this->response->getStatusCode(); |
|
218 | - |
|
219 | - return $this->http_status_code; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @return array |
|
224 | - */ |
|
225 | - public function getResults() |
|
226 | - { |
|
227 | - |
|
228 | - return $this->results; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param array $results |
|
233 | - * |
|
234 | - * @return $this |
|
235 | - */ |
|
236 | - public function setResults(?array $results) |
|
237 | - { |
|
238 | - |
|
239 | - $this->results = $results; |
|
240 | - |
|
241 | - return $this; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return array |
|
246 | - * @since v0.6.0 |
|
247 | - */ |
|
248 | - public function getResult() |
|
249 | - { |
|
250 | - |
|
251 | - return $this->result; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * @param array $result |
|
256 | - * |
|
257 | - * @return $this |
|
258 | - * @since v0.6.0 |
|
259 | - */ |
|
260 | - public function setResult(?array $result) |
|
261 | - { |
|
262 | - |
|
263 | - $this->result = $result; |
|
264 | - |
|
265 | - return $this; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * @return array |
|
270 | - */ |
|
271 | - public function getArrayResponse(): array |
|
272 | - { |
|
273 | - |
|
274 | - return $this->array_response; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @param array $array_response |
|
279 | - * |
|
280 | - * @return GoogleMapsResponse |
|
281 | - */ |
|
282 | - public function setArrayResponse(array $array_response): GoogleMapsResponse |
|
283 | - { |
|
284 | - |
|
285 | - $this->array_response = $array_response; |
|
286 | - |
|
287 | - return $this; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * @return mixed |
|
292 | - */ |
|
293 | - public function getErrorMessage() |
|
294 | - { |
|
295 | - |
|
296 | - return $this->error_message; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param $error_message |
|
301 | - * |
|
302 | - * @return GoogleMapsResponse |
|
303 | - */ |
|
304 | - public function setErrorMessage($error_message): GoogleMapsResponse |
|
305 | - { |
|
306 | - |
|
307 | - $this->error_message = $error_message; |
|
308 | - |
|
309 | - return $this; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * @return int |
|
314 | - */ |
|
315 | - public function getHttpStatusCode(): int |
|
316 | - { |
|
317 | - |
|
318 | - return intval($this->http_status_code); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @return array|null |
|
323 | - */ |
|
324 | - public function getHtmlAttributions(): ?array |
|
325 | - { |
|
326 | - |
|
327 | - return $this->html_attributions; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * @param array|null $html_attributions |
|
332 | - * |
|
333 | - * @return GoogleMapsResponse |
|
334 | - */ |
|
335 | - public function setHtmlAttributions(?array $html_attributions): GoogleMapsResponse |
|
336 | - { |
|
337 | - |
|
338 | - $this->html_attributions = $html_attributions; |
|
339 | - |
|
340 | - return $this; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * @return string |
|
345 | - */ |
|
346 | - public function getNextPageToken(): string |
|
347 | - { |
|
348 | - |
|
349 | - return $this->next_page_token ?? ''; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @param $next_page_token |
|
354 | - * |
|
355 | - * @return GoogleMapsResponse |
|
356 | - */ |
|
357 | - public function setNextPageToken($next_page_token): GoogleMapsResponse |
|
358 | - { |
|
359 | - |
|
360 | - $this->next_page_token = $next_page_token; |
|
361 | - |
|
362 | - return $this; |
|
363 | - } |
|
26 | + /** |
|
27 | + * @var Response |
|
28 | + */ |
|
29 | + protected $response = null; |
|
30 | + |
|
31 | + /** |
|
32 | + * Single result |
|
33 | + * When the Places service returns results from a details request, it places them within a single result |
|
34 | + * |
|
35 | + * @var array |
|
36 | + * @see https://developers.google.com/places/web-service/details#PlaceDetailsResults |
|
37 | + */ |
|
38 | + protected $result = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * contains an array of places, with information about each. |
|
42 | + * The Places API returns up to 20 establishment results per query. |
|
43 | + * Additionally, political results may be returned which serve to identify the area of the request. |
|
44 | + * |
|
45 | + * @var array |
|
46 | + * @see https://developers.google.com/places/web-service/search#PlaceSearchResults |
|
47 | + */ |
|
48 | + protected $results = null; |
|
49 | + |
|
50 | + /** |
|
51 | + * contains metadata on the request. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + * @see https://developers.google.com/places/web-service/search#PlaceSearchStatusCodes |
|
55 | + */ |
|
56 | + protected $status = null; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + protected $error_message = null; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var array |
|
65 | + */ |
|
66 | + protected $array_response = null; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var null|array |
|
70 | + */ |
|
71 | + protected $http_status_code = null; |
|
72 | + |
|
73 | + /** |
|
74 | + * may contain a set of attributions about this listing which must be displayed to the user (some listings may not have attribution). |
|
75 | + * |
|
76 | + * @var null|array |
|
77 | + * @since 0.5.0 |
|
78 | + */ |
|
79 | + protected $html_attributions = null; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var null|string |
|
83 | + * @since 0.5.0 |
|
84 | + */ |
|
85 | + protected $next_page_token = null; |
|
86 | + |
|
87 | + /** |
|
88 | + * GoogleMapsResponse constructor. |
|
89 | + * |
|
90 | + * @param Response $response |
|
91 | + */ |
|
92 | + public function __construct(Response $response) |
|
93 | + { |
|
94 | + |
|
95 | + $this->setResponse($response); |
|
96 | + |
|
97 | + $this->parseResponse(); |
|
98 | + |
|
99 | + $this->checkHttpStatusCode(); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param Response $response |
|
104 | + * |
|
105 | + * @return GoogleMapsResponse |
|
106 | + */ |
|
107 | + public function setResponse(Response $response): GoogleMapsResponse |
|
108 | + { |
|
109 | + |
|
110 | + $this->response = $response; |
|
111 | + |
|
112 | + return $this; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @return GoogleMapsResponse |
|
117 | + * |
|
118 | + * @throws RequestException |
|
119 | + * @throws ResponseException |
|
120 | + */ |
|
121 | + protected function parseResponse(): GoogleMapsResponse |
|
122 | + { |
|
123 | + |
|
124 | + $json_response = $this->response->getBody()->getContents(); |
|
125 | + $array_response = $this->toArray($json_response); |
|
126 | + $result = null; |
|
127 | + $results = null; |
|
128 | + |
|
129 | + if (empty($array_response[GoogleMapsResponseFields::STATUS])) { |
|
130 | + throw new ResponseException('Missing "status" in GoogleMapsApi Response'); |
|
131 | + } |
|
132 | + $this->setStatus($array_response[GoogleMapsResponseFields::STATUS]); |
|
133 | + |
|
134 | + if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) { |
|
135 | + $error_message = 'Something went wrong'; |
|
136 | + if($this->getStatus() === 'INVALID_REQUEST'){ |
|
137 | + print_r($array_response);die();} |
|
138 | + if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) { |
|
139 | + $error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE]; |
|
140 | + $this->setErrorMessage($error_message); |
|
141 | + } elseif (!empty($array_response[GoogleMapsResponseFields::STATUS])) { |
|
142 | + $error_message .= ': ' . $array_response[GoogleMapsResponseFields::STATUS]; |
|
143 | + $this->setErrorMessage($error_message); |
|
144 | + } |
|
145 | + throw new RequestException($error_message); |
|
146 | + |
|
147 | + } |
|
148 | + |
|
149 | + if (!empty($array_response[GoogleMapsResponseFields::RESULTS])) { |
|
150 | + $results = $array_response[GoogleMapsResponseFields::RESULTS]; |
|
151 | + |
|
152 | + } elseif (!empty($array_response[GoogleMapsResponseFields::CANDIDATES])) { |
|
153 | + $results = $array_response[GoogleMapsResponseFields::CANDIDATES]; |
|
154 | + |
|
155 | + }elseif (!empty($array_response[GoogleMapsResponseFields::RESULT])) { |
|
156 | + $result = $array_response[GoogleMapsResponseFields::RESULT]; |
|
157 | + |
|
158 | + } else { |
|
159 | + $result = $array_response; |
|
160 | + } |
|
161 | + $this->setResult($result); |
|
162 | + $this->setResults($results); |
|
163 | + |
|
164 | + if (!empty($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS])) { |
|
165 | + $this->setHtmlAttributions($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS]); |
|
166 | + } |
|
167 | + |
|
168 | + if (!empty($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN])) { |
|
169 | + $this->setNextPageToken($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN]); |
|
170 | + } |
|
171 | + |
|
172 | + return $this; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $json_response |
|
177 | + * |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function toArray(string $json_response): array |
|
181 | + { |
|
182 | + |
|
183 | + $this->array_response = json_decode($json_response, true); |
|
184 | + |
|
185 | + return $this->array_response; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return string |
|
190 | + */ |
|
191 | + public function getStatus(): string |
|
192 | + { |
|
193 | + |
|
194 | + return $this->status; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param string $status |
|
199 | + * |
|
200 | + * @return GoogleMapsResponse |
|
201 | + */ |
|
202 | + public function setStatus(string $status) |
|
203 | + { |
|
204 | + |
|
205 | + $this->status = $status; |
|
206 | + |
|
207 | + return $this; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Check HTTP status code (silent/No exceptions!) |
|
212 | + * @return int |
|
213 | + */ |
|
214 | + protected function checkHttpStatusCode(): int |
|
215 | + { |
|
216 | + |
|
217 | + $this->http_status_code = $this->response->getStatusCode(); |
|
218 | + |
|
219 | + return $this->http_status_code; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @return array |
|
224 | + */ |
|
225 | + public function getResults() |
|
226 | + { |
|
227 | + |
|
228 | + return $this->results; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param array $results |
|
233 | + * |
|
234 | + * @return $this |
|
235 | + */ |
|
236 | + public function setResults(?array $results) |
|
237 | + { |
|
238 | + |
|
239 | + $this->results = $results; |
|
240 | + |
|
241 | + return $this; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return array |
|
246 | + * @since v0.6.0 |
|
247 | + */ |
|
248 | + public function getResult() |
|
249 | + { |
|
250 | + |
|
251 | + return $this->result; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * @param array $result |
|
256 | + * |
|
257 | + * @return $this |
|
258 | + * @since v0.6.0 |
|
259 | + */ |
|
260 | + public function setResult(?array $result) |
|
261 | + { |
|
262 | + |
|
263 | + $this->result = $result; |
|
264 | + |
|
265 | + return $this; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * @return array |
|
270 | + */ |
|
271 | + public function getArrayResponse(): array |
|
272 | + { |
|
273 | + |
|
274 | + return $this->array_response; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @param array $array_response |
|
279 | + * |
|
280 | + * @return GoogleMapsResponse |
|
281 | + */ |
|
282 | + public function setArrayResponse(array $array_response): GoogleMapsResponse |
|
283 | + { |
|
284 | + |
|
285 | + $this->array_response = $array_response; |
|
286 | + |
|
287 | + return $this; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * @return mixed |
|
292 | + */ |
|
293 | + public function getErrorMessage() |
|
294 | + { |
|
295 | + |
|
296 | + return $this->error_message; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param $error_message |
|
301 | + * |
|
302 | + * @return GoogleMapsResponse |
|
303 | + */ |
|
304 | + public function setErrorMessage($error_message): GoogleMapsResponse |
|
305 | + { |
|
306 | + |
|
307 | + $this->error_message = $error_message; |
|
308 | + |
|
309 | + return $this; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * @return int |
|
314 | + */ |
|
315 | + public function getHttpStatusCode(): int |
|
316 | + { |
|
317 | + |
|
318 | + return intval($this->http_status_code); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @return array|null |
|
323 | + */ |
|
324 | + public function getHtmlAttributions(): ?array |
|
325 | + { |
|
326 | + |
|
327 | + return $this->html_attributions; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * @param array|null $html_attributions |
|
332 | + * |
|
333 | + * @return GoogleMapsResponse |
|
334 | + */ |
|
335 | + public function setHtmlAttributions(?array $html_attributions): GoogleMapsResponse |
|
336 | + { |
|
337 | + |
|
338 | + $this->html_attributions = $html_attributions; |
|
339 | + |
|
340 | + return $this; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * @return string |
|
345 | + */ |
|
346 | + public function getNextPageToken(): string |
|
347 | + { |
|
348 | + |
|
349 | + return $this->next_page_token ?? ''; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @param $next_page_token |
|
354 | + * |
|
355 | + * @return GoogleMapsResponse |
|
356 | + */ |
|
357 | + public function setNextPageToken($next_page_token): GoogleMapsResponse |
|
358 | + { |
|
359 | + |
|
360 | + $this->next_page_token = $next_page_token; |
|
361 | + |
|
362 | + return $this; |
|
363 | + } |
|
364 | 364 | |
365 | 365 | } |
366 | 366 | \ No newline at end of file |
@@ -133,13 +133,13 @@ |
||
133 | 133 | |
134 | 134 | if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) { |
135 | 135 | $error_message = 'Something went wrong'; |
136 | - if($this->getStatus() === 'INVALID_REQUEST'){ |
|
137 | - print_r($array_response);die();} |
|
136 | + if ($this->getStatus() === 'INVALID_REQUEST') { |
|
137 | + print_r($array_response); die(); } |
|
138 | 138 | if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) { |
139 | 139 | $error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE]; |
140 | 140 | $this->setErrorMessage($error_message); |
141 | 141 | } elseif (!empty($array_response[GoogleMapsResponseFields::STATUS])) { |
142 | - $error_message .= ': ' . $array_response[GoogleMapsResponseFields::STATUS]; |
|
142 | + $error_message .= ': '.$array_response[GoogleMapsResponseFields::STATUS]; |
|
143 | 143 | $this->setErrorMessage($error_message); |
144 | 144 | } |
145 | 145 | throw new RequestException($error_message); |
@@ -30,45 +30,45 @@ |
||
30 | 30 | class TimeZoneResult extends GoogleMapsResult |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - protected $dstOffset = 0; |
|
33 | + /** |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + protected $dstOffset = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - protected $rawOffset = 0; |
|
38 | + /** |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + protected $rawOffset = 0; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $timeZoneId = ''; |
|
43 | + /** |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $timeZoneId = ''; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - protected $timeZoneName = ''; |
|
48 | + /** |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + protected $timeZoneName = ''; |
|
52 | 52 | |
53 | - /** |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - protected $typeCheck = [ |
|
57 | - GoogleMapsResultFields::DST_OFFSET => 'int', |
|
58 | - GoogleMapsResultFields::RAW_OFFSET => 'int', |
|
59 | - GoogleMapsResultFields::TIMEZONE_ID => 'string', |
|
60 | - GoogleMapsResultFields::TIMEZONE_NAME => 'string', |
|
61 | - ]; |
|
53 | + /** |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + protected $typeCheck = [ |
|
57 | + GoogleMapsResultFields::DST_OFFSET => 'int', |
|
58 | + GoogleMapsResultFields::RAW_OFFSET => 'int', |
|
59 | + GoogleMapsResultFields::TIMEZONE_ID => 'string', |
|
60 | + GoogleMapsResultFields::TIMEZONE_NAME => 'string', |
|
61 | + ]; |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param string $initial_field_name |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - protected function getFieldName(string $initial_field_name): string |
|
69 | - { |
|
63 | + /** |
|
64 | + * @param string $initial_field_name |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + protected function getFieldName(string $initial_field_name): string |
|
69 | + { |
|
70 | 70 | |
71 | - return lcfirst($initial_field_name); |
|
72 | - } |
|
71 | + return lcfirst($initial_field_name); |
|
72 | + } |
|
73 | 73 | |
74 | 74 | } |
75 | 75 | \ No newline at end of file |