From 1c9b352720407a282ae1744a4d312e318d1f312e Mon Sep 17 00:00:00 2001 From: lis-dev Date: Sat, 29 Aug 2020 23:20:29 +0300 Subject: [PATCH] update codestyle (#37) * Fixed definitions and phpdoc comments * Fixed getCity method for cities with similar names * fixed codestyle --- src/Delivery/NovaPoshtaApi2.php | 84 ++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/src/Delivery/NovaPoshtaApi2.php b/src/Delivery/NovaPoshtaApi2.php index 48eb170..e1c2e91 100644 --- a/src/Delivery/NovaPoshtaApi2.php +++ b/src/Delivery/NovaPoshtaApi2.php @@ -14,6 +14,8 @@ */ class NovaPoshtaApi2 { + const API_URI = 'https://api.novaposhta.ua/v2.0'; + /** * Key for API NovaPoshta. * @@ -46,7 +48,7 @@ class NovaPoshtaApi2 /** * @var string Areas (loaded from file, because there is no so function in NovaPoshta API 2.0) */ - protected $areas; + protected $areas = ''; /** * @var string Set current model for methods save(), update(), delete() @@ -56,12 +58,12 @@ class NovaPoshtaApi2 /** * @var string Set method of current model */ - protected $method; + protected $method = ''; /** * @var array Set params of current method of current model */ - protected $params; + protected $params = array(); /** * Default constructor. @@ -69,14 +71,14 @@ class NovaPoshtaApi2 * @param string $key NovaPoshta API key * @param string $language Default Language * @param bool $throwErrors Throw request errors as Exceptions - * @param bool $connectionType Connection type (curl | file_get_contents) + * @param string $connectionType Connection type (curl | file_get_contents) * * @return NovaPoshtaApi2 */ public function __construct($key, $language = 'ru', $throwErrors = false, $connectionType = 'curl') { $this->throwErrors = $throwErrors; - return $this + $this ->setKey($key) ->setLanguage($language) ->setConnectionType($connectionType) @@ -178,17 +180,17 @@ public function getFormat() /** * Prepare data before return it. * - * @param json $data + * @param string|array $data * * @return mixed */ private function prepare($data) { - //Returns array + // Returns array if ('array' == $this->format) { $result = is_array($data) ? $data - : json_decode($data, 1); + : json_decode($data, true); // If error exists, throw Exception if ($this->throwErrors and array_key_exists('errors', $result)) { throw new \Exception(is_array($result['errors']) ? implode("\n", $result['errors']) : $result['errors']); @@ -202,7 +204,8 @@ private function prepare($data) /** * Converts array to xml. * - * @param array + * @param array $array + * @param \SimpleXMLElement|bool $xml */ private function array2xml(array $array, $xml = false) { @@ -231,8 +234,8 @@ private function request($model, $method, $params = null) { // Get required URL $url = 'xml' == $this->format - ? 'https://api.novaposhta.ua/v2.0/xml/' - : 'https://api.novaposhta.ua/v2.0/json/'; + ? self::API_URI.'/xml/' + : self::API_URI.'/json/'; $data = array( 'apiKey' => $this->key, @@ -241,23 +244,26 @@ private function request($model, $method, $params = null) 'language' => $this->language, 'methodProperties' => $params, ); + $result = array(); // Convert data to neccessary format $post = 'xml' == $this->format ? $this->array2xml($data) - : $post = json_encode($data); + : json_encode($data); if ('curl' == $this->getConnectionType()) { $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.('xml' == $this->format ? 'text/xml' : 'application/json'))); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - $result = curl_exec($ch); - curl_close($ch); + if (is_resource($ch)) { + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.('xml' == $this->format ? 'text/xml' : 'application/json'))); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + $result = curl_exec($ch); + curl_close($ch); + } } else { - $result = file_get_contents($url, null, stream_context_create(array( + $result = file_get_contents($url, false, stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded;\r\n", @@ -283,8 +289,8 @@ public function model($model = '') } $this->model = $model; - $this->method = null; - $this->params = null; + $this->method = ''; + $this->params = array(); return $this; } @@ -302,7 +308,7 @@ public function method($method = '') } $this->method = $method; - $this->params = null; + $this->params = array(); return $this; } @@ -403,6 +409,7 @@ public function findNearestWarehouse($searchStringArray) public function getWarehouse($cityRef, $description = '') { $warehouses = $this->getWarehouses($cityRef); + $error = array(); $data = array(); if (is_array($warehouses['data'])) { if (1 === count($warehouses['data']) or !$description) { @@ -493,16 +500,17 @@ protected function findArea(array $areas, $findByString = '', $ref = '') public function getArea($findByString = '', $ref = '') { // Load areas list from file - empty($this->areas) and $this->areas = include dirname(__FILE__).'/NovaPoshtaApi2Areas.php'; + empty($this->areas) and $this->areas = (include dirname(__FILE__).'/NovaPoshtaApi2Areas.php'); $data = $this->findArea($this->areas, $findByString, $ref); // Error - empty($data) and $error = 'Area was not found'; + $error = array(); + empty($data) and $error = array('Area was not found'); // Return data in same format like NovaPoshta API return $this->prepare( array( 'success' => empty($error), 'data' => $data, - 'errors' => (array) $error, + 'errors' => $error, 'warnings' => array(), 'info' => array(), ) @@ -562,20 +570,22 @@ public function getCity($cityName, $areaName = '') { // Get cities by name $cities = $this->getCities(0, $cityName); - if (is_array($cities['data'])) { + $data = array(); + if (is_array($cities) && is_array($cities['data'])) { // If cities more then one, calculate current by area name $data = (count($cities['data']) > 1) ? $this->findCityByRegion($cities, $areaName) - : $cities['data'][0]; + : array($cities['data'][0]); } // Error - (!$data) and $error = 'City was not found'; + $error = array(); + (!$data) and $error = array('City was not found'); // Return data in same format like NovaPoshta API return $this->prepare( array( 'success' => empty($error), - 'data' => array($data), - 'errors' => (array) $error, + 'data' => $data, + 'errors' => $error, 'warnings' => array(), 'info' => array(), ) @@ -842,7 +852,7 @@ public function generateReport($params) /** * Check required fields for new InternetDocument and set defaults. * - * @param array & $counterparty Recipient info array + * @param array &$counterparty Recipient info array */ protected function checkInternetDocumentRecipient(array &$counterparty) { @@ -875,7 +885,7 @@ protected function checkInternetDocumentRecipient(array &$counterparty) /** * Check required params for new InternetDocument and set defaults. * - * @param array & $params + * @param array &$params */ protected function checkInternetDocumentParams(array &$params) { @@ -917,7 +927,7 @@ protected function checkInternetDocumentParams(array &$params) * 'PayerType' => (Sender|Recipient - default), 'PaymentMethod' => (NonCash|Cash - default) * 'ServiceType' => (DoorsDoors|DoorsWarehouse|WarehouseDoors|WarehouseWarehouse - default) * 'CargoType' => String - * @param mixed + * @return mixed */ public function newInternetDocument($sender, $recipient, $params) { @@ -979,7 +989,7 @@ public function newInternetDocument($sender, $recipient, $params) * Get only link on internet document for printing. * * @param string $method Called method of NovaPoshta API - * @param array|string $documentRefs Array of Documents IDs + * @param array $documentRefs Array of Documents IDs * @param string $type (html_link|pdf_link) * * @return mixed @@ -1032,7 +1042,7 @@ public function printMarkings($documentRefs, $type = 'new_html', $size = '85x85' { $documentRefs = (array) $documentRefs; $documentSize = $size === '85x85' ? '85x85' : '100x100'; - $method = 'printMarking' . $documentSize; + $method = 'printMarking'.$documentSize; // If needs link if ('html_link' == $type or 'pdf_link' == $type) { return $this->printGetLink($method, $documentRefs, $type);