@@ -15,33 +15,33 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ResponseCluster extends Common |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Cluster of addresses |
|
| 20 | - */ |
|
| 21 | - public ?Cluster $cluster = null; |
|
| 18 | + /** |
|
| 19 | + * Cluster of addresses |
|
| 20 | + */ |
|
| 21 | + public ?Cluster $cluster = null; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A number of the returned addresses. |
|
| 25 | - * @example 1 |
|
| 26 | - */ |
|
| 27 | - public ?int $address_count = null; |
|
| 23 | + /** |
|
| 24 | + * A number of the returned addresses. |
|
| 25 | + * @example 1 |
|
| 26 | + */ |
|
| 27 | + public ?int $address_count = null; |
|
| 28 | 28 | |
| 29 | - public function __construct(?array $params = null) |
|
| 30 | - { |
|
| 31 | - if ($params !== null) { |
|
| 32 | - foreach ($this as $key => $value) { |
|
| 33 | - if (isset($params[$key])) { |
|
| 34 | - if ($key === 'cluster') { |
|
| 35 | - if (is_array($params[$key])) { |
|
| 36 | - $this->{$key} = new Cluster($params[$key]); |
|
| 37 | - } elseif (is_object($params[$key]) && $params[$key] instanceof Cluster) { |
|
| 38 | - $this->{$key} = $params[$key]; |
|
| 39 | - } |
|
| 40 | - } else { |
|
| 41 | - $this->{$key} = $params[$key]; |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - } |
|
| 29 | + public function __construct(?array $params = null) |
|
| 30 | + { |
|
| 31 | + if ($params !== null) { |
|
| 32 | + foreach ($this as $key => $value) { |
|
| 33 | + if (isset($params[$key])) { |
|
| 34 | + if ($key === 'cluster') { |
|
| 35 | + if (is_array($params[$key])) { |
|
| 36 | + $this->{$key} = new Cluster($params[$key]); |
|
| 37 | + } elseif (is_object($params[$key]) && $params[$key] instanceof Cluster) { |
|
| 38 | + $this->{$key} = $params[$key]; |
|
| 39 | + } |
|
| 40 | + } else { |
|
| 41 | + $this->{$key} = $params[$key]; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -28,10 +28,10 @@ |
||
| 28 | 28 | |
| 29 | 29 | public function __construct(?array $params = null) |
| 30 | 30 | { |
| 31 | - if ($params !== null) { |
|
| 31 | + if ($params!==null) { |
|
| 32 | 32 | foreach ($this as $key => $value) { |
| 33 | 33 | if (isset($params[$key])) { |
| 34 | - if ($key === 'cluster') { |
|
| 34 | + if ($key==='cluster') { |
|
| 35 | 35 | if (is_array($params[$key])) { |
| 36 | 36 | $this->{$key} = new Cluster($params[$key]); |
| 37 | 37 | } elseif (is_object($params[$key]) && $params[$key] instanceof Cluster) { |
@@ -13,35 +13,35 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Cluster extends Common |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Geohash |
|
| 18 | - * @example 1mr8h4r9 |
|
| 19 | - */ |
|
| 20 | - public ?string $geohash = null; |
|
| 16 | + /** |
|
| 17 | + * Geohash |
|
| 18 | + * @example 1mr8h4r9 |
|
| 19 | + */ |
|
| 20 | + public ?string $geohash = null; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Latitude |
|
| 24 | - * @example -60.456132888793945 |
|
| 25 | - */ |
|
| 26 | - public ?float $lat = null; |
|
| 22 | + /** |
|
| 23 | + * Latitude |
|
| 24 | + * @example -60.456132888793945 |
|
| 25 | + */ |
|
| 26 | + public ?float $lat = null; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Longitude |
|
| 30 | - * @example -60.456132888793945 |
|
| 31 | - */ |
|
| 32 | - public ?float $lng = null; |
|
| 28 | + /** |
|
| 29 | + * Longitude |
|
| 30 | + * @example -60.456132888793945 |
|
| 31 | + */ |
|
| 32 | + public ?float $lng = null; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Boundary box, array of pairs of floats. |
|
| 36 | - * @var float[][] |
|
| 37 | - * @example [[52.294921875, 5.2294921875], [52.3388671875, 5.2734375]] |
|
| 38 | - */ |
|
| 39 | - public ?array $bbox = null; |
|
| 34 | + /** |
|
| 35 | + * Boundary box, array of pairs of floats. |
|
| 36 | + * @var float[][] |
|
| 37 | + * @example [[52.294921875, 5.2294921875], [52.3388671875, 5.2734375]] |
|
| 38 | + */ |
|
| 39 | + public ?array $bbox = null; |
|
| 40 | 40 | |
| 41 | - public function __construct(?array $params = null) |
|
| 42 | - { |
|
| 43 | - if ($params !== null) { |
|
| 44 | - $this->fillFromArray($params); |
|
| 45 | - } |
|
| 46 | - } |
|
| 41 | + public function __construct(?array $params = null) |
|
| 42 | + { |
|
| 43 | + if ($params !== null) { |
|
| 44 | + $this->fillFromArray($params); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | |
| 36 | 36 | public function __construct(?array $params = null) |
| 37 | 37 | { |
| 38 | - if ($params !== null) { |
|
| 38 | + if ($params!==null) { |
|
| 39 | 39 | $this->fillFromArray($params); |
| 40 | 40 | } |
| 41 | 41 | } |
@@ -13,27 +13,27 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ScheduleItem extends Common |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * If true, the schedule is enabled. |
|
| 18 | - */ |
|
| 19 | - public ?bool $enable = null; |
|
| 16 | + /** |
|
| 17 | + * If true, the schedule is enabled. |
|
| 18 | + */ |
|
| 19 | + public ?bool $enable = null; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Schedule mode. |
|
| 23 | - * @example 'monthly' |
|
| 24 | - */ |
|
| 25 | - public ?string $mode = null; |
|
| 21 | + /** |
|
| 22 | + * Schedule mode. |
|
| 23 | + * @example 'monthly' |
|
| 24 | + */ |
|
| 25 | + public ?string $mode = null; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Monthly properties. |
|
| 29 | - * @example ['every' => 1] |
|
| 30 | - */ |
|
| 31 | - public ?array $monthly = null; |
|
| 27 | + /** |
|
| 28 | + * Monthly properties. |
|
| 29 | + * @example ['every' => 1] |
|
| 30 | + */ |
|
| 31 | + public ?array $monthly = null; |
|
| 32 | 32 | |
| 33 | - public function __construct(?array $params = null) |
|
| 34 | - { |
|
| 35 | - if ($params !== null) { |
|
| 36 | - $this->fillFromArray($params); |
|
| 37 | - } |
|
| 38 | - } |
|
| 33 | + public function __construct(?array $params = null) |
|
| 34 | + { |
|
| 35 | + if ($params !== null) { |
|
| 36 | + $this->fillFromArray($params); |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | |
| 36 | 36 | public function __construct(?array $params = null) |
| 37 | 37 | { |
| 38 | - if ($params !== null) { |
|
| 38 | + if ($params!==null) { |
|
| 39 | 39 | $this->fillFromArray($params); |
| 40 | 40 | } |
| 41 | 41 | } |
@@ -14,34 +14,34 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Address extends ResponseAddress |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * @param array|string $params_or_address_1 - The route Address Line 1 or array of Address's values. |
|
| 19 | - * @param float $cached_lat - A latitude of the contact's cached position. |
|
| 20 | - * @param float $cached_lng - A longitude of the contact's cached position. |
|
| 21 | - * @param string $address_stop_type - The contact's stop type. available types: |
|
| 22 | - * 'DELIVERY', 'PICKUP', 'BREAK', 'MEETUP', |
|
| 23 | - * 'SERVICE', 'VISIT', 'DRIVEBY' |
|
| 24 | - */ |
|
| 25 | - public function __construct( |
|
| 26 | - $params_or_address_1, |
|
| 27 | - ?float $cached_lat = null, |
|
| 28 | - ?float $cached_lng = null, |
|
| 29 | - ?string $address_stop_type = null |
|
| 30 | - ) { |
|
| 31 | - if (is_array($params_or_address_1) && isset($params_or_address_1['address_1']) |
|
| 32 | - && isset($params_or_address_1['cached_lat']) && isset($params_or_address_1['cached_lng']) |
|
| 33 | - && isset($params_or_address_1['address_stop_type']) |
|
| 34 | - ) { |
|
| 35 | - parent::__construct($params_or_address_1); |
|
| 36 | - } elseif (is_string($params_or_address_1) && $params_or_address_1 != "" |
|
| 37 | - && !is_null($cached_lat) && !is_null($cached_lng) && !is_null($address_stop_type) |
|
| 38 | - ) { |
|
| 39 | - $this->address_1 = $params_or_address_1; |
|
| 40 | - $this->cached_lat = $cached_lat; |
|
| 41 | - $this->cached_lng = $cached_lng; |
|
| 42 | - $this->address_stop_type = $address_stop_type; |
|
| 43 | - } else { |
|
| 44 | - throw new ApiError('The fields address_1, cached_lat, cached_lng, address_stop_type are required.'); |
|
| 45 | - } |
|
| 46 | - } |
|
| 17 | + /** |
|
| 18 | + * @param array|string $params_or_address_1 - The route Address Line 1 or array of Address's values. |
|
| 19 | + * @param float $cached_lat - A latitude of the contact's cached position. |
|
| 20 | + * @param float $cached_lng - A longitude of the contact's cached position. |
|
| 21 | + * @param string $address_stop_type - The contact's stop type. available types: |
|
| 22 | + * 'DELIVERY', 'PICKUP', 'BREAK', 'MEETUP', |
|
| 23 | + * 'SERVICE', 'VISIT', 'DRIVEBY' |
|
| 24 | + */ |
|
| 25 | + public function __construct( |
|
| 26 | + $params_or_address_1, |
|
| 27 | + ?float $cached_lat = null, |
|
| 28 | + ?float $cached_lng = null, |
|
| 29 | + ?string $address_stop_type = null |
|
| 30 | + ) { |
|
| 31 | + if (is_array($params_or_address_1) && isset($params_or_address_1['address_1']) |
|
| 32 | + && isset($params_or_address_1['cached_lat']) && isset($params_or_address_1['cached_lng']) |
|
| 33 | + && isset($params_or_address_1['address_stop_type']) |
|
| 34 | + ) { |
|
| 35 | + parent::__construct($params_or_address_1); |
|
| 36 | + } elseif (is_string($params_or_address_1) && $params_or_address_1 != "" |
|
| 37 | + && !is_null($cached_lat) && !is_null($cached_lng) && !is_null($address_stop_type) |
|
| 38 | + ) { |
|
| 39 | + $this->address_1 = $params_or_address_1; |
|
| 40 | + $this->cached_lat = $cached_lat; |
|
| 41 | + $this->cached_lng = $cached_lng; |
|
| 42 | + $this->address_stop_type = $address_stop_type; |
|
| 43 | + } else { |
|
| 44 | + throw new ApiError('The fields address_1, cached_lat, cached_lng, address_stop_type are required.'); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -33,7 +33,7 @@ |
||
| 33 | 33 | && isset($params_or_address_1['address_stop_type']) |
| 34 | 34 | ) { |
| 35 | 35 | parent::__construct($params_or_address_1); |
| 36 | - } elseif (is_string($params_or_address_1) && $params_or_address_1 != "" |
|
| 36 | + } elseif (is_string($params_or_address_1) && $params_or_address_1!="" |
|
| 37 | 37 | && !is_null($cached_lat) && !is_null($cached_lng) && !is_null($address_stop_type) |
| 38 | 38 | ) { |
| 39 | 39 | $this->address_1 = $params_or_address_1; |
@@ -13,37 +13,37 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class StatusChecker extends Common |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * HTTP code. |
|
| 18 | - * @example 200, 202 |
|
| 19 | - */ |
|
| 20 | - public ?int $code = null; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Requested data if request was handled synchronously. |
|
| 24 | - * @example ['status' => 1] - Successful operation |
|
| 25 | - */ |
|
| 26 | - public ?array $data = null; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Path to the status checker if request was handled asynchronously. |
|
| 30 | - */ |
|
| 31 | - public ?string $location = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Job Id to check status if request was handled asynchronously. |
|
| 35 | - */ |
|
| 36 | - public ?string $x_job_id = null; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Job running time in seconds. |
|
| 40 | - */ |
|
| 41 | - public ?string $x_r4m_async_job_running_time = null; |
|
| 42 | - |
|
| 43 | - public function __construct(?array $params = null) |
|
| 44 | - { |
|
| 45 | - if ($params !== null) { |
|
| 46 | - $this->fillFromArray($params); |
|
| 47 | - } |
|
| 48 | - } |
|
| 16 | + /** |
|
| 17 | + * HTTP code. |
|
| 18 | + * @example 200, 202 |
|
| 19 | + */ |
|
| 20 | + public ?int $code = null; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Requested data if request was handled synchronously. |
|
| 24 | + * @example ['status' => 1] - Successful operation |
|
| 25 | + */ |
|
| 26 | + public ?array $data = null; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Path to the status checker if request was handled asynchronously. |
|
| 30 | + */ |
|
| 31 | + public ?string $location = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Job Id to check status if request was handled asynchronously. |
|
| 35 | + */ |
|
| 36 | + public ?string $x_job_id = null; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Job running time in seconds. |
|
| 40 | + */ |
|
| 41 | + public ?string $x_r4m_async_job_running_time = null; |
|
| 42 | + |
|
| 43 | + public function __construct(?array $params = null) |
|
| 44 | + { |
|
| 45 | + if ($params !== null) { |
|
| 46 | + $this->fillFromArray($params); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | |
| 36 | 36 | public function __construct(?array $params = null) |
| 37 | 37 | { |
| 38 | - if ($params !== null) { |
|
| 38 | + if ($params!==null) { |
|
| 39 | 39 | $this->fillFromArray($params); |
| 40 | 40 | } |
| 41 | 41 | } |
@@ -14,43 +14,43 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ResponseAll extends Common |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Array of ResponceAddresses. |
|
| 19 | - * @var ResponceAddress[] |
|
| 20 | - */ |
|
| 21 | - public ?array $results = null; |
|
| 17 | + /** |
|
| 18 | + * Array of ResponceAddresses. |
|
| 19 | + * @var ResponceAddress[] |
|
| 20 | + */ |
|
| 21 | + public ?array $results = null; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Total quantity of ResponceAddresses that match the query. |
|
| 25 | - * @example 1 |
|
| 26 | - */ |
|
| 27 | - public ?int $total = null; |
|
| 23 | + /** |
|
| 24 | + * Total quantity of ResponceAddresses that match the query. |
|
| 25 | + * @example 1 |
|
| 26 | + */ |
|
| 27 | + public ?int $total = null; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * An array of valid fields name in results array. |
|
| 31 | - * @var string[] |
|
| 32 | - */ |
|
| 33 | - public ?array $fields = null; |
|
| 29 | + /** |
|
| 30 | + * An array of valid fields name in results array. |
|
| 31 | + * @var string[] |
|
| 32 | + */ |
|
| 33 | + public ?array $fields = null; |
|
| 34 | 34 | |
| 35 | - public function __construct(?array $params = null) |
|
| 36 | - { |
|
| 37 | - if ($params !== null) { |
|
| 38 | - foreach ($this as $key => $value) { |
|
| 39 | - if (isset($params[$key])) { |
|
| 40 | - if ($key === 'results') { |
|
| 41 | - $this->{$key} = array(); |
|
| 42 | - foreach ($params[$key] as $ra_key => $ra_value) { |
|
| 43 | - if (is_array($ra_value)) { |
|
| 44 | - array_push($this->{$key}, new ResponseAddress($ra_value)); |
|
| 45 | - } elseif (is_object($ra_value) && $ra_value instanceof ResponseAddress) { |
|
| 46 | - array_push($this->{$key}, $ra_value); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - } else { |
|
| 50 | - $this->{$key} = $params[$key]; |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 35 | + public function __construct(?array $params = null) |
|
| 36 | + { |
|
| 37 | + if ($params !== null) { |
|
| 38 | + foreach ($this as $key => $value) { |
|
| 39 | + if (isset($params[$key])) { |
|
| 40 | + if ($key === 'results') { |
|
| 41 | + $this->{$key} = array(); |
|
| 42 | + foreach ($params[$key] as $ra_key => $ra_value) { |
|
| 43 | + if (is_array($ra_value)) { |
|
| 44 | + array_push($this->{$key}, new ResponseAddress($ra_value)); |
|
| 45 | + } elseif (is_object($ra_value) && $ra_value instanceof ResponseAddress) { |
|
| 46 | + array_push($this->{$key}, $ra_value); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + } else { |
|
| 50 | + $this->{$key} = $params[$key]; |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -34,10 +34,10 @@ |
||
| 34 | 34 | |
| 35 | 35 | public function __construct(?array $params = null) |
| 36 | 36 | { |
| 37 | - if ($params !== null) { |
|
| 37 | + if ($params!==null) { |
|
| 38 | 38 | foreach ($this as $key => $value) { |
| 39 | 39 | if (isset($params[$key])) { |
| 40 | - if ($key === 'results') { |
|
| 40 | + if ($key==='results') { |
|
| 41 | 41 | $this->{$key} = array(); |
| 42 | 42 | foreach ($params[$key] as $ra_key => $ra_value) { |
| 43 | 43 | if (is_array($ra_value)) { |
@@ -7,367 +7,367 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Route4Me |
| 9 | 9 | { |
| 10 | - public static $apiKey; |
|
| 11 | - public static $baseUrl = Endpoint::BASE_URL; |
|
| 12 | - |
|
| 13 | - public static function setApiKey($apiKey) |
|
| 14 | - { |
|
| 15 | - self::$apiKey = $apiKey; |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - public static function getApiKey() |
|
| 19 | - { |
|
| 20 | - return self::$apiKey; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public static function setBaseUrl($baseUrl) |
|
| 24 | - { |
|
| 25 | - self::$baseUrl = $baseUrl; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public static function getBaseUrl() |
|
| 29 | - { |
|
| 30 | - return self::$baseUrl; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Make request with CURL |
|
| 35 | - * |
|
| 36 | - * @since 1.2.3 changed error handling |
|
| 37 | - * @since 1.2.8 added $options['return_headers'] |
|
| 38 | - * |
|
| 39 | - * @param array $options |
|
| 40 | - * string url - HTTP URL. |
|
| 41 | - * string method - HTTP method. |
|
| 42 | - * string api_key - API key to access to route4me server. |
|
| 43 | - * array query - Array of query parameters. |
|
| 44 | - * array body - Array of body parameters. |
|
| 45 | - * string HTTPHEADER - Content type of body e.g. |
|
| 46 | - * 'Content-Type: application/json' |
|
| 47 | - * 'Content-Type: multipart/form-data' |
|
| 48 | - * array HTTPHEADERS - Array of headers. |
|
| 49 | - * string FILE - Path to uploading file. |
|
| 50 | - * array return_headers - Array of response headers to return as a result. |
|
| 51 | - * @throws Exception\ApiError |
|
| 52 | - */ |
|
| 53 | - public static function makeRequst($options) |
|
| 54 | - { |
|
| 55 | - $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 56 | - $query = isset($options['query']) |
|
| 57 | - ? array_filter($options['query'], function ($x) { |
|
| 58 | - return !is_null($x); |
|
| 59 | - }) : []; |
|
| 60 | - |
|
| 61 | - $body = isset($options['body']) ? $options['body'] : null; |
|
| 62 | - $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 63 | - $headers = [ |
|
| 64 | - 'User-Agent: Route4Me php-sdk', |
|
| 65 | - ]; |
|
| 66 | - |
|
| 67 | - $return_headers = (isset($options['return_headers']) ? $options['return_headers'] : null); |
|
| 68 | - |
|
| 69 | - if (isset($options['HTTPHEADER'])) { |
|
| 70 | - $headers[] = $options['HTTPHEADER']; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if (isset($options['HTTPHEADERS'])) { |
|
| 74 | - foreach ($options['HTTPHEADERS'] as $header) { |
|
| 75 | - $headers[] = $header; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $ch = curl_init(); |
|
| 80 | - |
|
| 81 | - $url = isset($options['url']) |
|
| 82 | - ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 83 | - $query, |
|
| 84 | - ['api_key' => self::getApiKey()] |
|
| 85 | - )) : ''; |
|
| 86 | - |
|
| 87 | - $baseUrl = self::getBaseUrl(); |
|
| 88 | - |
|
| 89 | - $curlOpts = [ |
|
| 90 | - CURLOPT_URL => $baseUrl.$url, |
|
| 91 | - CURLOPT_RETURNTRANSFER => true, |
|
| 92 | - CURLOPT_TIMEOUT => 120, |
|
| 93 | - CURLOPT_FOLLOWLOCATION => true, |
|
| 94 | - CURLOPT_SSL_VERIFYHOST => false, |
|
| 95 | - CURLOPT_SSL_VERIFYPEER => false, |
|
| 96 | - CURLOPT_HTTPHEADER => $headers, |
|
| 97 | - ]; |
|
| 98 | - |
|
| 99 | - curl_setopt_array($ch, $curlOpts); |
|
| 100 | - |
|
| 101 | - // read response headers if need |
|
| 102 | - $response_headers = []; |
|
| 103 | - if ($return_headers) { |
|
| 104 | - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$response_headers) { |
|
| 105 | - $len = strlen($header); |
|
| 106 | - $header = explode(':', $header, 2); |
|
| 107 | - if (count($header) >= 2) { |
|
| 108 | - $response_headers[strtolower(trim($header[0]))] = trim($header[1]); |
|
| 109 | - } |
|
| 110 | - return $len; |
|
| 111 | - }); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if (null != $file) { |
|
| 115 | - $cfile = new \CURLFile($file, '', ''); |
|
| 116 | - $body['strFilename']=$cfile; |
|
| 117 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 118 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 119 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 120 | - } else { |
|
| 121 | - switch ($method) { |
|
| 122 | - case 'DELETE': |
|
| 123 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | - break; |
|
| 125 | - case 'DELETEARRAY': |
|
| 126 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | - break; |
|
| 129 | - case 'PUT': |
|
| 130 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | - break; |
|
| 132 | - case 'POST': |
|
| 133 | - if (isset($body)) { |
|
| 134 | - $bodyData = json_encode($body); |
|
| 135 | - if (isset($options['HTTPHEADER'])) { |
|
| 136 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 137 | - $bodyData = $body; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 141 | - } |
|
| 142 | - break; |
|
| 143 | - case 'ADD': |
|
| 144 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 145 | - break; |
|
| 146 | - case 'PATCH': |
|
| 147 | - if (isset($body)) { |
|
| 148 | - $bodyData = json_encode($body); |
|
| 149 | - if (isset($options['HTTPHEADER'])) { |
|
| 150 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 151 | - $bodyData = $body; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 155 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 156 | - } |
|
| 157 | - break; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 161 | - if (isset($body)) { |
|
| 162 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $result = curl_exec($ch); |
|
| 168 | - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 169 | - curl_close($ch); |
|
| 170 | - |
|
| 171 | - $json = null; |
|
| 172 | - if (strpos($result, '<?xml') > -1) { |
|
| 173 | - $xml = simplexml_load_string($result); |
|
| 174 | - $json = self::object2array($xml); |
|
| 175 | - } else { |
|
| 176 | - $json = json_decode($result, true); |
|
| 177 | - } |
|
| 178 | - if (200 == $code || 201 == $code || 202 == $code) { |
|
| 179 | - if (isset($json['errors'])) { |
|
| 180 | - throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 181 | - } else { |
|
| 182 | - // return response headers if they were asked for |
|
| 183 | - if (count($response_headers) !== 0) { |
|
| 184 | - $res = [ |
|
| 185 | - 'code' => $code, |
|
| 186 | - 'data' => $json |
|
| 187 | - ]; |
|
| 188 | - foreach ($return_headers as $key => $value) { |
|
| 189 | - // most headers has char '-' but it is forbidden in PHP names replace it with '_' |
|
| 190 | - $res[strtolower(str_replace('-', '_', $value))] = |
|
| 191 | - (isset($response_headers[$value]) ? $response_headers[$value] : null); |
|
| 192 | - } |
|
| 193 | - return $res; |
|
| 194 | - } |
|
| 195 | - return $json; |
|
| 196 | - } |
|
| 197 | - } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 198 | - throw new ApiError('', $code, $result); |
|
| 199 | - } else { |
|
| 200 | - if (isset($json['messages'])) { |
|
| 201 | - $msg = ''; |
|
| 202 | - foreach ($json['messages'] as $key => $value) { |
|
| 203 | - if ($msg !== '') { |
|
| 204 | - $msg .= PHP_EOL; |
|
| 205 | - } |
|
| 206 | - $msg .= $key . ': ' . implode(', ', $value); |
|
| 207 | - } |
|
| 208 | - throw new ApiError($msg, $code, $result); |
|
| 209 | - } elseif (isset($json['errors'])) { |
|
| 210 | - $msg = ''; |
|
| 211 | - foreach ($json['errors'] as $key => $value) { |
|
| 212 | - if ($msg !== '') { |
|
| 213 | - $msg .= PHP_EOL; |
|
| 214 | - } |
|
| 215 | - $msg .= $value; |
|
| 216 | - } |
|
| 217 | - throw new ApiError($msg, $code, $result); |
|
| 218 | - } else { |
|
| 219 | - throw new ApiError($result, $code, $result); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @param $object: JSON object |
|
| 226 | - */ |
|
| 227 | - public static function object2array($object) |
|
| 228 | - { |
|
| 229 | - return @json_decode(@json_encode($object), 1); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Prints on the screen main keys and values of the array. |
|
| 234 | - * |
|
| 235 | - * @param $results: object to be printed on the screen |
|
| 236 | - * @param $deepPrinting: if true, object will be printed recursively |
|
| 237 | - */ |
|
| 238 | - public static function simplePrint($results, $deepPrinting = null) |
|
| 239 | - { |
|
| 240 | - if (isset($results)) { |
|
| 241 | - if (is_array($results)) { |
|
| 242 | - foreach ($results as $key => $result) { |
|
| 243 | - if (is_array($result)) { |
|
| 244 | - foreach ($result as $key1 => $result1) { |
|
| 245 | - if (is_array($result1)) { |
|
| 246 | - if ($deepPrinting) { |
|
| 247 | - echo "<br>$key1 ------><br>"; |
|
| 248 | - self::simplePrint($result1, true); |
|
| 249 | - echo '------<br>'; |
|
| 250 | - } else { |
|
| 251 | - echo $key1.' --> '.'Array() <br>'; |
|
| 252 | - } |
|
| 253 | - } else { |
|
| 254 | - if (is_object($result1)) { |
|
| 255 | - if ($deepPrinting) { |
|
| 256 | - echo "<br>$key1 ------><br>"; |
|
| 257 | - $oarray = (array) $result1; |
|
| 258 | - self::simplePrint($oarray, true); |
|
| 259 | - echo '------<br>'; |
|
| 260 | - } else { |
|
| 261 | - echo $key1.' --> '.'Object <br>'; |
|
| 262 | - } |
|
| 263 | - } else { |
|
| 264 | - if (!is_null($result1)) { |
|
| 265 | - echo $key1.' --> '.$result1.'<br>'; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } else { |
|
| 271 | - if (is_object($result)) { |
|
| 272 | - if ($deepPrinting) { |
|
| 273 | - echo "<br>$key ------><br>"; |
|
| 274 | - $oarray = (array) $result; |
|
| 275 | - self::simplePrint($oarray, true); |
|
| 276 | - echo '------<br>'; |
|
| 277 | - } else { |
|
| 278 | - echo $key.' --> '.'Object <br>'; |
|
| 279 | - } |
|
| 280 | - } else { |
|
| 281 | - if (!is_null($result)) { |
|
| 282 | - echo $key.' --> '.$result.'<br>'; |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - //echo "<br>"; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Generates query or body parameters. |
|
| 294 | - * |
|
| 295 | - * @param $allFields: all known fields could be used for parameters generation |
|
| 296 | - * @param $params: input parameters (array or object) |
|
| 297 | - */ |
|
| 298 | - public static function generateRequestParameters($allFields, $params) |
|
| 299 | - { |
|
| 300 | - $generatedParams = []; |
|
| 301 | - |
|
| 302 | - if (is_array($params)) { |
|
| 303 | - foreach ($allFields as $field) { |
|
| 304 | - if (isset($params[$field])) { |
|
| 305 | - $generatedParams[$field] = $params[$field]; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - } elseif (is_object($params)) { |
|
| 309 | - foreach ($allFields as $field) { |
|
| 310 | - if (isset($params->{$field})) { |
|
| 311 | - $generatedParams[$field] = $params->{$field}; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - return $generatedParams; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Returns an array of the object properties. |
|
| 321 | - * |
|
| 322 | - * @param $object: An object |
|
| 323 | - * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 324 | - */ |
|
| 325 | - public static function getObjectProperties($object, $exclude) |
|
| 326 | - { |
|
| 327 | - $objectParameters = []; |
|
| 328 | - |
|
| 329 | - foreach (get_object_vars($object) as $key => $value) { |
|
| 330 | - if (property_exists($object, $key)) { |
|
| 331 | - if (!is_numeric(array_search($key, $exclude))) { |
|
| 332 | - array_push($objectParameters, $key); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - return $objectParameters; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Returns url path generated from the array of the fields and parameters. |
|
| 342 | - * |
|
| 343 | - * @param $allFields; array of the paossible fields (parameter names) |
|
| 344 | - * @param $params: input parameters (array or object) |
|
| 345 | - */ |
|
| 346 | - public static function generateUrlPath($allFields, $params) |
|
| 347 | - { |
|
| 348 | - $generatedPath = ''; |
|
| 349 | - |
|
| 350 | - if (is_array($params)) { |
|
| 351 | - foreach ($allFields as $field) { |
|
| 352 | - if (isset($params[$field])) { |
|
| 353 | - $generatedPath .= $params[$field].'/'; |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } elseif (is_object($params)) { |
|
| 357 | - foreach ($allFields as $field) { |
|
| 358 | - if (isset($params->{$field})) { |
|
| 359 | - $generatedPath .= $params->{$field}.'/'; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return $generatedPath; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - public static function getFileRealPath($fileName) |
|
| 368 | - { |
|
| 369 | - $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 370 | - |
|
| 371 | - return $rpath; |
|
| 372 | - } |
|
| 10 | + public static $apiKey; |
|
| 11 | + public static $baseUrl = Endpoint::BASE_URL; |
|
| 12 | + |
|
| 13 | + public static function setApiKey($apiKey) |
|
| 14 | + { |
|
| 15 | + self::$apiKey = $apiKey; |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + public static function getApiKey() |
|
| 19 | + { |
|
| 20 | + return self::$apiKey; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public static function setBaseUrl($baseUrl) |
|
| 24 | + { |
|
| 25 | + self::$baseUrl = $baseUrl; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public static function getBaseUrl() |
|
| 29 | + { |
|
| 30 | + return self::$baseUrl; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Make request with CURL |
|
| 35 | + * |
|
| 36 | + * @since 1.2.3 changed error handling |
|
| 37 | + * @since 1.2.8 added $options['return_headers'] |
|
| 38 | + * |
|
| 39 | + * @param array $options |
|
| 40 | + * string url - HTTP URL. |
|
| 41 | + * string method - HTTP method. |
|
| 42 | + * string api_key - API key to access to route4me server. |
|
| 43 | + * array query - Array of query parameters. |
|
| 44 | + * array body - Array of body parameters. |
|
| 45 | + * string HTTPHEADER - Content type of body e.g. |
|
| 46 | + * 'Content-Type: application/json' |
|
| 47 | + * 'Content-Type: multipart/form-data' |
|
| 48 | + * array HTTPHEADERS - Array of headers. |
|
| 49 | + * string FILE - Path to uploading file. |
|
| 50 | + * array return_headers - Array of response headers to return as a result. |
|
| 51 | + * @throws Exception\ApiError |
|
| 52 | + */ |
|
| 53 | + public static function makeRequst($options) |
|
| 54 | + { |
|
| 55 | + $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 56 | + $query = isset($options['query']) |
|
| 57 | + ? array_filter($options['query'], function ($x) { |
|
| 58 | + return !is_null($x); |
|
| 59 | + }) : []; |
|
| 60 | + |
|
| 61 | + $body = isset($options['body']) ? $options['body'] : null; |
|
| 62 | + $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 63 | + $headers = [ |
|
| 64 | + 'User-Agent: Route4Me php-sdk', |
|
| 65 | + ]; |
|
| 66 | + |
|
| 67 | + $return_headers = (isset($options['return_headers']) ? $options['return_headers'] : null); |
|
| 68 | + |
|
| 69 | + if (isset($options['HTTPHEADER'])) { |
|
| 70 | + $headers[] = $options['HTTPHEADER']; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if (isset($options['HTTPHEADERS'])) { |
|
| 74 | + foreach ($options['HTTPHEADERS'] as $header) { |
|
| 75 | + $headers[] = $header; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $ch = curl_init(); |
|
| 80 | + |
|
| 81 | + $url = isset($options['url']) |
|
| 82 | + ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 83 | + $query, |
|
| 84 | + ['api_key' => self::getApiKey()] |
|
| 85 | + )) : ''; |
|
| 86 | + |
|
| 87 | + $baseUrl = self::getBaseUrl(); |
|
| 88 | + |
|
| 89 | + $curlOpts = [ |
|
| 90 | + CURLOPT_URL => $baseUrl.$url, |
|
| 91 | + CURLOPT_RETURNTRANSFER => true, |
|
| 92 | + CURLOPT_TIMEOUT => 120, |
|
| 93 | + CURLOPT_FOLLOWLOCATION => true, |
|
| 94 | + CURLOPT_SSL_VERIFYHOST => false, |
|
| 95 | + CURLOPT_SSL_VERIFYPEER => false, |
|
| 96 | + CURLOPT_HTTPHEADER => $headers, |
|
| 97 | + ]; |
|
| 98 | + |
|
| 99 | + curl_setopt_array($ch, $curlOpts); |
|
| 100 | + |
|
| 101 | + // read response headers if need |
|
| 102 | + $response_headers = []; |
|
| 103 | + if ($return_headers) { |
|
| 104 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$response_headers) { |
|
| 105 | + $len = strlen($header); |
|
| 106 | + $header = explode(':', $header, 2); |
|
| 107 | + if (count($header) >= 2) { |
|
| 108 | + $response_headers[strtolower(trim($header[0]))] = trim($header[1]); |
|
| 109 | + } |
|
| 110 | + return $len; |
|
| 111 | + }); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if (null != $file) { |
|
| 115 | + $cfile = new \CURLFile($file, '', ''); |
|
| 116 | + $body['strFilename']=$cfile; |
|
| 117 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 118 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 119 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 120 | + } else { |
|
| 121 | + switch ($method) { |
|
| 122 | + case 'DELETE': |
|
| 123 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | + break; |
|
| 125 | + case 'DELETEARRAY': |
|
| 126 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | + break; |
|
| 129 | + case 'PUT': |
|
| 130 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | + break; |
|
| 132 | + case 'POST': |
|
| 133 | + if (isset($body)) { |
|
| 134 | + $bodyData = json_encode($body); |
|
| 135 | + if (isset($options['HTTPHEADER'])) { |
|
| 136 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 137 | + $bodyData = $body; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 141 | + } |
|
| 142 | + break; |
|
| 143 | + case 'ADD': |
|
| 144 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 145 | + break; |
|
| 146 | + case 'PATCH': |
|
| 147 | + if (isset($body)) { |
|
| 148 | + $bodyData = json_encode($body); |
|
| 149 | + if (isset($options['HTTPHEADER'])) { |
|
| 150 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 151 | + $bodyData = $body; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 155 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 156 | + } |
|
| 157 | + break; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 161 | + if (isset($body)) { |
|
| 162 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $result = curl_exec($ch); |
|
| 168 | + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 169 | + curl_close($ch); |
|
| 170 | + |
|
| 171 | + $json = null; |
|
| 172 | + if (strpos($result, '<?xml') > -1) { |
|
| 173 | + $xml = simplexml_load_string($result); |
|
| 174 | + $json = self::object2array($xml); |
|
| 175 | + } else { |
|
| 176 | + $json = json_decode($result, true); |
|
| 177 | + } |
|
| 178 | + if (200 == $code || 201 == $code || 202 == $code) { |
|
| 179 | + if (isset($json['errors'])) { |
|
| 180 | + throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 181 | + } else { |
|
| 182 | + // return response headers if they were asked for |
|
| 183 | + if (count($response_headers) !== 0) { |
|
| 184 | + $res = [ |
|
| 185 | + 'code' => $code, |
|
| 186 | + 'data' => $json |
|
| 187 | + ]; |
|
| 188 | + foreach ($return_headers as $key => $value) { |
|
| 189 | + // most headers has char '-' but it is forbidden in PHP names replace it with '_' |
|
| 190 | + $res[strtolower(str_replace('-', '_', $value))] = |
|
| 191 | + (isset($response_headers[$value]) ? $response_headers[$value] : null); |
|
| 192 | + } |
|
| 193 | + return $res; |
|
| 194 | + } |
|
| 195 | + return $json; |
|
| 196 | + } |
|
| 197 | + } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 198 | + throw new ApiError('', $code, $result); |
|
| 199 | + } else { |
|
| 200 | + if (isset($json['messages'])) { |
|
| 201 | + $msg = ''; |
|
| 202 | + foreach ($json['messages'] as $key => $value) { |
|
| 203 | + if ($msg !== '') { |
|
| 204 | + $msg .= PHP_EOL; |
|
| 205 | + } |
|
| 206 | + $msg .= $key . ': ' . implode(', ', $value); |
|
| 207 | + } |
|
| 208 | + throw new ApiError($msg, $code, $result); |
|
| 209 | + } elseif (isset($json['errors'])) { |
|
| 210 | + $msg = ''; |
|
| 211 | + foreach ($json['errors'] as $key => $value) { |
|
| 212 | + if ($msg !== '') { |
|
| 213 | + $msg .= PHP_EOL; |
|
| 214 | + } |
|
| 215 | + $msg .= $value; |
|
| 216 | + } |
|
| 217 | + throw new ApiError($msg, $code, $result); |
|
| 218 | + } else { |
|
| 219 | + throw new ApiError($result, $code, $result); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @param $object: JSON object |
|
| 226 | + */ |
|
| 227 | + public static function object2array($object) |
|
| 228 | + { |
|
| 229 | + return @json_decode(@json_encode($object), 1); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Prints on the screen main keys and values of the array. |
|
| 234 | + * |
|
| 235 | + * @param $results: object to be printed on the screen |
|
| 236 | + * @param $deepPrinting: if true, object will be printed recursively |
|
| 237 | + */ |
|
| 238 | + public static function simplePrint($results, $deepPrinting = null) |
|
| 239 | + { |
|
| 240 | + if (isset($results)) { |
|
| 241 | + if (is_array($results)) { |
|
| 242 | + foreach ($results as $key => $result) { |
|
| 243 | + if (is_array($result)) { |
|
| 244 | + foreach ($result as $key1 => $result1) { |
|
| 245 | + if (is_array($result1)) { |
|
| 246 | + if ($deepPrinting) { |
|
| 247 | + echo "<br>$key1 ------><br>"; |
|
| 248 | + self::simplePrint($result1, true); |
|
| 249 | + echo '------<br>'; |
|
| 250 | + } else { |
|
| 251 | + echo $key1.' --> '.'Array() <br>'; |
|
| 252 | + } |
|
| 253 | + } else { |
|
| 254 | + if (is_object($result1)) { |
|
| 255 | + if ($deepPrinting) { |
|
| 256 | + echo "<br>$key1 ------><br>"; |
|
| 257 | + $oarray = (array) $result1; |
|
| 258 | + self::simplePrint($oarray, true); |
|
| 259 | + echo '------<br>'; |
|
| 260 | + } else { |
|
| 261 | + echo $key1.' --> '.'Object <br>'; |
|
| 262 | + } |
|
| 263 | + } else { |
|
| 264 | + if (!is_null($result1)) { |
|
| 265 | + echo $key1.' --> '.$result1.'<br>'; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } else { |
|
| 271 | + if (is_object($result)) { |
|
| 272 | + if ($deepPrinting) { |
|
| 273 | + echo "<br>$key ------><br>"; |
|
| 274 | + $oarray = (array) $result; |
|
| 275 | + self::simplePrint($oarray, true); |
|
| 276 | + echo '------<br>'; |
|
| 277 | + } else { |
|
| 278 | + echo $key.' --> '.'Object <br>'; |
|
| 279 | + } |
|
| 280 | + } else { |
|
| 281 | + if (!is_null($result)) { |
|
| 282 | + echo $key.' --> '.$result.'<br>'; |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + //echo "<br>"; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Generates query or body parameters. |
|
| 294 | + * |
|
| 295 | + * @param $allFields: all known fields could be used for parameters generation |
|
| 296 | + * @param $params: input parameters (array or object) |
|
| 297 | + */ |
|
| 298 | + public static function generateRequestParameters($allFields, $params) |
|
| 299 | + { |
|
| 300 | + $generatedParams = []; |
|
| 301 | + |
|
| 302 | + if (is_array($params)) { |
|
| 303 | + foreach ($allFields as $field) { |
|
| 304 | + if (isset($params[$field])) { |
|
| 305 | + $generatedParams[$field] = $params[$field]; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + } elseif (is_object($params)) { |
|
| 309 | + foreach ($allFields as $field) { |
|
| 310 | + if (isset($params->{$field})) { |
|
| 311 | + $generatedParams[$field] = $params->{$field}; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + return $generatedParams; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Returns an array of the object properties. |
|
| 321 | + * |
|
| 322 | + * @param $object: An object |
|
| 323 | + * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 324 | + */ |
|
| 325 | + public static function getObjectProperties($object, $exclude) |
|
| 326 | + { |
|
| 327 | + $objectParameters = []; |
|
| 328 | + |
|
| 329 | + foreach (get_object_vars($object) as $key => $value) { |
|
| 330 | + if (property_exists($object, $key)) { |
|
| 331 | + if (!is_numeric(array_search($key, $exclude))) { |
|
| 332 | + array_push($objectParameters, $key); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + return $objectParameters; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Returns url path generated from the array of the fields and parameters. |
|
| 342 | + * |
|
| 343 | + * @param $allFields; array of the paossible fields (parameter names) |
|
| 344 | + * @param $params: input parameters (array or object) |
|
| 345 | + */ |
|
| 346 | + public static function generateUrlPath($allFields, $params) |
|
| 347 | + { |
|
| 348 | + $generatedPath = ''; |
|
| 349 | + |
|
| 350 | + if (is_array($params)) { |
|
| 351 | + foreach ($allFields as $field) { |
|
| 352 | + if (isset($params[$field])) { |
|
| 353 | + $generatedPath .= $params[$field].'/'; |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } elseif (is_object($params)) { |
|
| 357 | + foreach ($allFields as $field) { |
|
| 358 | + if (isset($params->{$field})) { |
|
| 359 | + $generatedPath .= $params->{$field}.'/'; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return $generatedPath; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + public static function getFileRealPath($fileName) |
|
| 368 | + { |
|
| 369 | + $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 370 | + |
|
| 371 | + return $rpath; |
|
| 372 | + } |
|
| 373 | 373 | } |
@@ -119,42 +119,42 @@ |
||
| 119 | 119 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
| 120 | 120 | } else { |
| 121 | 121 | switch ($method) { |
| 122 | - case 'DELETE': |
|
| 123 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | - break; |
|
| 125 | - case 'DELETEARRAY': |
|
| 126 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | - break; |
|
| 129 | - case 'PUT': |
|
| 130 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | - break; |
|
| 132 | - case 'POST': |
|
| 133 | - if (isset($body)) { |
|
| 134 | - $bodyData = json_encode($body); |
|
| 135 | - if (isset($options['HTTPHEADER'])) { |
|
| 136 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 137 | - $bodyData = $body; |
|
| 138 | - } |
|
| 122 | + case 'DELETE': |
|
| 123 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | + break; |
|
| 125 | + case 'DELETEARRAY': |
|
| 126 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | + break; |
|
| 129 | + case 'PUT': |
|
| 130 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | + break; |
|
| 132 | + case 'POST': |
|
| 133 | + if (isset($body)) { |
|
| 134 | + $bodyData = json_encode($body); |
|
| 135 | + if (isset($options['HTTPHEADER'])) { |
|
| 136 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 137 | + $bodyData = $body; |
|
| 139 | 138 | } |
| 140 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 141 | 139 | } |
| 142 | - break; |
|
| 143 | - case 'ADD': |
|
| 144 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 145 | - break; |
|
| 146 | - case 'PATCH': |
|
| 147 | - if (isset($body)) { |
|
| 148 | - $bodyData = json_encode($body); |
|
| 149 | - if (isset($options['HTTPHEADER'])) { |
|
| 150 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 151 | - $bodyData = $body; |
|
| 152 | - } |
|
| 140 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 141 | + } |
|
| 142 | + break; |
|
| 143 | + case 'ADD': |
|
| 144 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 145 | + break; |
|
| 146 | + case 'PATCH': |
|
| 147 | + if (isset($body)) { |
|
| 148 | + $bodyData = json_encode($body); |
|
| 149 | + if (isset($options['HTTPHEADER'])) { |
|
| 150 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 151 | + $bodyData = $body; |
|
| 153 | 152 | } |
| 154 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 155 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 156 | 153 | } |
| 157 | - break; |
|
| 154 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 155 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 156 | + } |
|
| 157 | + break; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
| 56 | 56 | $query = isset($options['query']) |
| 57 | - ? array_filter($options['query'], function ($x) { |
|
| 57 | + ? array_filter($options['query'], function($x) { |
|
| 58 | 58 | return !is_null($x); |
| 59 | 59 | }) : []; |
| 60 | 60 | |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $ch = curl_init(); |
| 80 | 80 | |
| 81 | 81 | $url = isset($options['url']) |
| 82 | - ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 82 | + ? $options['url'].'?'.http_build_query(array_merge( |
|
| 83 | 83 | $query, |
| 84 | 84 | ['api_key' => self::getApiKey()] |
| 85 | 85 | )) : ''; |
@@ -101,19 +101,19 @@ discard block |
||
| 101 | 101 | // read response headers if need |
| 102 | 102 | $response_headers = []; |
| 103 | 103 | if ($return_headers) { |
| 104 | - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$response_headers) { |
|
| 104 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$response_headers) { |
|
| 105 | 105 | $len = strlen($header); |
| 106 | 106 | $header = explode(':', $header, 2); |
| 107 | - if (count($header) >= 2) { |
|
| 107 | + if (count($header)>=2) { |
|
| 108 | 108 | $response_headers[strtolower(trim($header[0]))] = trim($header[1]); |
| 109 | 109 | } |
| 110 | 110 | return $len; |
| 111 | 111 | }); |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if (null != $file) { |
|
| 114 | + if (null!=$file) { |
|
| 115 | 115 | $cfile = new \CURLFile($file, '', ''); |
| 116 | - $body['strFilename']=$cfile; |
|
| 116 | + $body['strFilename'] = $cfile; |
|
| 117 | 117 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 118 | 118 | curl_setopt($ch, CURLOPT_POST, true); |
| 119 | 119 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | if (isset($body)) { |
| 134 | 134 | $bodyData = json_encode($body); |
| 135 | 135 | if (isset($options['HTTPHEADER'])) { |
| 136 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 136 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data')>0) { |
|
| 137 | 137 | $bodyData = $body; |
| 138 | 138 | } |
| 139 | 139 | } |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | if (isset($body)) { |
| 148 | 148 | $bodyData = json_encode($body); |
| 149 | 149 | if (isset($options['HTTPHEADER'])) { |
| 150 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 150 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data')>0) { |
|
| 151 | 151 | $bodyData = $body; |
| 152 | 152 | } |
| 153 | 153 | } |
@@ -169,18 +169,18 @@ discard block |
||
| 169 | 169 | curl_close($ch); |
| 170 | 170 | |
| 171 | 171 | $json = null; |
| 172 | - if (strpos($result, '<?xml') > -1) { |
|
| 172 | + if (strpos($result, '<?xml')>-1) { |
|
| 173 | 173 | $xml = simplexml_load_string($result); |
| 174 | 174 | $json = self::object2array($xml); |
| 175 | 175 | } else { |
| 176 | 176 | $json = json_decode($result, true); |
| 177 | 177 | } |
| 178 | - if (200 == $code || 201 == $code || 202 == $code) { |
|
| 178 | + if (200==$code || 201==$code || 202==$code) { |
|
| 179 | 179 | if (isset($json['errors'])) { |
| 180 | 180 | throw new ApiError(implode(', ', $json['errors']), $code, $result); |
| 181 | 181 | } else { |
| 182 | 182 | // return response headers if they were asked for |
| 183 | - if (count($response_headers) !== 0) { |
|
| 183 | + if (count($response_headers)!==0) { |
|
| 184 | 184 | $res = [ |
| 185 | 185 | 'code' => $code, |
| 186 | 186 | 'data' => $json |
@@ -200,16 +200,16 @@ discard block |
||
| 200 | 200 | if (isset($json['messages'])) { |
| 201 | 201 | $msg = ''; |
| 202 | 202 | foreach ($json['messages'] as $key => $value) { |
| 203 | - if ($msg !== '') { |
|
| 203 | + if ($msg!=='') { |
|
| 204 | 204 | $msg .= PHP_EOL; |
| 205 | 205 | } |
| 206 | - $msg .= $key . ': ' . implode(', ', $value); |
|
| 206 | + $msg .= $key.': '.implode(', ', $value); |
|
| 207 | 207 | } |
| 208 | 208 | throw new ApiError($msg, $code, $result); |
| 209 | 209 | } elseif (isset($json['errors'])) { |
| 210 | 210 | $msg = ''; |
| 211 | 211 | foreach ($json['errors'] as $key => $value) { |
| 212 | - if ($msg !== '') { |
|
| 212 | + if ($msg!=='') { |
|
| 213 | 213 | $msg .= PHP_EOL; |
| 214 | 214 | } |
| 215 | 215 | $msg .= $value; |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | if (is_object($result1)) { |
| 255 | 255 | if ($deepPrinting) { |
| 256 | 256 | echo "<br>$key1 ------><br>"; |
| 257 | - $oarray = (array) $result1; |
|
| 257 | + $oarray = (array)$result1; |
|
| 258 | 258 | self::simplePrint($oarray, true); |
| 259 | 259 | echo '------<br>'; |
| 260 | 260 | } else { |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | if (is_object($result)) { |
| 272 | 272 | if ($deepPrinting) { |
| 273 | 273 | echo "<br>$key ------><br>"; |
| 274 | - $oarray = (array) $result; |
|
| 274 | + $oarray = (array)$result; |
|
| 275 | 275 | self::simplePrint($oarray, true); |
| 276 | 276 | echo '------<br>'; |
| 277 | 277 | } else { |