Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postal code optional #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/getMoreCIFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
echo $company->getAddress()->getCounty();
echo $company->getAddress()->getStreet();
echo $company->getAddress()->getStreetNumber();
echo $company->getAddress()->getPostalCode();

// ... etc
}
3 changes: 2 additions & 1 deletion examples/getOneCIF.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
echo $company->getAddress()->getCounty();
echo $company->getAddress()->getStreet();
echo $company->getAddress()->getStreetNumber();
echo $company->getAddress()->getPostalCode();

// ... etc
// ... etc
4 changes: 2 additions & 2 deletions src/Models/CompanyAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down
60 changes: 28 additions & 32 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Parser
{
/** @var array */
private $data = [];
private $address = [];

/**
* Parser constructor.
Expand All @@ -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;
}

/**
Expand All @@ -63,12 +67,4 @@ public function getData(): array
{
return $this->data;
}

/**
* @return string
*/
public function getPostalCode()
{
return $this->data['codPostal'];
}
}
}
1 change: 1 addition & 0 deletions tests/Integrations/FlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
4 changes: 1 addition & 3 deletions tests/Unit/Models/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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());
Expand Down