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..bf16687 --- /dev/null +++ b/src/Delivery/Array2Xml.php @@ -0,0 +1,27 @@ +'); + } + 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..b3490b2 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 + * Я не могу понять нужно выносить этот метод в отдельный класс + * или он является фундаметном в этом классе NovaPoshtaApi2 */ - 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,11 +231,25 @@ private function request($model, $method, $params = null) 'methodProperties' => $params, ); $result = array(); + + $prepare = new PrepareReturnData(); + $array2xml = new Array2Xml(); + + /* + * Замечание R1KO: + * На самом деле логику преобразования данных в нужный формат стоило вынести в отдельную иерархию абстракций, как раз таки в PrepareReturnData. + * Здесь тоже самое, я не понимаю как вынести это преобразование из метода реквест. + * + */ // Convert data to neccessary format $post = 'xml' == $this->format - ? $this->array2xml($data) + ? $array2xml->array2xml($data) : json_encode($data); + /* + * Здесь тоже самое, я не понимаю как вынести этот транспорт из метода реквест, чтобы потом в нём использовать. + * + */ if ('curl' == $this->getConnectionType()) { $ch = curl_init($url); if (is_resource($ch)) { @@ -307,7 +284,7 @@ private function request($model, $method, $params = null) ))); } - return $this->prepare($result); + return $prepare->prepare($result); } /** @@ -1047,69 +1024,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..2994e67 --- /dev/null +++ b/src/Delivery/PrepareReturnData.php @@ -0,0 +1,50 @@ +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; +*/ + // 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 new file mode 100644 index 0000000..c8bb501 --- /dev/null +++ b/src/Delivery/PrintInternetDocument.php @@ -0,0 +1,86 @@ +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 + /* + * R1KO указал на несуществующий метод реквест. + * Но после вынесение метода printDocument из NovaPoshtaApi2 я не понял как использовать этот метод. + * Была мысль создать объект NovaPoshtaApi2 и из него вызывать реквест, но это точно не то. + * Оставляю так как и было, потому что не понял как лучше сделать. + */ + 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