Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
CRAP | |
75.00% |
3 / 4 |
| Search | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2.06 | |
75.00% |
3 / 4 |
| getAddressByZipCode | |
0.00% |
0 / 1 |
2.06 | |
75.00% |
3 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Luisccomp\DigitalCep; |
| 4 | |
| 5 | use Exception; |
| 6 | |
| 7 | class Search |
| 8 | { |
| 9 | private string $url = 'https://viacep.com.br/ws/'; |
| 10 | |
| 11 | /** |
| 12 | * Given a zip code, this method will return detailed address information from a |
| 13 | * address service API. The return will be at key => value form. |
| 14 | */ |
| 15 | public function getAddressByZipCode(string $zipCode) |
| 16 | { |
| 17 | if (strlen($zipCode) == 0) |
| 18 | { |
| 19 | throw new Exception('$zipCode cannot be an empty string!'); |
| 20 | } |
| 21 | |
| 22 | $zipCode = preg_replace('/[^0-9]/im', '', $zipCode); |
| 23 | |
| 24 | return (array) json_decode(file_get_contents($this->url . $zipCode . '/json')); |
| 25 | } |
| 26 | } |