@@ -54,7 +54,7 @@ |
||
| 54 | 54 | foreach ($this->typeCheck as $field_name => $field_type) { |
| 55 | 55 | if (empty($args[$field_name]) || is_null($args[$field_name])) { |
| 56 | 56 | if ($this->isFieldRequired($field_name)) { |
| 57 | - $this->addError('Missing "' . $field_name . '" in ' . static::class); |
|
| 57 | + $this->addError('Missing "'.$field_name.'" in '.static::class); |
|
| 58 | 58 | } |
| 59 | 59 | } else { |
| 60 | 60 | $this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
@@ -21,184 +21,184 @@ |
||
| 21 | 21 | abstract class AbstractObject |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $typeCheck = []; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $required = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - protected $errors = []; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * AbstractObject constructor. |
|
| 41 | - * |
|
| 42 | - * @param array $args |
|
| 43 | - */ |
|
| 44 | - public function __construct(?array $args = []) |
|
| 45 | - { |
|
| 46 | - |
|
| 47 | - if (is_null($args)) { |
|
| 48 | - $args = []; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $this->setArgs($args); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param array $args |
|
| 56 | - * |
|
| 57 | - * @throws Exception |
|
| 58 | - */ |
|
| 59 | - protected function setArgs(array $args) |
|
| 60 | - { |
|
| 61 | - |
|
| 62 | - foreach ($this->typeCheck as $field_name => $field_type) { |
|
| 63 | - if (empty($args[$field_name]) || is_null($args[$field_name])) { |
|
| 64 | - if ($this->isFieldRequired($field_name)) { |
|
| 65 | - $this->addError('Missing "' . $field_name . '" in ' . static::class); |
|
| 66 | - } |
|
| 67 | - } else { |
|
| 68 | - $this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - $this->throwErrors(); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param string $field_name |
|
| 76 | - * |
|
| 77 | - * @return bool |
|
| 78 | - */ |
|
| 79 | - protected function isFieldRequired(string $field_name): bool |
|
| 80 | - { |
|
| 81 | - |
|
| 82 | - return in_array($field_name, $this->required); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $error |
|
| 87 | - * |
|
| 88 | - * @return array |
|
| 89 | - */ |
|
| 90 | - protected function addError(string $error): array |
|
| 91 | - { |
|
| 92 | - |
|
| 93 | - array_push($this->errors, $error); |
|
| 94 | - |
|
| 95 | - return $this->errors; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $field_type |
|
| 100 | - * @param string $field_value |
|
| 101 | - * |
|
| 102 | - * @return mixed |
|
| 103 | - */ |
|
| 104 | - protected function parseFieldValue(string $field_type, $field_value) |
|
| 105 | - { |
|
| 106 | - |
|
| 107 | - switch ($field_type) { |
|
| 108 | - case 'string': |
|
| 109 | - case 'int': |
|
| 110 | - case 'float': |
|
| 111 | - case 'array': |
|
| 112 | - case 'json': |
|
| 113 | - case 'bool': |
|
| 114 | - return $field_value; |
|
| 115 | - default: |
|
| 116 | - return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @throws Exception |
|
| 122 | - */ |
|
| 123 | - protected function throwErrors() |
|
| 124 | - { |
|
| 125 | - |
|
| 126 | - if (count($this->errors)) { |
|
| 127 | - throw new Exception(implode(', ', $this->errors)); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function toJson(): string |
|
| 135 | - { |
|
| 136 | - |
|
| 137 | - return json_encode($this->toArray()); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return array |
|
| 142 | - */ |
|
| 143 | - public function toArray(): array |
|
| 144 | - { |
|
| 145 | - |
|
| 146 | - $fields = get_object_vars($this); |
|
| 147 | - |
|
| 148 | - foreach ($fields as $field_name => $field_value) { |
|
| 149 | - |
|
| 150 | - if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
|
| 151 | - $fields[$field_name] = $field_value->toArray(); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $fields; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - public function __toString(): string |
|
| 162 | - { |
|
| 163 | - |
|
| 164 | - return implode(',', $this->toArray()); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param $name |
|
| 169 | - * @param $arguments |
|
| 170 | - * |
|
| 171 | - * @return mixed |
|
| 172 | - */ |
|
| 173 | - public function __call($name, $arguments) |
|
| 174 | - { |
|
| 175 | - |
|
| 176 | - preg_match('/(?<=(g|s)et)([A-Za-z0-9])\w+/', $name, $match); |
|
| 177 | - |
|
| 178 | - $camel_field = (empty($match[0])) ? '' : $match[0]; |
|
| 179 | - |
|
| 180 | - $snake_field = $this->getFieldName($camel_field); |
|
| 181 | - |
|
| 182 | - $field_type = (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
|
| 183 | - |
|
| 184 | - if (!empty($match[1]) && $field_type) { |
|
| 185 | - switch ($match[1]) { |
|
| 186 | - case 's': |
|
| 187 | - return $this->$snake_field = $this->parseFieldValue($field_type, current($arguments)); |
|
| 188 | - case 'g': |
|
| 189 | - return $this->$snake_field; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param string $initial_field_name |
|
| 196 | - * |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - protected function getFieldName(string $initial_field_name): string |
|
| 200 | - { |
|
| 201 | - |
|
| 202 | - return camel2Snake($initial_field_name); |
|
| 203 | - } |
|
| 24 | + /** |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $typeCheck = []; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $required = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + protected $errors = []; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * AbstractObject constructor. |
|
| 41 | + * |
|
| 42 | + * @param array $args |
|
| 43 | + */ |
|
| 44 | + public function __construct(?array $args = []) |
|
| 45 | + { |
|
| 46 | + |
|
| 47 | + if (is_null($args)) { |
|
| 48 | + $args = []; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $this->setArgs($args); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param array $args |
|
| 56 | + * |
|
| 57 | + * @throws Exception |
|
| 58 | + */ |
|
| 59 | + protected function setArgs(array $args) |
|
| 60 | + { |
|
| 61 | + |
|
| 62 | + foreach ($this->typeCheck as $field_name => $field_type) { |
|
| 63 | + if (empty($args[$field_name]) || is_null($args[$field_name])) { |
|
| 64 | + if ($this->isFieldRequired($field_name)) { |
|
| 65 | + $this->addError('Missing "' . $field_name . '" in ' . static::class); |
|
| 66 | + } |
|
| 67 | + } else { |
|
| 68 | + $this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + $this->throwErrors(); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param string $field_name |
|
| 76 | + * |
|
| 77 | + * @return bool |
|
| 78 | + */ |
|
| 79 | + protected function isFieldRequired(string $field_name): bool |
|
| 80 | + { |
|
| 81 | + |
|
| 82 | + return in_array($field_name, $this->required); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $error |
|
| 87 | + * |
|
| 88 | + * @return array |
|
| 89 | + */ |
|
| 90 | + protected function addError(string $error): array |
|
| 91 | + { |
|
| 92 | + |
|
| 93 | + array_push($this->errors, $error); |
|
| 94 | + |
|
| 95 | + return $this->errors; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $field_type |
|
| 100 | + * @param string $field_value |
|
| 101 | + * |
|
| 102 | + * @return mixed |
|
| 103 | + */ |
|
| 104 | + protected function parseFieldValue(string $field_type, $field_value) |
|
| 105 | + { |
|
| 106 | + |
|
| 107 | + switch ($field_type) { |
|
| 108 | + case 'string': |
|
| 109 | + case 'int': |
|
| 110 | + case 'float': |
|
| 111 | + case 'array': |
|
| 112 | + case 'json': |
|
| 113 | + case 'bool': |
|
| 114 | + return $field_value; |
|
| 115 | + default: |
|
| 116 | + return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @throws Exception |
|
| 122 | + */ |
|
| 123 | + protected function throwErrors() |
|
| 124 | + { |
|
| 125 | + |
|
| 126 | + if (count($this->errors)) { |
|
| 127 | + throw new Exception(implode(', ', $this->errors)); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function toJson(): string |
|
| 135 | + { |
|
| 136 | + |
|
| 137 | + return json_encode($this->toArray()); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return array |
|
| 142 | + */ |
|
| 143 | + public function toArray(): array |
|
| 144 | + { |
|
| 145 | + |
|
| 146 | + $fields = get_object_vars($this); |
|
| 147 | + |
|
| 148 | + foreach ($fields as $field_name => $field_value) { |
|
| 149 | + |
|
| 150 | + if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
|
| 151 | + $fields[$field_name] = $field_value->toArray(); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $fields; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + public function __toString(): string |
|
| 162 | + { |
|
| 163 | + |
|
| 164 | + return implode(',', $this->toArray()); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param $name |
|
| 169 | + * @param $arguments |
|
| 170 | + * |
|
| 171 | + * @return mixed |
|
| 172 | + */ |
|
| 173 | + public function __call($name, $arguments) |
|
| 174 | + { |
|
| 175 | + |
|
| 176 | + preg_match('/(?<=(g|s)et)([A-Za-z0-9])\w+/', $name, $match); |
|
| 177 | + |
|
| 178 | + $camel_field = (empty($match[0])) ? '' : $match[0]; |
|
| 179 | + |
|
| 180 | + $snake_field = $this->getFieldName($camel_field); |
|
| 181 | + |
|
| 182 | + $field_type = (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
|
| 183 | + |
|
| 184 | + if (!empty($match[1]) && $field_type) { |
|
| 185 | + switch ($match[1]) { |
|
| 186 | + case 's': |
|
| 187 | + return $this->$snake_field = $this->parseFieldValue($field_type, current($arguments)); |
|
| 188 | + case 'g': |
|
| 189 | + return $this->$snake_field; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param string $initial_field_name |
|
| 196 | + * |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + protected function getFieldName(string $initial_field_name): string |
|
| 200 | + { |
|
| 201 | + |
|
| 202 | + return camel2Snake($initial_field_name); |
|
| 203 | + } |
|
| 204 | 204 | } |
@@ -58,7 +58,7 @@ |
||
| 58 | 58 | $params = []; |
| 59 | 59 | |
| 60 | 60 | foreach ($this->params as $param_name => $param_value) { |
| 61 | - $params[$param_name] = (string)$param_value; |
|
| 61 | + $params[$param_name] = (string) $param_value; |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | return http_build_query($params); |
@@ -17,80 +17,80 @@ |
||
| 17 | 17 | class GoogleMapsRequest |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - * @since 0.5.0 |
|
| 23 | - */ |
|
| 24 | - protected $endpoint = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - private $params = []; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * GoogleMapsRequest constructor. |
|
| 33 | - * |
|
| 34 | - * @param array $params |
|
| 35 | - * @param null|string $endpoint |
|
| 36 | - */ |
|
| 37 | - public function __construct(array $params = [], ?string $endpoint = null) |
|
| 38 | - { |
|
| 39 | - |
|
| 40 | - if ($params) { |
|
| 41 | - foreach ($params as $param_name => $param_value) { |
|
| 42 | - $this->setParam($param_name, $param_value); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - $this->endpoint = $endpoint; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $param_name |
|
| 51 | - * @param mixed $param_value |
|
| 52 | - * |
|
| 53 | - * @return GoogleMapsRequest |
|
| 54 | - */ |
|
| 55 | - public function setParam(string $param_name, $param_value): GoogleMapsRequest |
|
| 56 | - { |
|
| 57 | - |
|
| 58 | - $this->params[$param_name] = $param_value; |
|
| 59 | - |
|
| 60 | - return $this; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @return string |
|
| 65 | - */ |
|
| 66 | - public function getQuery(): string |
|
| 67 | - { |
|
| 68 | - |
|
| 69 | - $params = []; |
|
| 70 | - |
|
| 71 | - foreach ($this->params as $param_name => $param_value) { |
|
| 72 | - $params[$param_name] = (string)$param_value; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - return http_build_query($params); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function getParams(): array |
|
| 82 | - { |
|
| 83 | - |
|
| 84 | - return $this->params; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return null|string |
|
| 89 | - */ |
|
| 90 | - public function getEndpoint(): ?string |
|
| 91 | - { |
|
| 92 | - |
|
| 93 | - return $this->endpoint; |
|
| 94 | - } |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + * @since 0.5.0 |
|
| 23 | + */ |
|
| 24 | + protected $endpoint = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + private $params = []; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * GoogleMapsRequest constructor. |
|
| 33 | + * |
|
| 34 | + * @param array $params |
|
| 35 | + * @param null|string $endpoint |
|
| 36 | + */ |
|
| 37 | + public function __construct(array $params = [], ?string $endpoint = null) |
|
| 38 | + { |
|
| 39 | + |
|
| 40 | + if ($params) { |
|
| 41 | + foreach ($params as $param_name => $param_value) { |
|
| 42 | + $this->setParam($param_name, $param_value); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + $this->endpoint = $endpoint; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $param_name |
|
| 51 | + * @param mixed $param_value |
|
| 52 | + * |
|
| 53 | + * @return GoogleMapsRequest |
|
| 54 | + */ |
|
| 55 | + public function setParam(string $param_name, $param_value): GoogleMapsRequest |
|
| 56 | + { |
|
| 57 | + |
|
| 58 | + $this->params[$param_name] = $param_value; |
|
| 59 | + |
|
| 60 | + return $this; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @return string |
|
| 65 | + */ |
|
| 66 | + public function getQuery(): string |
|
| 67 | + { |
|
| 68 | + |
|
| 69 | + $params = []; |
|
| 70 | + |
|
| 71 | + foreach ($this->params as $param_name => $param_value) { |
|
| 72 | + $params[$param_name] = (string)$param_value; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + return http_build_query($params); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function getParams(): array |
|
| 82 | + { |
|
| 83 | + |
|
| 84 | + return $this->params; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return null|string |
|
| 89 | + */ |
|
| 90 | + public function getEndpoint(): ?string |
|
| 91 | + { |
|
| 92 | + |
|
| 93 | + return $this->endpoint; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | \ No newline at end of file |
@@ -17,13 +17,13 @@ |
||
| 17 | 17 | class LatLngBoundsFields |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * string : northeast |
|
| 22 | - */ |
|
| 23 | - const NORTHEAST = 'northeast'; |
|
| 20 | + /** |
|
| 21 | + * string : northeast |
|
| 22 | + */ |
|
| 23 | + const NORTHEAST = 'northeast'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * string : southwest |
|
| 27 | - */ |
|
| 28 | - const SOUTHWEST = 'southwest'; |
|
| 25 | + /** |
|
| 26 | + * string : southwest |
|
| 27 | + */ |
|
| 28 | + const SOUTHWEST = 'southwest'; |
|
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -17,13 +17,13 @@ |
||
| 17 | 17 | class LatLngFields |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * string - latitude |
|
| 22 | - */ |
|
| 23 | - const LAT = 'lat'; |
|
| 20 | + /** |
|
| 21 | + * string - latitude |
|
| 22 | + */ |
|
| 23 | + const LAT = 'lat'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * string - longitude |
|
| 27 | - */ |
|
| 28 | - const LNG = 'lng'; |
|
| 25 | + /** |
|
| 26 | + * string - longitude |
|
| 27 | + */ |
|
| 28 | + const LNG = 'lng'; |
|
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -17,23 +17,23 @@ |
||
| 17 | 17 | class PhotoFields |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - const HEIGHT = "height"; |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + const HEIGHT = "height"; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const WIDTH = "width"; |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const WIDTH = "width"; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - const PHOTO_REFERENCE = "photo_reference"; |
|
| 30 | + /** |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + const PHOTO_REFERENCE = "photo_reference"; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - const HTML_ATTRIBUTIONS = "html_attributions"; |
|
| 35 | + /** |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + const HTML_ATTRIBUTIONS = "html_attributions"; |
|
| 39 | 39 | } |
| 40 | 40 | \ No newline at end of file |
@@ -17,19 +17,19 @@ |
||
| 17 | 17 | class GeometryFields |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * string: location |
|
| 22 | - */ |
|
| 23 | - const LOCATION = 'location'; |
|
| 20 | + /** |
|
| 21 | + * string: location |
|
| 22 | + */ |
|
| 23 | + const LOCATION = 'location'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * string: location_type |
|
| 27 | - */ |
|
| 28 | - const LOCATION_TYPE = 'location_type'; |
|
| 25 | + /** |
|
| 26 | + * string: location_type |
|
| 27 | + */ |
|
| 28 | + const LOCATION_TYPE = 'location_type'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * string: viewport |
|
| 32 | - */ |
|
| 33 | - const VIEWPORT = 'viewport'; |
|
| 30 | + /** |
|
| 31 | + * string: viewport |
|
| 32 | + */ |
|
| 33 | + const VIEWPORT = 'viewport'; |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -17,19 +17,19 @@ |
||
| 17 | 17 | class AddressComponentFields |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * string: long_name |
|
| 22 | - */ |
|
| 23 | - const LONG_NAME = 'long_name'; |
|
| 20 | + /** |
|
| 21 | + * string: long_name |
|
| 22 | + */ |
|
| 23 | + const LONG_NAME = 'long_name'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * string: short_name |
|
| 27 | - */ |
|
| 28 | - const SHORT_NAME = 'short_name'; |
|
| 25 | + /** |
|
| 26 | + * string: short_name |
|
| 27 | + */ |
|
| 28 | + const SHORT_NAME = 'short_name'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * string: types |
|
| 32 | - */ |
|
| 33 | - const TYPES = 'types'; |
|
| 30 | + /** |
|
| 31 | + * string: types |
|
| 32 | + */ |
|
| 33 | + const TYPES = 'types'; |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -73,13 +73,13 @@ |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | if (is_array($locations)) { |
| 76 | - $locations = implode('|', array_map(function ($item) { |
|
| 76 | + $locations = implode('|', array_map(function($item) { |
|
| 77 | 77 | |
| 78 | - return (string)$item; |
|
| 78 | + return (string) $item; |
|
| 79 | 79 | }, $locations)); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - return (string)$locations; |
|
| 82 | + return (string) $locations; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -28,92 +28,92 @@ |
||
| 28 | 28 | class Elevation extends Api |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - const SERVICE_ENDPOINT = 'elevation'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $result_collection_type = ElevationResultsCollection::class; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Positional Requests |
|
| 43 | - * |
|
| 44 | - * @param LatLng|string|array $locations |
|
| 45 | - * This parameter takes either a single location or multiple locations passed as an array or as an encoded polyline |
|
| 46 | - * |
|
| 47 | - * @return GoogleMapsResultsCollection |
|
| 48 | - * |
|
| 49 | - * @since 0.3.0 |
|
| 50 | - */ |
|
| 51 | - public function getByLocations($locations): GoogleMapsResultsCollection |
|
| 52 | - { |
|
| 53 | - |
|
| 54 | - $locations = $this->parseLocations($locations); |
|
| 55 | - |
|
| 56 | - return $this->callApi([ |
|
| 57 | - GoogleMapsRequestFields::LOCATIONS => $locations |
|
| 58 | - ]); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param array|string $locations |
|
| 63 | - * |
|
| 64 | - * @return string |
|
| 65 | - * |
|
| 66 | - * @since 0.3.0 |
|
| 67 | - */ |
|
| 68 | - public function parseLocations($locations): string |
|
| 69 | - { |
|
| 70 | - |
|
| 71 | - if ($locations instanceof Path) { |
|
| 72 | - $locations = $locations->toArray(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if (is_array($locations)) { |
|
| 76 | - $locations = implode('|', array_map(function ($item) { |
|
| 77 | - |
|
| 78 | - return (string)$item; |
|
| 79 | - }, $locations)); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return (string)$locations; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Sampled Path Requests |
|
| 87 | - * |
|
| 88 | - * @param array|string $path |
|
| 89 | - * This parameter takes either a multiple locations passed as an array or as an encoded polyline |
|
| 90 | - * |
|
| 91 | - * @param int $samples |
|
| 92 | - * This will be the number of results as well |
|
| 93 | - * |
|
| 94 | - * @throws InvalidArgumentException |
|
| 95 | - * @return GoogleMapsResultsCollection |
|
| 96 | - * |
|
| 97 | - * @since 0.4.0 |
|
| 98 | - */ |
|
| 99 | - public function getBySampledPath($path, int $samples): GoogleMapsResultsCollection |
|
| 100 | - { |
|
| 101 | - |
|
| 102 | - if ((is_array($path) && count($path) < 2) || |
|
| 103 | - $path instanceof Path && $path->count() < 2) { |
|
| 104 | - throw new InvalidArgumentException('The number of items provided in the path must be greater than 1 (One)'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($samples <= 0) { |
|
| 108 | - throw new InvalidArgumentException('The number of samples must be greater than 0 (Zero)'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $path = $this->parseLocations($path); |
|
| 112 | - |
|
| 113 | - return $this->callApi([ |
|
| 114 | - GoogleMapsRequestFields::PATH => $path, |
|
| 115 | - GoogleMapsRequestFields::SAMPLES => $samples, |
|
| 116 | - ]); |
|
| 117 | - } |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + const SERVICE_ENDPOINT = 'elevation'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $result_collection_type = ElevationResultsCollection::class; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Positional Requests |
|
| 43 | + * |
|
| 44 | + * @param LatLng|string|array $locations |
|
| 45 | + * This parameter takes either a single location or multiple locations passed as an array or as an encoded polyline |
|
| 46 | + * |
|
| 47 | + * @return GoogleMapsResultsCollection |
|
| 48 | + * |
|
| 49 | + * @since 0.3.0 |
|
| 50 | + */ |
|
| 51 | + public function getByLocations($locations): GoogleMapsResultsCollection |
|
| 52 | + { |
|
| 53 | + |
|
| 54 | + $locations = $this->parseLocations($locations); |
|
| 55 | + |
|
| 56 | + return $this->callApi([ |
|
| 57 | + GoogleMapsRequestFields::LOCATIONS => $locations |
|
| 58 | + ]); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param array|string $locations |
|
| 63 | + * |
|
| 64 | + * @return string |
|
| 65 | + * |
|
| 66 | + * @since 0.3.0 |
|
| 67 | + */ |
|
| 68 | + public function parseLocations($locations): string |
|
| 69 | + { |
|
| 70 | + |
|
| 71 | + if ($locations instanceof Path) { |
|
| 72 | + $locations = $locations->toArray(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if (is_array($locations)) { |
|
| 76 | + $locations = implode('|', array_map(function ($item) { |
|
| 77 | + |
|
| 78 | + return (string)$item; |
|
| 79 | + }, $locations)); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return (string)$locations; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Sampled Path Requests |
|
| 87 | + * |
|
| 88 | + * @param array|string $path |
|
| 89 | + * This parameter takes either a multiple locations passed as an array or as an encoded polyline |
|
| 90 | + * |
|
| 91 | + * @param int $samples |
|
| 92 | + * This will be the number of results as well |
|
| 93 | + * |
|
| 94 | + * @throws InvalidArgumentException |
|
| 95 | + * @return GoogleMapsResultsCollection |
|
| 96 | + * |
|
| 97 | + * @since 0.4.0 |
|
| 98 | + */ |
|
| 99 | + public function getBySampledPath($path, int $samples): GoogleMapsResultsCollection |
|
| 100 | + { |
|
| 101 | + |
|
| 102 | + if ((is_array($path) && count($path) < 2) || |
|
| 103 | + $path instanceof Path && $path->count() < 2) { |
|
| 104 | + throw new InvalidArgumentException('The number of items provided in the path must be greater than 1 (One)'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($samples <= 0) { |
|
| 108 | + throw new InvalidArgumentException('The number of samples must be greater than 0 (Zero)'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $path = $this->parseLocations($path); |
|
| 112 | + |
|
| 113 | + return $this->callApi([ |
|
| 114 | + GoogleMapsRequestFields::PATH => $path, |
|
| 115 | + GoogleMapsRequestFields::SAMPLES => $samples, |
|
| 116 | + ]); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |
| 120 | 120 | \ No newline at end of file |
@@ -25,279 +25,279 @@ |
||
| 25 | 25 | class GoogleMapsApi |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var GoogleMapsApi |
|
| 30 | - */ |
|
| 31 | - protected static $instance = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var GoogleMapsRequest |
|
| 35 | - */ |
|
| 36 | - protected $request = null; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Google Maps Geocode Service API url |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - private $api_url = "https://maps.googleapis.com/maps/api/"; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * your own Google Maps API key |
|
| 46 | - * @var string |
|
| 47 | - * @see https://developers.google.com/maps/documentation/javascript/get-api-key |
|
| 48 | - */ |
|
| 49 | - private $key = ''; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Google Maps API sensor |
|
| 53 | - * @var string - true|false |
|
| 54 | - */ |
|
| 55 | - private $sensor = 'false'; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Google Maps API service name |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private $service_endpoint = ''; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - private $response = null; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @var GoogleMapsClient |
|
| 70 | - */ |
|
| 71 | - private $client = null; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - private $type = null; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * GoogleMapsApi constructor. |
|
| 80 | - * |
|
| 81 | - * @param array $config |
|
| 82 | - */ |
|
| 83 | - public function __construct(array $config = []) |
|
| 84 | - { |
|
| 85 | - |
|
| 86 | - // Set "API key" |
|
| 87 | - $key = (empty($config[GoogleMapsApiConfigFields::KEY])) ? '' : $config[GoogleMapsApiConfigFields::KEY]; |
|
| 88 | - $this->setKey($key); |
|
| 89 | - |
|
| 90 | - // Set "sensor" |
|
| 91 | - $sensor = (empty($config[GoogleMapsApiConfigFields::SENSOR])) ? SensorValues::FALSE : $config[GoogleMapsApiConfigFields::SENSOR]; |
|
| 92 | - $this->setSensor($sensor); |
|
| 93 | - |
|
| 94 | - // Set the endpoint |
|
| 95 | - $service_endpoint = (empty($config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT])) ? '' : $config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT]; |
|
| 96 | - $this->setServiceEndpoint($service_endpoint); |
|
| 97 | - |
|
| 98 | - // Set Client |
|
| 99 | - $this->setClient(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - public function getApiUrl(): string |
|
| 106 | - { |
|
| 107 | - |
|
| 108 | - return $this->api_url; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Perform the Google Maps API call |
|
| 113 | - * |
|
| 114 | - * @param GoogleMapsRequest $request |
|
| 115 | - * |
|
| 116 | - * @return Http\GoogleMapsResponse|string |
|
| 117 | - */ |
|
| 118 | - public function get(GoogleMapsRequest $request) |
|
| 119 | - { |
|
| 120 | - |
|
| 121 | - $this->setRequest($request); |
|
| 122 | - |
|
| 123 | - $url = $this->getUrl(); |
|
| 124 | - |
|
| 125 | - $query = $this->getQuery(); |
|
| 126 | - |
|
| 127 | - $this->response = $this->getClient()->get($url, $query); |
|
| 128 | - |
|
| 129 | - return $this->response; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return string |
|
| 134 | - * @throws RequestException |
|
| 135 | - */ |
|
| 136 | - public function getUrl(): string |
|
| 137 | - { |
|
| 138 | - |
|
| 139 | - $url_chunks = []; |
|
| 140 | - $service_endpoint = $this->getServiceEndpoint(); |
|
| 141 | - if (!$service_endpoint) { |
|
| 142 | - throw new RequestException('Service name missing!'); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $request_endpoint = $this->request->getEndpoint(); |
|
| 146 | - array_push($url_chunks, $this->api_url . $service_endpoint); |
|
| 147 | - |
|
| 148 | - if($request_endpoint) { |
|
| 149 | - array_push($url_chunks, $request_endpoint); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - array_push($url_chunks, GoogleMapsResponseFormat::JSON); |
|
| 153 | - return implode("/", $url_chunks); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function getServiceEndpoint(): string |
|
| 160 | - { |
|
| 161 | - |
|
| 162 | - return $this->service_endpoint; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param string $service_endpoint |
|
| 167 | - * |
|
| 168 | - * @return GoogleMapsApi |
|
| 169 | - */ |
|
| 170 | - public function setServiceEndpoint(string $service_endpoint): GoogleMapsApi |
|
| 171 | - { |
|
| 172 | - |
|
| 173 | - $this->service_endpoint = $service_endpoint; |
|
| 174 | - |
|
| 175 | - return $this; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function getQuery(): string |
|
| 182 | - { |
|
| 183 | - |
|
| 184 | - $api_query = http_build_query([ |
|
| 185 | - GoogleMapsRequestFields::KEY => $this->getKey(), |
|
| 186 | - GoogleMapsRequestFields::SENSOR => $this->getSensor(), |
|
| 187 | - ]); |
|
| 188 | - |
|
| 189 | - $request_query = $this->getRequest()->getQuery(); |
|
| 190 | - |
|
| 191 | - return implode('&', [ |
|
| 192 | - $api_query, |
|
| 193 | - $request_query |
|
| 194 | - ]); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return string |
|
| 199 | - */ |
|
| 200 | - public function getKey(): string |
|
| 201 | - { |
|
| 202 | - |
|
| 203 | - return $this->key; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param string $key |
|
| 208 | - * |
|
| 209 | - * @return GoogleMapsApi |
|
| 210 | - */ |
|
| 211 | - public function setKey(string $key): GoogleMapsApi |
|
| 212 | - { |
|
| 213 | - |
|
| 214 | - $this->key = $key; |
|
| 215 | - |
|
| 216 | - return $this; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - public function getSensor(): string |
|
| 223 | - { |
|
| 224 | - |
|
| 225 | - return $this->sensor ? 'true' : 'false'; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Set sensor parameter |
|
| 230 | - * |
|
| 231 | - * @param bool|string $sensor |
|
| 232 | - * |
|
| 233 | - * @return GoogleMapsApi |
|
| 234 | - */ |
|
| 235 | - public function setSensor($sensor): GoogleMapsApi |
|
| 236 | - { |
|
| 237 | - |
|
| 238 | - if ($sensor !== SensorValues::FALSE) { |
|
| 239 | - $sensor = SensorValues::TRUE; |
|
| 240 | - } |
|
| 241 | - $this->sensor = $sensor; |
|
| 242 | - |
|
| 243 | - return $this; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @return GoogleMapsRequest |
|
| 248 | - */ |
|
| 249 | - public function getRequest(): GoogleMapsRequest |
|
| 250 | - { |
|
| 251 | - |
|
| 252 | - return $this->request; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @param GoogleMapsRequest $request |
|
| 257 | - * |
|
| 258 | - * @return GoogleMapsApi |
|
| 259 | - */ |
|
| 260 | - public function setRequest(GoogleMapsRequest $request) |
|
| 261 | - { |
|
| 262 | - |
|
| 263 | - $this->request = $request; |
|
| 264 | - |
|
| 265 | - return $this; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @return GoogleMapsClient |
|
| 270 | - */ |
|
| 271 | - public function getClient() |
|
| 272 | - { |
|
| 273 | - |
|
| 274 | - return $this->client; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param GoogleMapsClient|null $client |
|
| 279 | - * |
|
| 280 | - * @return GoogleMapsApi |
|
| 281 | - */ |
|
| 282 | - public function setClient(?GoogleMapsClient $client = null): GoogleMapsApi |
|
| 283 | - { |
|
| 284 | - |
|
| 285 | - $this->client = $client ?? new GoogleMapsClient(); |
|
| 286 | - |
|
| 287 | - return $this; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param string $type |
|
| 292 | - * |
|
| 293 | - * @return GoogleMapsApi |
|
| 294 | - */ |
|
| 295 | - protected function setType(string $type): GoogleMapsApi |
|
| 296 | - { |
|
| 297 | - |
|
| 298 | - $this->type = $type; |
|
| 299 | - |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 28 | + /** |
|
| 29 | + * @var GoogleMapsApi |
|
| 30 | + */ |
|
| 31 | + protected static $instance = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var GoogleMapsRequest |
|
| 35 | + */ |
|
| 36 | + protected $request = null; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Google Maps Geocode Service API url |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + private $api_url = "https://maps.googleapis.com/maps/api/"; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * your own Google Maps API key |
|
| 46 | + * @var string |
|
| 47 | + * @see https://developers.google.com/maps/documentation/javascript/get-api-key |
|
| 48 | + */ |
|
| 49 | + private $key = ''; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Google Maps API sensor |
|
| 53 | + * @var string - true|false |
|
| 54 | + */ |
|
| 55 | + private $sensor = 'false'; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Google Maps API service name |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private $service_endpoint = ''; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + private $response = null; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @var GoogleMapsClient |
|
| 70 | + */ |
|
| 71 | + private $client = null; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + private $type = null; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * GoogleMapsApi constructor. |
|
| 80 | + * |
|
| 81 | + * @param array $config |
|
| 82 | + */ |
|
| 83 | + public function __construct(array $config = []) |
|
| 84 | + { |
|
| 85 | + |
|
| 86 | + // Set "API key" |
|
| 87 | + $key = (empty($config[GoogleMapsApiConfigFields::KEY])) ? '' : $config[GoogleMapsApiConfigFields::KEY]; |
|
| 88 | + $this->setKey($key); |
|
| 89 | + |
|
| 90 | + // Set "sensor" |
|
| 91 | + $sensor = (empty($config[GoogleMapsApiConfigFields::SENSOR])) ? SensorValues::FALSE : $config[GoogleMapsApiConfigFields::SENSOR]; |
|
| 92 | + $this->setSensor($sensor); |
|
| 93 | + |
|
| 94 | + // Set the endpoint |
|
| 95 | + $service_endpoint = (empty($config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT])) ? '' : $config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT]; |
|
| 96 | + $this->setServiceEndpoint($service_endpoint); |
|
| 97 | + |
|
| 98 | + // Set Client |
|
| 99 | + $this->setClient(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + public function getApiUrl(): string |
|
| 106 | + { |
|
| 107 | + |
|
| 108 | + return $this->api_url; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Perform the Google Maps API call |
|
| 113 | + * |
|
| 114 | + * @param GoogleMapsRequest $request |
|
| 115 | + * |
|
| 116 | + * @return Http\GoogleMapsResponse|string |
|
| 117 | + */ |
|
| 118 | + public function get(GoogleMapsRequest $request) |
|
| 119 | + { |
|
| 120 | + |
|
| 121 | + $this->setRequest($request); |
|
| 122 | + |
|
| 123 | + $url = $this->getUrl(); |
|
| 124 | + |
|
| 125 | + $query = $this->getQuery(); |
|
| 126 | + |
|
| 127 | + $this->response = $this->getClient()->get($url, $query); |
|
| 128 | + |
|
| 129 | + return $this->response; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return string |
|
| 134 | + * @throws RequestException |
|
| 135 | + */ |
|
| 136 | + public function getUrl(): string |
|
| 137 | + { |
|
| 138 | + |
|
| 139 | + $url_chunks = []; |
|
| 140 | + $service_endpoint = $this->getServiceEndpoint(); |
|
| 141 | + if (!$service_endpoint) { |
|
| 142 | + throw new RequestException('Service name missing!'); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $request_endpoint = $this->request->getEndpoint(); |
|
| 146 | + array_push($url_chunks, $this->api_url . $service_endpoint); |
|
| 147 | + |
|
| 148 | + if($request_endpoint) { |
|
| 149 | + array_push($url_chunks, $request_endpoint); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + array_push($url_chunks, GoogleMapsResponseFormat::JSON); |
|
| 153 | + return implode("/", $url_chunks); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function getServiceEndpoint(): string |
|
| 160 | + { |
|
| 161 | + |
|
| 162 | + return $this->service_endpoint; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param string $service_endpoint |
|
| 167 | + * |
|
| 168 | + * @return GoogleMapsApi |
|
| 169 | + */ |
|
| 170 | + public function setServiceEndpoint(string $service_endpoint): GoogleMapsApi |
|
| 171 | + { |
|
| 172 | + |
|
| 173 | + $this->service_endpoint = $service_endpoint; |
|
| 174 | + |
|
| 175 | + return $this; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function getQuery(): string |
|
| 182 | + { |
|
| 183 | + |
|
| 184 | + $api_query = http_build_query([ |
|
| 185 | + GoogleMapsRequestFields::KEY => $this->getKey(), |
|
| 186 | + GoogleMapsRequestFields::SENSOR => $this->getSensor(), |
|
| 187 | + ]); |
|
| 188 | + |
|
| 189 | + $request_query = $this->getRequest()->getQuery(); |
|
| 190 | + |
|
| 191 | + return implode('&', [ |
|
| 192 | + $api_query, |
|
| 193 | + $request_query |
|
| 194 | + ]); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return string |
|
| 199 | + */ |
|
| 200 | + public function getKey(): string |
|
| 201 | + { |
|
| 202 | + |
|
| 203 | + return $this->key; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param string $key |
|
| 208 | + * |
|
| 209 | + * @return GoogleMapsApi |
|
| 210 | + */ |
|
| 211 | + public function setKey(string $key): GoogleMapsApi |
|
| 212 | + { |
|
| 213 | + |
|
| 214 | + $this->key = $key; |
|
| 215 | + |
|
| 216 | + return $this; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + public function getSensor(): string |
|
| 223 | + { |
|
| 224 | + |
|
| 225 | + return $this->sensor ? 'true' : 'false'; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Set sensor parameter |
|
| 230 | + * |
|
| 231 | + * @param bool|string $sensor |
|
| 232 | + * |
|
| 233 | + * @return GoogleMapsApi |
|
| 234 | + */ |
|
| 235 | + public function setSensor($sensor): GoogleMapsApi |
|
| 236 | + { |
|
| 237 | + |
|
| 238 | + if ($sensor !== SensorValues::FALSE) { |
|
| 239 | + $sensor = SensorValues::TRUE; |
|
| 240 | + } |
|
| 241 | + $this->sensor = $sensor; |
|
| 242 | + |
|
| 243 | + return $this; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @return GoogleMapsRequest |
|
| 248 | + */ |
|
| 249 | + public function getRequest(): GoogleMapsRequest |
|
| 250 | + { |
|
| 251 | + |
|
| 252 | + return $this->request; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @param GoogleMapsRequest $request |
|
| 257 | + * |
|
| 258 | + * @return GoogleMapsApi |
|
| 259 | + */ |
|
| 260 | + public function setRequest(GoogleMapsRequest $request) |
|
| 261 | + { |
|
| 262 | + |
|
| 263 | + $this->request = $request; |
|
| 264 | + |
|
| 265 | + return $this; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @return GoogleMapsClient |
|
| 270 | + */ |
|
| 271 | + public function getClient() |
|
| 272 | + { |
|
| 273 | + |
|
| 274 | + return $this->client; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param GoogleMapsClient|null $client |
|
| 279 | + * |
|
| 280 | + * @return GoogleMapsApi |
|
| 281 | + */ |
|
| 282 | + public function setClient(?GoogleMapsClient $client = null): GoogleMapsApi |
|
| 283 | + { |
|
| 284 | + |
|
| 285 | + $this->client = $client ?? new GoogleMapsClient(); |
|
| 286 | + |
|
| 287 | + return $this; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param string $type |
|
| 292 | + * |
|
| 293 | + * @return GoogleMapsApi |
|
| 294 | + */ |
|
| 295 | + protected function setType(string $type): GoogleMapsApi |
|
| 296 | + { |
|
| 297 | + |
|
| 298 | + $this->type = $type; |
|
| 299 | + |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | 303 | } |
@@ -143,9 +143,9 @@ |
||
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | $request_endpoint = $this->request->getEndpoint(); |
| 146 | - array_push($url_chunks, $this->api_url . $service_endpoint); |
|
| 146 | + array_push($url_chunks, $this->api_url.$service_endpoint); |
|
| 147 | 147 | |
| 148 | - if($request_endpoint) { |
|
| 148 | + if ($request_endpoint) { |
|
| 149 | 149 | array_push($url_chunks, $request_endpoint); |
| 150 | 150 | } |
| 151 | 151 | |