From 5a621310ae17863823167b5ae85f0064a450b57a Mon Sep 17 00:00:00 2001 From: segit Date: Sun, 15 May 2022 22:12:20 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=C3=90=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=BF=D0=BE=D1=87=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class_analysis.txt | 84 +++++++++++++++++ src/Delivery/Array2Xml.php | 25 ++++++ src/Delivery/NovaPoshtaApi2.php | 119 ++----------------------- src/Delivery/PrepareReturnData.php | 38 ++++++++ src/Delivery/PrintInternetDocument.php | 80 +++++++++++++++++ 5 files changed, 236 insertions(+), 110 deletions(-) create mode 100644 class_analysis.txt create mode 100644 src/Delivery/Array2Xml.php create mode 100644 src/Delivery/PrepareReturnData.php create mode 100644 src/Delivery/PrintInternetDocument.php diff --git a/class_analysis.txt b/class_analysis.txt new file mode 100644 index 0000000..3b6741a --- /dev/null +++ b/class_analysis.txt @@ -0,0 +1,84 @@ +Попытка проанализировать класс и выделить из него части в другие подклассы, +которым здесь не место. + +1. Геттеры и сеттеры. +key +connectionType +timeout +language +format (Format of returned data by methods (json, xml, array)) + +2. +prepare(); //Prepare data before return it +array2xml(); +request(); //Make request to NovaPoshta API +model(); //Set current model and empties method and params properties. +method(); //Set method of current model property and empties params properties. +params(); //Set params of current method/property property +execute(); //Execute request to NovaPoshta API + +documentsTracking(); //Get tracking information by track number + +getCities(); //Get cities of company NovaPoshta +getWarehouses(); //Get warehouses by city +getWarehousesTypes(); //Get warehouse types +findNearestWarehouse(); //Get 5 nearest warehouses by array of strings +getWarehouse(); //Get one warehouse by city name and warehouse's description +getStreet(); //Get streets list by city and/or search string +findArea(); //Find current area in list of areas +getArea(); //Get area by name or by ID +getAreas(); //Get areas list by city and/or search string +findCityByRegion(); //Find city from list by name of region +getCity(); //Get city by name and region (if it needs) +__call(); //Magic method of calling functions (uses for calling Common Model of NovaPoshta API) + + +delete(); //Delete method of current model +update(); //Update method of current model +save(); //Save method of current model + + +getCounterparties(); //getCounterparties() function of model Counterparty +cloneLoyaltyCounterpartySender(); //cloneLoyaltyCounterpartySender() function of model Counterparty +getCounterpartyContactPersons(); //getCounterpartyContactPersons() function of model Counterparty. +getCounterpartyAddresses(); //getCounterpartyAddresses() function of model Counterparty. +getCounterpartyOptions(); //getCounterpartyOptions() function of model Counterparty. +getCounterpartyByEDRPOU(); getCounterpartyByEDRPOU() function of model Counterparty. + + +getDocumentPrice(); //Get price of delivery between two cities. +getDocumentDeliveryDate(); //Get approximately date of delivery between two cities. +getDocumentList(); //Get documents list. +getDocument(); //Get document info by ID. + +generateReport(); //Generetes report by Document refs. + +checkInternetDocumentRecipients(); //Check required fields for new InternetDocument and set defaults. +checkInternetDocumentParams(); //Check required params for new InternetDocument and set defaults. +newInternetDocument(); //Create Internet Document by. + + +printGetLink(); //Get only link on internet document for printing. +printDocument(); //printDocument method of InternetDocument model. +printMarking(); //printMarkings method of InternetDocument model. + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Delivery/Array2Xml.php b/src/Delivery/Array2Xml.php new file mode 100644 index 0000000..1bc4beb --- /dev/null +++ b/src/Delivery/Array2Xml.php @@ -0,0 +1,25 @@ +'); + foreach ($array as $key => $value) { + if (is_numeric($key)) { + $key = 'item'; + } + if (is_array($value)) { + $this->array2xml($value, $xml->addChild($key)); + } else { + $xml->addChild($key, $value); + } + } + return $xml->asXML(); + } +} \ No newline at end of file diff --git a/src/Delivery/NovaPoshtaApi2.php b/src/Delivery/NovaPoshtaApi2.php index d56035d..24f5b01 100644 --- a/src/Delivery/NovaPoshtaApi2.php +++ b/src/Delivery/NovaPoshtaApi2.php @@ -1,6 +1,9 @@ format; } - /** - * Prepare data before return it. - * - * @param string|array $data - * - * @return mixed - */ - private function prepare($data) - { - // Returns array - if ('array' == $this->format) { - $result = is_array($data) - ? $data - : json_decode($data, true); - // If error exists, throw Exception - if ($this->throwErrors and array_key_exists('errors', $result) and $result['errors']) { - throw new \Exception(is_array($result['errors']) ? implode("\n", $result['errors']) : $result['errors']); - } - return $result; - } - // Returns json or xml document - return $data; - } - /** - * Converts array to xml. - * - * @param array $array - * @param \SimpleXMLElement|bool $xml - */ - private function array2xml(array $array, $xml = false) - { - (false === $xml) and $xml = new \SimpleXMLElement(''); - foreach ($array as $key => $value) { - if (is_numeric($key)) { - $key = 'item'; - } - if (is_array($value)) { - $this->array2xml($value, $xml->addChild($key)); - } else { - $xml->addChild($key, $value); - } - } - return $xml->asXML(); - } /** * Make request to NovaPoshta API. @@ -268,9 +227,13 @@ private function request($model, $method, $params = null) 'methodProperties' => $params, ); $result = array(); + + $prepare = new PrepareReturnData(); + $array2xml = new Array2Xml(); + // Convert data to neccessary format $post = 'xml' == $this->format - ? $this->array2xml($data) + ? $array2xml->array2xml($data) : json_encode($data); if ('curl' == $this->getConnectionType()) { @@ -307,7 +270,7 @@ private function request($model, $method, $params = null) ))); } - return $this->prepare($result); + return $prepare->prepare($result); } /** @@ -1047,69 +1010,5 @@ public function newInternetDocument($sender, $recipient, $params) return $this->model('InternetDocument')->save($paramsInternetDocument); } - /** - * Get only link on internet document for printing. - * - * @param string $method Called method of NovaPoshta API - * @param array $documentRefs Array of Documents IDs - * @param string $type (html_link|pdf_link) - * - * @return mixed - */ - protected function printGetLink($method, $documentRefs, $type) - { - $data = 'https://my.novaposhta.ua/orders/'.$method.'/orders[]/'.implode(',', $documentRefs) - .'/type/'.str_replace('_link', '', $type) - .'/apiKey/'.$this->key; - // Return data in same format like NovaPoshta API - return $this->prepare( - array( - 'success' => true, - 'data' => array($data), - 'errors' => array(), - 'warnings' => array(), - 'info' => array(), - ) - ); - } - - /** - * printDocument method of InternetDocument model. - * - * @param array|string $documentRefs Array of Documents IDs - * @param string $type (pdf|html|html_link|pdf_link) - * - * @return mixed - */ - public function printDocument($documentRefs, $type = 'html') - { - $documentRefs = (array) $documentRefs; - // If needs link - if ('html_link' == $type or 'pdf_link' == $type) { - return $this->printGetLink('printDocument', $documentRefs, $type); - } - // If needs data - return $this->request('InternetDocument', 'printDocument', array('DocumentRefs' => $documentRefs, 'Type' => $type)); - } - /** - * printMarkings method of InternetDocument model. - * - * @param array|string $documentRefs Array of Documents IDs - * @param string $type (pdf|new_pdf|new_html|old_html|html_link|pdf_link) - * - * @return mixed - */ - public function printMarkings($documentRefs, $type = 'new_html', $size = '85x85') - { - $documentRefs = (array) $documentRefs; - $documentSize = $size === '85x85' ? '85x85' : '100x100'; - $method = 'printMarking'.$documentSize; - // If needs link - if ('html_link' == $type or 'pdf_link' == $type) { - return $this->printGetLink($method, $documentRefs, $type); - } - // If needs data - return $this->request('InternetDocument', $method, array('DocumentRefs' => $documentRefs, 'Type' => $type)); - } } diff --git a/src/Delivery/PrepareReturnData.php b/src/Delivery/PrepareReturnData.php new file mode 100644 index 0000000..6d36a1d --- /dev/null +++ b/src/Delivery/PrepareReturnData.php @@ -0,0 +1,38 @@ +format) { + $result = is_array($data) + ? $data + : json_decode($data, true); + // If error exists, throw Exception + if ($this->throwErrors and array_key_exists('errors', $result) and $result['errors']) { + throw new \Exception(is_array($result['errors']) ? implode("\n", $result['errors']) : $result['errors']); + } + return $result; + } + // Returns json or xml document + return $data; + } +} \ No newline at end of file diff --git a/src/Delivery/PrintInternetDocument.php b/src/Delivery/PrintInternetDocument.php new file mode 100644 index 0000000..9bc8fde --- /dev/null +++ b/src/Delivery/PrintInternetDocument.php @@ -0,0 +1,80 @@ +prepare = $prepare; + } + + + /** + * Get only link on internet document for printing. + * + * @param string $method Called method of NovaPoshta API + * @param array $documentRefs Array of Documents IDs + * @param string $type (html_link|pdf_link) + * + * @return mixed + */ + protected function printGetLink($method, $documentRefs, $type) + { + $data = 'https://my.novaposhta.ua/orders/'.$method.'/orders[]/'.implode(',', $documentRefs) + .'/type/'.str_replace('_link', '', $type) + .'/apiKey/'.$this->key; + // Return data in same format like NovaPoshta API + return $this->prepare->prepare( + array( + 'success' => true, + 'data' => array($data), + 'errors' => array(), + 'warnings' => array(), + 'info' => array(), + ) + ); + } + + /** + * printDocument method of InternetDocument model. + * + * @param array|string $documentRefs Array of Documents IDs + * @param string $type (pdf|html|html_link|pdf_link) + * + * @return mixed + */ + public function printDocument($documentRefs, $type = 'html') + { + $documentRefs = (array) $documentRefs; + // If needs link + if ('html_link' == $type or 'pdf_link' == $type) { + return $this->printGetLink('printDocument', $documentRefs, $type); + } + // If needs data + return $this->request('InternetDocument', 'printDocument', array('DocumentRefs' => $documentRefs, 'Type' => $type)); + } + + /** + * printMarkings method of InternetDocument model. + * + * @param array|string $documentRefs Array of Documents IDs + * @param string $type (pdf|new_pdf|new_html|old_html|html_link|pdf_link) + * + * @return mixed + */ + public function printMarkings($documentRefs, $type = 'new_html', $size = '85x85') + { + $documentRefs = (array) $documentRefs; + $documentSize = $size === '85x85' ? '85x85' : '100x100'; + $method = 'printMarking'.$documentSize; + // If needs link + if ('html_link' == $type or 'pdf_link' == $type) { + return $this->printGetLink($method, $documentRefs, $type); + } + // If needs data + return $this->request('InternetDocument', $method, array('DocumentRefs' => $documentRefs, 'Type' => $type)); + } +} \ No newline at end of file From d01e094b21a0204fdf9fe391f5ecc04b483b6d7e Mon Sep 17 00:00:00 2001 From: segit Date: Sun, 22 May 2022 21:58:06 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B5=D1=82=D0=BD=D0=BD=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Delivery/Array2Xml.php | 4 +++- src/Delivery/NovaPoshtaApi2.php | 14 ++++++++++++++ src/Delivery/PrepareReturnData.php | 14 +++++++++++++- src/Delivery/PrintInternetDocument.php | 6 ++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Delivery/Array2Xml.php b/src/Delivery/Array2Xml.php index 1bc4beb..bf16687 100644 --- a/src/Delivery/Array2Xml.php +++ b/src/Delivery/Array2Xml.php @@ -9,7 +9,9 @@ class Array2Xml */ public function array2xml(array $array, $xml = false) { - (false === $xml) and $xml = new \SimpleXMLElement(''); + if ($xml) { + $xml = new \SimpleXMLElement(''); + } foreach ($array as $key => $value) { if (is_numeric($key)) { $key = 'item'; diff --git a/src/Delivery/NovaPoshtaApi2.php b/src/Delivery/NovaPoshtaApi2.php index 24f5b01..b3490b2 100644 --- a/src/Delivery/NovaPoshtaApi2.php +++ b/src/Delivery/NovaPoshtaApi2.php @@ -205,6 +205,10 @@ public function getFormat() + /** + * Я не могу понять нужно выносить этот метод в отдельный класс + * или он является фундаметном в этом классе NovaPoshtaApi2 + */ /** * Make request to NovaPoshta API. * @@ -231,11 +235,21 @@ private function request($model, $method, $params = null) $prepare = new PrepareReturnData(); $array2xml = new Array2Xml(); + /* + * Замечание R1KO: + * На самом деле логику преобразования данных в нужный формат стоило вынести в отдельную иерархию абстракций, как раз таки в PrepareReturnData. + * Здесь тоже самое, я не понимаю как вынести это преобразование из метода реквест. + * + */ // Convert data to neccessary format $post = 'xml' == $this->format ? $array2xml->array2xml($data) : json_encode($data); + /* + * Здесь тоже самое, я не понимаю как вынести этот транспорт из метода реквест, чтобы потом в нём использовать. + * + */ if ('curl' == $this->getConnectionType()) { $ch = curl_init($url); if (is_resource($ch)) { diff --git a/src/Delivery/PrepareReturnData.php b/src/Delivery/PrepareReturnData.php index 6d36a1d..2994e67 100644 --- a/src/Delivery/PrepareReturnData.php +++ b/src/Delivery/PrepareReturnData.php @@ -22,7 +22,7 @@ class PrepareReturnData public function prepare($data) { // Returns array - if ('array' == $this->format) { + /* if ('array' == $this->format) { $result = is_array($data) ? $data : json_decode($data, true); @@ -34,5 +34,17 @@ public function prepare($data) } // Returns json or xml document return $data; +*/ + // Returns json or xml document + if ($this->format != 'array') return $data; + + $result = is_array($data) + ? $data + : json_decode($data, true); + // If error exists, throw Exception + if ($this->throwErrors and array_key_exists('errors', $result) and $result['errors']) { + throw new \Exception(is_array($result['errors']) ? implode("\n", $result['errors']) : $result['errors']); + } + return $result; } } \ No newline at end of file diff --git a/src/Delivery/PrintInternetDocument.php b/src/Delivery/PrintInternetDocument.php index 9bc8fde..c8bb501 100644 --- a/src/Delivery/PrintInternetDocument.php +++ b/src/Delivery/PrintInternetDocument.php @@ -54,6 +54,12 @@ public function printDocument($documentRefs, $type = 'html') return $this->printGetLink('printDocument', $documentRefs, $type); } // If needs data + /* + * R1KO указал на несуществующий метод реквест. + * Но после вынесение метода printDocument из NovaPoshtaApi2 я не понял как использовать этот метод. + * Была мысль создать объект NovaPoshtaApi2 и из него вызывать реквест, но это точно не то. + * Оставляю так как и было, потому что не понял как лучше сделать. + */ return $this->request('InternetDocument', 'printDocument', array('DocumentRefs' => $documentRefs, 'Type' => $type)); }