1414 */
1515class NovaPoshtaApi2
1616{
17+ const API_URI = 'https://api.novaposhta.ua/v2.0 ' ;
18+
1719 /**
1820 * Key for API NovaPoshta.
1921 *
@@ -46,7 +48,7 @@ class NovaPoshtaApi2
4648 /**
4749 * @var string Areas (loaded from file, because there is no so function in NovaPoshta API 2.0)
4850 */
49- protected $ areas ;
51+ protected $ areas = '' ;
5052
5153 /**
5254 * @var string Set current model for methods save(), update(), delete()
@@ -56,27 +58,27 @@ class NovaPoshtaApi2
5658 /**
5759 * @var string Set method of current model
5860 */
59- protected $ method ;
61+ protected $ method = '' ;
6062
6163 /**
6264 * @var array Set params of current method of current model
6365 */
64- protected $ params ;
66+ protected $ params = array () ;
6567
6668 /**
6769 * Default constructor.
6870 *
6971 * @param string $key NovaPoshta API key
7072 * @param string $language Default Language
7173 * @param bool $throwErrors Throw request errors as Exceptions
72- * @param bool $connectionType Connection type (curl | file_get_contents)
74+ * @param string $connectionType Connection type (curl | file_get_contents)
7375 *
7476 * @return NovaPoshtaApi2
7577 */
7678 public function __construct ($ key , $ language = 'ru ' , $ throwErrors = false , $ connectionType = 'curl ' )
7779 {
7880 $ this ->throwErrors = $ throwErrors ;
79- return $ this
81+ $ this
8082 ->setKey ($ key )
8183 ->setLanguage ($ language )
8284 ->setConnectionType ($ connectionType )
@@ -178,17 +180,17 @@ public function getFormat()
178180 /**
179181 * Prepare data before return it.
180182 *
181- * @param json $data
183+ * @param string|array $data
182184 *
183185 * @return mixed
184186 */
185187 private function prepare ($ data )
186188 {
187- //Returns array
189+ // Returns array
188190 if ('array ' == $ this ->format ) {
189191 $ result = is_array ($ data )
190192 ? $ data
191- : json_decode ($ data , 1 );
193+ : json_decode ($ data , true );
192194 // If error exists, throw Exception
193195 if ($ this ->throwErrors and array_key_exists ('errors ' , $ result )) {
194196 throw new \Exception (is_array ($ result ['errors ' ]) ? implode ("\n" , $ result ['errors ' ]) : $ result ['errors ' ]);
@@ -202,7 +204,8 @@ private function prepare($data)
202204 /**
203205 * Converts array to xml.
204206 *
205- * @param array
207+ * @param array $array
208+ * @param \SimpleXMLElement|bool $xml
206209 */
207210 private function array2xml (array $ array , $ xml = false )
208211 {
@@ -231,8 +234,8 @@ private function request($model, $method, $params = null)
231234 {
232235 // Get required URL
233236 $ url = 'xml ' == $ this ->format
234- ? ' https://api.novaposhta.ua/v2.0 /xml/ '
235- : ' https://api.novaposhta.ua/v2.0 /json/ ' ;
237+ ? self :: API_URI . ' /xml/ '
238+ : self :: API_URI . ' /json/ ' ;
236239
237240 $ data = array (
238241 'apiKey ' => $ this ->key ,
@@ -241,23 +244,26 @@ private function request($model, $method, $params = null)
241244 'language ' => $ this ->language ,
242245 'methodProperties ' => $ params ,
243246 );
247+ $ result = array ();
244248 // Convert data to neccessary format
245249 $ post = 'xml ' == $ this ->format
246250 ? $ this ->array2xml ($ data )
247- : $ post = json_encode ($ data );
251+ : json_encode ($ data );
248252
249253 if ('curl ' == $ this ->getConnectionType ()) {
250254 $ ch = curl_init ($ url );
251- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
252- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Content-Type: ' .('xml ' == $ this ->format ? 'text/xml ' : 'application/json ' )));
253- curl_setopt ($ ch , CURLOPT_HEADER , 0 );
254- curl_setopt ($ ch , CURLOPT_POST , 1 );
255- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
256- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ post );
257- $ result = curl_exec ($ ch );
258- curl_close ($ ch );
255+ if (is_resource ($ ch )) {
256+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
257+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Content-Type: ' .('xml ' == $ this ->format ? 'text/xml ' : 'application/json ' )));
258+ curl_setopt ($ ch , CURLOPT_HEADER , 0 );
259+ curl_setopt ($ ch , CURLOPT_POST , 1 );
260+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
261+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ post );
262+ $ result = curl_exec ($ ch );
263+ curl_close ($ ch );
264+ }
259265 } else {
260- $ result = file_get_contents ($ url , null , stream_context_create (array (
266+ $ result = file_get_contents ($ url , false , stream_context_create (array (
261267 'http ' => array (
262268 'method ' => 'POST ' ,
263269 'header ' => "Content-type: application/x-www-form-urlencoded; \r\n" ,
@@ -283,8 +289,8 @@ public function model($model = '')
283289 }
284290
285291 $ this ->model = $ model ;
286- $ this ->method = null ;
287- $ this ->params = null ;
292+ $ this ->method = '' ;
293+ $ this ->params = array () ;
288294 return $ this ;
289295 }
290296
@@ -302,7 +308,7 @@ public function method($method = '')
302308 }
303309
304310 $ this ->method = $ method ;
305- $ this ->params = null ;
311+ $ this ->params = array () ;
306312 return $ this ;
307313 }
308314
@@ -403,6 +409,7 @@ public function findNearestWarehouse($searchStringArray)
403409 public function getWarehouse ($ cityRef , $ description = '' )
404410 {
405411 $ warehouses = $ this ->getWarehouses ($ cityRef );
412+ $ error = array ();
406413 $ data = array ();
407414 if (is_array ($ warehouses ['data ' ])) {
408415 if (1 === count ($ warehouses ['data ' ]) or !$ description ) {
@@ -493,16 +500,17 @@ protected function findArea(array $areas, $findByString = '', $ref = '')
493500 public function getArea ($ findByString = '' , $ ref = '' )
494501 {
495502 // Load areas list from file
496- empty ($ this ->areas ) and $ this ->areas = include dirname (__FILE__ ).'/NovaPoshtaApi2Areas.php ' ;
503+ empty ($ this ->areas ) and $ this ->areas = ( include dirname (__FILE__ ).'/NovaPoshtaApi2Areas.php ' ) ;
497504 $ data = $ this ->findArea ($ this ->areas , $ findByString , $ ref );
498505 // Error
499- empty ($ data ) and $ error = 'Area was not found ' ;
506+ $ error = array ();
507+ empty ($ data ) and $ error = array ('Area was not found ' );
500508 // Return data in same format like NovaPoshta API
501509 return $ this ->prepare (
502510 array (
503511 'success ' => empty ($ error ),
504512 'data ' => $ data ,
505- 'errors ' => ( array ) $ error ,
513+ 'errors ' => $ error ,
506514 'warnings ' => array (),
507515 'info ' => array (),
508516 )
@@ -562,20 +570,22 @@ public function getCity($cityName, $areaName = '')
562570 {
563571 // Get cities by name
564572 $ cities = $ this ->getCities (0 , $ cityName );
565- if (is_array ($ cities ['data ' ])) {
573+ $ data = array ();
574+ if (is_array ($ cities ) && is_array ($ cities ['data ' ])) {
566575 // If cities more then one, calculate current by area name
567576 $ data = (count ($ cities ['data ' ]) > 1 )
568577 ? $ this ->findCityByRegion ($ cities , $ areaName )
569- : $ cities ['data ' ][0 ];
578+ : array ( $ cities ['data ' ][0 ]) ;
570579 }
571580 // Error
572- (!$ data ) and $ error = 'City was not found ' ;
581+ $ error = array ();
582+ (!$ data ) and $ error = array ('City was not found ' );
573583 // Return data in same format like NovaPoshta API
574584 return $ this ->prepare (
575585 array (
576586 'success ' => empty ($ error ),
577- 'data ' => array ( $ data) ,
578- 'errors ' => ( array ) $ error ,
587+ 'data ' => $ data ,
588+ 'errors ' => $ error ,
579589 'warnings ' => array (),
580590 'info ' => array (),
581591 )
@@ -842,7 +852,7 @@ public function generateReport($params)
842852 /**
843853 * Check required fields for new InternetDocument and set defaults.
844854 *
845- * @param array & $counterparty Recipient info array
855+ * @param array &$counterparty Recipient info array
846856 */
847857 protected function checkInternetDocumentRecipient (array &$ counterparty )
848858 {
@@ -875,7 +885,7 @@ protected function checkInternetDocumentRecipient(array &$counterparty)
875885 /**
876886 * Check required params for new InternetDocument and set defaults.
877887 *
878- * @param array & $params
888+ * @param array &$params
879889 */
880890 protected function checkInternetDocumentParams (array &$ params )
881891 {
@@ -917,7 +927,7 @@ protected function checkInternetDocumentParams(array &$params)
917927 * 'PayerType' => (Sender|Recipient - default), 'PaymentMethod' => (NonCash|Cash - default)
918928 * 'ServiceType' => (DoorsDoors|DoorsWarehouse|WarehouseDoors|WarehouseWarehouse - default)
919929 * 'CargoType' => String
920- * @param mixed
930+ * @return mixed
921931 */
922932 public function newInternetDocument ($ sender , $ recipient , $ params )
923933 {
@@ -979,7 +989,7 @@ public function newInternetDocument($sender, $recipient, $params)
979989 * Get only link on internet document for printing.
980990 *
981991 * @param string $method Called method of NovaPoshta API
982- * @param array|string $documentRefs Array of Documents IDs
992+ * @param array $documentRefs Array of Documents IDs
983993 * @param string $type (html_link|pdf_link)
984994 *
985995 * @return mixed
@@ -1032,7 +1042,7 @@ public function printMarkings($documentRefs, $type = 'new_html', $size = '85x85'
10321042 {
10331043 $ documentRefs = (array ) $ documentRefs ;
10341044 $ documentSize = $ size === '85x85 ' ? '85x85 ' : '100x100 ' ;
1035- $ method = 'printMarking ' . $ documentSize ;
1045+ $ method = 'printMarking ' . $ documentSize ;
10361046 // If needs link
10371047 if ('html_link ' == $ type or 'pdf_link ' == $ type ) {
10381048 return $ this ->printGetLink ($ method , $ documentRefs , $ type );
0 commit comments