From 0538f30d24ffc771720593ee3543c58a845cbe4d Mon Sep 17 00:00:00 2001 From: Arshavin Date: Mon, 1 Aug 2022 15:32:20 +0300 Subject: [PATCH 1/2] Parse ANAF data only once Also: - field 'postalCode' made optional; - unnecessary method getPostalCode() removed. --- src/Models/CompanyAddress.php | 4 +-- src/Parser.php | 60 ++++++++++++++++------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/Models/CompanyAddress.php b/src/Models/CompanyAddress.php index bbbac08..67ae037 100644 --- a/src/Models/CompanyAddress.php +++ b/src/Models/CompanyAddress.php @@ -52,9 +52,9 @@ public function getStreetNumber(): string /** * @return string */ - public function getPostalCode() + public function getPostalCode(): ?string { - return $this->parser->getPostalCode(); + return $this->parser->getAddress()['postalCode']; } /** diff --git a/src/Parser.php b/src/Parser.php index 4fe6938..b136c86 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -5,6 +5,7 @@ class Parser { /** @var array */ private $data = []; + private $address = []; /** * Parser constructor. @@ -13,38 +14,41 @@ class Parser public function __construct(array $data) { $this->data = $data; - } - /** - * @return array - */ - public function getAddress(): array - { - $address = []; + if (!empty($data['adresa'])) { + // Normal case from all uppercase + $rawText = mb_convert_case($data['adresa'], MB_CASE_TITLE, 'UTF-8'); - // Normal case from all uppercase - $rawText = mb_convert_case($this->data['adresa'], MB_CASE_TITLE, 'UTF-8'); + // Parse address + $list = array_map('trim', explode(",", $rawText, 5)); + list($county, $city, $street, $number, $other) = array_pad($list, 5, ''); - // Parse address - $list = array_map('trim', explode(",", $rawText, 5)); - list($county, $city, $street, $number, $other) = array_pad($list, 5, ''); + // Parse county + $this->address['county'] = trim(str_replace('Jud.', '', $county)); - // Parse county - $address['county'] = trim(str_replace('Jud.', '', $county)); + // Parse city + $this->address['city'] = trim(str_replace(['Mun.', 'Orş.'], ['', 'Oraş'], $city)); - // Parse city - $address['city'] = trim(str_replace(['Mun.', 'Orş.'], ['', 'Oraş'], $city)); + // Parse street + $this->address['street'] = trim(str_replace('Str.', '', $street)); - // Parse street - $address['street'] = trim(str_replace('Str.', '', $street)); + // Parse street number + $this->address['streetNumber'] = trim(str_replace('Nr.', '', $number)); - // Parse street number - $address['streetNumber'] = trim(str_replace('Nr.', '', $number)); + // Parse others + $this->address['others'] = trim($other); + } - // Parse others - $address['others'] = trim($other); + // Parse postal code + $this->address['postalCode'] = $data['codPostal'] ?? NULL; + } - return $address; + /** + * @return array + */ + public function getAddress(): array + { + return $this->address; } /** @@ -63,12 +67,4 @@ public function getData(): array { return $this->data; } - - /** - * @return string - */ - public function getPostalCode() - { - return $this->data['codPostal']; - } -} \ No newline at end of file +} From fdd8bb3ee7efb7971e40d2ce0b6f07edca18460f Mon Sep 17 00:00:00 2001 From: Arshavin Date: Mon, 1 Aug 2022 15:32:59 +0300 Subject: [PATCH 2/2] Test 'postalCode' field --- examples/getMoreCIFs.php | 1 + examples/getOneCIF.php | 3 ++- tests/Integrations/FlowTest.php | 1 + tests/Unit/Models/CompanyTest.php | 4 +--- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/getMoreCIFs.php b/examples/getMoreCIFs.php index 2190654..26d178d 100644 --- a/examples/getMoreCIFs.php +++ b/examples/getMoreCIFs.php @@ -16,6 +16,7 @@ echo $company->getAddress()->getCounty(); echo $company->getAddress()->getStreet(); echo $company->getAddress()->getStreetNumber(); + echo $company->getAddress()->getPostalCode(); // ... etc } diff --git a/examples/getOneCIF.php b/examples/getOneCIF.php index 59d7c35..25388f9 100644 --- a/examples/getOneCIF.php +++ b/examples/getOneCIF.php @@ -14,5 +14,6 @@ echo $company->getAddress()->getCounty(); echo $company->getAddress()->getStreet(); echo $company->getAddress()->getStreetNumber(); +echo $company->getAddress()->getPostalCode(); -// ... etc \ No newline at end of file +// ... etc diff --git a/tests/Integrations/FlowTest.php b/tests/Integrations/FlowTest.php index b2dcd56..2d8ae4d 100644 --- a/tests/Integrations/FlowTest.php +++ b/tests/Integrations/FlowTest.php @@ -47,5 +47,6 @@ public function testAddressParser() $this->assertEquals("Oraş Voluntari", $results->getAddress()->getCity()); $this->assertEquals("Şos. Bucureşti Nord", $results->getAddress()->getStreet()); $this->assertEquals("15-23", $results->getAddress()->getStreetNumber()); + $this->assertEquals("057003", $results->getAddress()->getPostalCode()); } } diff --git a/tests/Unit/Models/CompanyTest.php b/tests/Unit/Models/CompanyTest.php index 990075a..13d3bda 100644 --- a/tests/Unit/Models/CompanyTest.php +++ b/tests/Unit/Models/CompanyTest.php @@ -24,6 +24,7 @@ public function testCompanyDetails() 'city' => 'Sector 1', 'street' => 'Şos. Bucureşti-Ploieşti', 'streetNumber' => '172-176', + 'postalCode' => '057003' ]; $parset->expects($this->any()) @@ -37,9 +38,6 @@ public function testCompanyDetails() 'denumire' => 'Test', 'telefon' => 07676000000, ])); - $parset->expects($this->any()) - ->method("getPostalCode") - ->will($this->returnValue("057003")); $company = new Company($parset); $this->assertEquals(123456, $company->getCIF());