Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
cmesptchr committed Jun 30, 2020
1 parent 0ace844 commit 4addc03
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 19 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Spatie bvba <info@spatie.be>
Copyright (c) Ptchr BV <info@ptchr.nl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Total Downloads](https://img.shields.io/packagist/dt/cmesptchr/postcode-service.svg?style=flat-square)](https://packagist.org/packages/cmesptchr/postcode-service)


Pro6pp v2 API implementation
[API docs]((https://pro6pp.nl/docs/v2/redoc))

## Installation

Expand Down
21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
{
"name": "spatie/postcode-service",
"name": "cmesptchr/postcode-service",
"description": "Pro6pp v2 API implementation",
"keywords": [
"spatie",
"postcode-service"
"postcode",
"api",
"pro6pp",
"php"
],
"homepage": "https://github.com/spatie/postcode-service",
"homepage": "https://github.com/cmesptchr/postcode-service",
"license": "MIT",
"authors": [
{
"name": "Christiaan Mes",
"email": "[email protected]",
"homepage": "https://spatie.be",
"homepage": "https://ptchr.nl",
"role": "Developer"
}
],
"require": {
"php": "^7.4"
"php": "^7.4",
"guzzlehttp/guzzle": "^7.0",
"ext-json": "*"
},
"require-dev": {
"roave/security-advisories": "dev-master",
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^3.11"
},
"autoload": {
"psr-4": {
"Spatie\\Skeleton\\": "src"
"Ptchr\\PostalCodeService\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Spatie\\Skeleton\\Tests\\": "tests"
"Ptchr\\PostalCodeService\\Tests\\": "tests"
}
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Spatie Test Suite">
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/AddressNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Ptchr\PostalCodeService\Exceptions;

class AddressNotFoundException extends \Exception
{

}
7 changes: 7 additions & 0 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ptchr\PostalCodeService\Exceptions;

class BadRequestException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Exceptions/InvalidPostalCodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ptchr\PostalCodeService\Exceptions;

class InvalidPostalCodeException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ptchr\PostalCodeService\Exceptions;

class UnauthorizedException extends \Exception
{
}
215 changes: 215 additions & 0 deletions src/Pro6pp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

namespace Ptchr\PostalCodeService;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Ptchr\PostalCodeService\Exceptions\AddressNotFoundException;
use Ptchr\PostalCodeService\Exceptions\BadRequestException;
use Ptchr\PostalCodeService\Exceptions\InvalidPostalCodeException;
use Ptchr\PostalCodeService\Exceptions\UnauthorizedException;

class Pro6pp
{
/**
* @var string
*/
private string $apiKey;

/**
* @var Client
*/
private $guzzle;

/**
* @var string
*/
private string $baseUrl = 'https://api.pro6pp.nl/v2/';

/**
* Pro6pp constructor.
* @param string $apiKey
*/
public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
$this->guzzle = new Client();
}

/**
* @param string $postalCode
* @param string|int $houseNumber
* @param string|null $city
* @param string|null $street
* @param string|null $addition
* @return array
* @throws AddressNotFoundException
* @throws BadRequestException
* @throws GuzzleException
* @throws InvalidPostalCodeException
* @throws UnauthorizedException
*/
public function autocomplete(
string $postalCode,
string $houseNumber,
string $city = null,
string $street = null,
string $addition = null
): array {
$parameters = [];

if ($houseNumber !== null) {
$parameters['streetNumber'] = str_replace(' ', '', $houseNumber);
}

$postalCode = str_replace(' ', '', $postalCode);
$postCodeContainsOnlyNumbers = (bool) preg_match('/^[0-9]+$/', $postalCode);

$country = null;

switch (strlen($postalCode)) {
case strlen($postalCode) === 6 && ! $postCodeContainsOnlyNumbers:
$country = 'nl';
$parameters['postalCode'] = $postalCode;

break;
case $city !== null && $street !== null && strlen($postalCode) === 5 && $postCodeContainsOnlyNumbers:
$country = 'de';
$parameters['postalCode'] = $postalCode;

break;
case $street !== null && strlen($postalCode) === 4 && $postCodeContainsOnlyNumbers:
$country = 'be';
$parameters['postalCode'] = $postalCode;

break;
default:
throw new InvalidPostalCodeException('Cannot match Country postal code format for: '.$postalCode);

break;
}

if ($city !== null) {
$parameters['settlement'] = $city;
}

if ($addition !== null) {
$parameters['premise'] = $addition;
}

if ($street !== null) {
$parameters['street'] = $street;
}

try {
return $this->request('get', 'autocomplete/'.$country.'/', $parameters);
} catch (ClientException $exception) {
$statusCode = $exception->getResponse()->getStatusCode();

if ($statusCode === 404) {
$message = 'No address found for postal code: '.$postalCode.' with house number: '.$houseNumber;

throw new AddressNotFoundException($message, $exception->getCode(), $exception);
}

if ($statusCode === 400) {
throw new BadRequestException('Invalid request', $exception->getCode(), $exception);
}

if ($statusCode === 401) {
throw new UnauthorizedException('Unauthorized', $exception->getCode(), $exception);
}
}
}

/**
* @param string $countryCode
* @param string|int $postalCode
* @param int $maxResults
* @return array
* @throws BadRequestException
* @throws GuzzleException
* @throws UnauthorizedException
*/
public function suggestAddressesByPostalCode(string $countryCode, $postalCode, int $maxResults = 10): array
{
$parameters = [
'postalCode' => $postalCode,
'maxResults' => $maxResults,
];

try {
return $this->request('get', 'suggest/'.strtolower($countryCode).'/postalCode', $parameters);
} catch (ClientException $exception) {
$statusCode = $exception->getResponse()->getStatusCode();

if ($statusCode === 400) {
throw new BadRequestException('Invalid request', $exception->getCode(), $exception);
}

if ($statusCode === 401) {
throw new UnauthorizedException('Unauthorized', $exception->getCode(), $exception);
}
}
}

/**
* @param string $countryCode
* @param string|int $cityName
* @param int $maxResults
* @return array
* @throws BadRequestException
* @throws GuzzleException
* @throws UnauthorizedException
*/
public function suggestCitiesByName(string $countryCode, string $cityName, int $maxResults = 10): array
{
$parameters = [
'settlement' => $cityName,
'maxResults' => $maxResults,
];

try {
return $this->request('get', 'suggest/'.strtolower($countryCode).'/settlement', $parameters);
} catch (ClientException $exception) {
$statusCode = $exception->getResponse()->getStatusCode();

if ($statusCode === 400) {
throw new BadRequestException('Invalid request', $exception->getCode(), $exception);
}

if ($statusCode === 401) {
throw new UnauthorizedException('Unauthorized', $exception->getCode(), $exception);
}
}
}

/**
* @param string $method
* @param string $endpoint
* @param array $requestParameters
* @return array
* @throws GuzzleException
*/
private function request(string $method, string $endpoint, array $requestParameters): array
{
$requestParameters['authKey'] = $this->apiKey;

$response = $this->guzzle->request($method, $this->baseUrl.$endpoint, [
'query' => $requestParameters,
]);

return $this->formatResponseData($response);
}

/**
* @param ResponseInterface $response
* @return array
*/
private function formatResponseData(ResponseInterface $response): array
{
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
}
}
7 changes: 0 additions & 7 deletions src/SkeletonClass.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Spatie\Skeleton\Tests;
namespace Ptchr\PostalCodeService\Tests;

use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit 4addc03

Please sign in to comment.