Skip to content

Commit

Permalink
update codestyle (#37)
Browse files Browse the repository at this point in the history
* Fixed definitions and phpdoc comments
* Fixed getCity method for cities with similar names
* fixed codestyle
  • Loading branch information
lis-dev authored Aug 29, 2020
1 parent 1093fc8 commit 1c9b352
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions src/Delivery/NovaPoshtaApi2.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
class NovaPoshtaApi2
{
const API_URI = 'https://api.novaposhta.ua/v2.0';

/**
* Key for API NovaPoshta.
*
Expand Down Expand Up @@ -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()
Expand All @@ -56,27 +58,27 @@ 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.
*
* @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)
Expand Down Expand Up @@ -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']);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -283,8 +289,8 @@ public function model($model = '')
}

$this->model = $model;
$this->method = null;
$this->params = null;
$this->method = '';
$this->params = array();
return $this;
}

Expand All @@ -302,7 +308,7 @@ public function method($method = '')
}

$this->method = $method;
$this->params = null;
$this->params = array();
return $this;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1c9b352

Please sign in to comment.