Skip to content
Draft
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
76 changes: 19 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Register for free to obtain a **test API key** at [nederlandpostcode.nl](https:/
- [Address Endpoint](#address-endpoint)
- [Single Address](#single-address)
- [Multiple Addresses](#multiple-addresses)
- [Energy Label Endpoint](#energy-label-endpoint)
- [Quota Endpoint](#quota-endpoint)
- [Error Handling](#error-handling)

Expand Down Expand Up @@ -124,67 +125,28 @@ $address = $client->list(
);
```

This will return an `AddressCollection` object like this:
This will return an `AddressCollection`, which is a collection of `Address` objects.

### Energy Label Endpoint

The energy label endpoint allows you to fetch energy label information for a given postcode and house number.

```php
AddressCollection {
items: [
Address {
postcode: "1015CN",
number: 10,
addition: 'A',
street: "Keizersgracht",
city: "Amsterdam",
municipality: "Amsterdam",
province: "Noord-Holland",
coordinates: Coordinates {
latitude: 52.379401496779124,
longitude: 4.889216673725493
}
},
Address {
postcode: "1015CN",
number: 10,
addition: 'B',
street: "Keizersgracht",
city: "Amsterdam",
municipality: "Amsterdam",
province: "Noord-Holland",
coordinates: Coordinates {
latitude: 52.379401496779124,
longitude: 4.889216673725493
}
},
Address {
postcode: "1015CN",
number: 10,
addition: 'C',
street: "Keizersgracht",
city: "Amsterdam",
municipality: "Amsterdam",
province: "Noord-Holland",
coordinates: Coordinates {
latitude: 52.379401496779124,
longitude: 4.889216673725493
}
},
Address {
postcode: "1015CN",
number: 10,
addition: 'D',
street: "Keizersgracht",
city: "Amsterdam",
municipality: "Amsterdam",
province: "Noord-Holland",
coordinates: Coordinates {
latitude: 52.379401496779124,
longitude: 4.889216673725493
}
},
]
}
use Label84\NederlandPostcode\NederlandPostcodeClient;

$client = new NederlandPostcodeClient(
key: 'npa_live_XXX'
);

$energyLabels = $client->energyLabels()->get(
postcode: '1118BN',
number: 800,
addition: null,
);
```

This will return an `EnergyLabelCollection` object, which is a collection of `EnergyLabel` objects.

### Quota Endpoint

The quota endpoint allows you to check your current API usage and limits. This endpoint does not increment your usage count.
Expand Down
24 changes: 24 additions & 0 deletions src/DTO/EnergyLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Label84\NederlandPostcode\DTO;

use DateTime;

class EnergyLabel
{
public function __construct(
public readonly string $postcode,
public readonly int $number,
public readonly ?string $addition,
public readonly string $street,
public readonly string $city,
public readonly DateTime $inspectionDate,
public readonly DateTime $validUntilDate,
public readonly string $constructionType,
public readonly ?string $buildingType,
public readonly string $energyLabel,
public readonly ?float $maxEnergyDemand,
public readonly ?float $maxFossilEnergyDemand,
public readonly ?float $minRenewableShare,
) {}
}
42 changes: 42 additions & 0 deletions src/DTO/EnergyLabelCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Label84\NederlandPostcode\DTO;

use ArrayIterator;
use Countable;
use IteratorAggregate;

/** @implements IteratorAggregate<EnergyLabel> */
class EnergyLabelCollection implements IteratorAggregate, Countable
{
/** @var EnergyLabel[] */
protected array $items = [];

/** @param EnergyLabel[] $items */
public function __construct(array $items = [])
{
$this->items = $items;
}

/** @return EnergyLabel[] */
public function all(): array
{
return $this->items;
}

public function count(): int
{
return count($this->items);
}

/** @return ArrayIterator<int, EnergyLabel> */
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}

public function isEmpty(): bool
{
return empty($this->items);
}
}
60 changes: 60 additions & 0 deletions src/Factories/EnergyLabelCollectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Label84\NederlandPostcode\Factories;

use DateTime;
use Label84\NederlandPostcode\DTO\EnergyLabel;
use Label84\NederlandPostcode\DTO\EnergyLabelCollection;

class EnergyLabelCollectionFactory
{
/**
* @param array{data: array{
* postcode: string,
* number: int,
* addition?: string|null,
* street: string,
* city: string,
* energy_labels: array<array{
* inspection_date: string,
* valid_until_date: string,
* construction_type: string,
* building_type: string|null,
* energy_label: string,
* max_energy_demand: float,
* max_fossil_energy_demand: float,
* min_renewable_share: float,
* }>,
* }} $response
*/
public static function make(array $response): EnergyLabelCollection
{
$postcode = $response['data']['postcode'];
$number = $response['data']['number'];
$addition = $response['data']['addition'] ?? null;
$street = $response['data']['street'];
$city = $response['data']['city'];

$energyLabels = array_map(function (array $attributes) use ($postcode, $number, $addition, $street, $city) {
return new EnergyLabel(
postcode: $postcode,
number: $number,
addition: $addition,
street: $street,
city: $city,
inspectionDate: new DateTime($attributes['inspection_date']),
validUntilDate: new DateTime($attributes['valid_until_date']),
constructionType: $attributes['construction_type'],
buildingType: $attributes['building_type'],
energyLabel: $attributes['energy_label'],
maxEnergyDemand: $attributes['max_energy_demand'],
maxFossilEnergyDemand: $attributes['max_fossil_energy_demand'],
minRenewableShare: $attributes['min_renewable_share'],
);
}, $response['data']['energy_labels']);

$energyLabels = array_values($energyLabels);

return new EnergyLabelCollection($energyLabels);
}
}
7 changes: 7 additions & 0 deletions src/NederlandPostcodeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Label84\NederlandPostcode\Exceptions\AddressNotFoundException;
use Label84\NederlandPostcode\Exceptions\MultipleAddressesFoundException;
use Label84\NederlandPostcode\Resources\AddressesResource;
use Label84\NederlandPostcode\Resources\EnergyLabelResource;
use Label84\NederlandPostcode\Resources\QuotaResource;

/**
* @method AddressesResource addresses()
* @method QuotaResource quota()
* @method EnergyLabelResource energyLabels()
* @method AddressCollection<Address> list(string $postcode, int $number, ?string $addition = null, array<int|string, string|AddressAttributesEnum> $attributes = [])
* @method Address find(string $postcode, int $number, ?string $addition = null, array<int|string, string|AddressAttributesEnum> $attributes = [])
* @method Quota usage()
Expand Down Expand Up @@ -62,6 +64,11 @@ public function addresses(): AddressesResource
return new AddressesResource($this);
}

public function energyLabels(): EnergyLabelResource
{
return new EnergyLabelResource($this);
}

public function quota(): QuotaResource
{
return new QuotaResource($this);
Expand Down
26 changes: 26 additions & 0 deletions src/Resources/EnergyLabelResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Label84\NederlandPostcode\Resources;

use Label84\NederlandPostcode\DTO\EnergyLabelCollection;
use Label84\NederlandPostcode\Factories\EnergyLabelCollectionFactory;

class EnergyLabelResource extends BaseResource
{
public function get(
string $postcode,
int $number,
?string $addition,
): EnergyLabelCollection {
// @phpstan-ignore-next-line
return EnergyLabelCollectionFactory::make($this->request(
method: 'GET',
path: 'v1/energy-label',
query: [
'postcode' => $postcode,
'number' => $number,
'addition' => $addition,
],
));
}
}
37 changes: 37 additions & 0 deletions tests/Resources/EnergyLabelResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Label84\NederlandPostcode\Tests\Resources;

use Label84\NederlandPostcode\DTO\EnergyLabelCollection;
use Label84\NederlandPostcode\Tests\TestCase;

class EnergyLabelResourceTest extends TestCase
{
public function test_get_single_result(): void
{
$result = $this->client
->energyLabels()
->get(
postcode: '1118BN',
number: 800,
addition: null,
);

$this->assertInstanceOf(EnergyLabelCollection::class, $result);
$this->assertCount(1, $result);
}

public function test_get_multiple_results(): void
{
$result = $this->client
->energyLabels()
->get(
postcode: '1015CN',
number: 10,
addition: 'A',
);

$this->assertInstanceOf(EnergyLabelCollection::class, $result);
$this->assertCount(2, $result);
}
}
Loading