Skip to content
Merged
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
77 changes: 77 additions & 0 deletions src/Data/UnitType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace NieuwbouwOffice\PhpSdk\Data;

class UnitType
{
public function __construct(
public readonly string $uuid,
public readonly ?bool $is_online,
public readonly string $title,
public readonly ?string $kind,
public readonly ?int $order,
public readonly ?string $short_description,
public readonly ?int $home_count,
public readonly ?int $media_count,
public readonly ?string $plan_part,
public readonly ?string $description,
public readonly ?string $description_html,
public readonly ?int $count,
public readonly ?int $price_from,
public readonly ?int $price_to,
public readonly ?int $lot_area_from,
public readonly ?int $lot_area_to,
public readonly ?int $living_area_from,
public readonly ?int $living_area_to,
public readonly ?int $volume_from,
public readonly ?int $volume_to,
public readonly ?int $bedrooms_from,
public readonly ?int $bedrooms_to,
public readonly ?int $rooms_from,
public readonly ?int $rooms_to,
public readonly ?string $ownership,
public readonly ?string $tenure,
) {}

public static function fromResponse(array $data): self
{
return new self(
uuid: $data['Projectwoning_UUId'],
is_online: self::toBool($data['Projectwoning_Online'] ?? null),
title: $data['Projectwoning_Titel'],
kind: $data['Woning_Type'] ?? null,
order: self::toInt($data['Projectwoning_Volgorde'] ?? null),
short_description: $data['Projectwoning_Beschrijving_Kort'] ?? null,
home_count: self::toInt($data['NrWoningen'] ?? null),
media_count: self::toInt($data['NrMedia'] ?? null),
plan_part: $data['Plandeel'] ?? null,
description: $data['Projectwoning_Beschrijving_Lang'] ?? null,
description_html: $data['Projectwoning_Beschrijving_HTML'] ?? null,
count: self::toInt($data['Projectwoning_Aantal'] ?? null),
price_from: self::toInt($data['Projectwoning_PrijsVan'] ?? null),
price_to: self::toInt($data['Projectwoning_PrijsTot'] ?? null),
lot_area_from: self::toInt($data['Projectwoning_KavelOppVan'] ?? null),
lot_area_to: self::toInt($data['Projectwoning_KavelOppTot'] ?? null),
living_area_from: self::toInt($data['Projectwoning_WoonOppVan'] ?? null),
living_area_to: self::toInt($data['Projectwoning_WoonOppTot'] ?? null),
volume_from: self::toInt($data['Projectwoning_InhoudVan'] ?? null),
volume_to: self::toInt($data['Projectwoning_InhoudTot'] ?? null),
bedrooms_from: self::toInt($data['Projectwoning_SlaapkamersVan'] ?? null),
bedrooms_to: self::toInt($data['Projectwoning_SlaapkamersTot'] ?? null),
rooms_from: self::toInt($data['Projectwoning_KamersVan'] ?? null),
rooms_to: self::toInt($data['Projectwoning_KamersTot'] ?? null),
ownership: $data['Eigendom'] ?? null,
tenure: $data['Huurkoop'] ?? null,
);
}

private static function toInt(mixed $value): ?int
{
return $value === null ? null : (int) $value;
}

private static function toBool(mixed $value): ?bool
{
return $value === null ? null : (bool) (int) $value;
}
}
7 changes: 7 additions & 0 deletions src/Requests/UnitTypes/GetUnitTypeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace NieuwbouwOffice\PhpSdk\Requests\UnitTypes;

use NieuwbouwOffice\PhpSdk\Data\UnitType;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class GetUnitTypeRequest extends Request
{
Expand All @@ -18,4 +20,9 @@ public function resolveEndpoint(): string
{
return "/projects/{$this->projectUuid}/projectwoningen/{$this->uuid}/";
}

public function createDtoFromResponse(Response $response): UnitType
{
return UnitType::fromResponse($response->json('data.object'));
}
}
13 changes: 13 additions & 0 deletions src/Requests/UnitTypes/GetUnitTypesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace NieuwbouwOffice\PhpSdk\Requests\UnitTypes;

use NieuwbouwOffice\PhpSdk\Data\UnitType;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class GetUnitTypesRequest extends Request
{
Expand All @@ -15,4 +17,15 @@ public function resolveEndpoint(): string
{
return "/projects/{$this->projectUuid}/projectwoningen/";
}

/**
* @return UnitType[]
*/
public function createDtoFromResponse(Response $response): array
{
return array_map(
fn (array $object) => UnitType::fromResponse($object),
$response->json('data.objects'),
);
}
}
13 changes: 8 additions & 5 deletions src/Resources/UnitTypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace NieuwbouwOffice\PhpSdk\Resources;

use NieuwbouwOffice\PhpSdk\Data\UnitType;
use NieuwbouwOffice\PhpSdk\Requests\UnitTypes\GetUnitTypeRequest;
use NieuwbouwOffice\PhpSdk\Requests\UnitTypes\GetUnitTypesRequest;
use Saloon\Http\BaseResource;
use Saloon\Http\Connector;
use Saloon\Http\Response;

class UnitTypeResource extends BaseResource
{
Expand All @@ -17,13 +17,16 @@ public function __construct(
parent::__construct($connector);
}

public function list(): Response
/**
* @return UnitType[]
*/
public function list(): array
{
return $this->connector->send(new GetUnitTypesRequest($this->projectUuid));
return $this->connector->send(new GetUnitTypesRequest($this->projectUuid))->dto();
}

public function get(string $uuid): Response
public function get(string $uuid): UnitType
{
return $this->connector->send(new GetUnitTypeRequest($this->projectUuid, $uuid));
return $this->connector->send(new GetUnitTypeRequest($this->projectUuid, $uuid))->dto();
}
}
61 changes: 61 additions & 0 deletions tests/Data/UnitTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use NieuwbouwOffice\PhpSdk\Data\UnitType;

it('exposes its properties as readonly public values', function () {
$unitType = new UnitType(
uuid: 'unit-1',
is_online: true,
title: 'Rug-aan-rug woning',
kind: 'Tussenwoning',
order: 1,
short_description: 'Korte beschrijving',
home_count: 22,
media_count: 7,
plan_part: 'Plandeel A',
description: 'Lange beschrijving',
description_html: '<p>Lange beschrijving</p>',
count: 22,
price_from: 317600,
price_to: 423300,
lot_area_from: 50,
lot_area_to: 80,
living_area_from: 79,
living_area_to: 79,
volume_from: 200,
volume_to: 240,
bedrooms_from: 2,
bedrooms_to: 2,
rooms_from: 3,
rooms_to: 3,
ownership: 'Eigendom',
tenure: 'Koopwoning',
);

expect($unitType->uuid)->toBe('unit-1')
->and($unitType->is_online)->toBeTrue()
->and($unitType->title)->toBe('Rug-aan-rug woning')
->and($unitType->kind)->toBe('Tussenwoning')
->and($unitType->order)->toBe(1)
->and($unitType->short_description)->toBe('Korte beschrijving')
->and($unitType->home_count)->toBe(22)
->and($unitType->media_count)->toBe(7)
->and($unitType->plan_part)->toBe('Plandeel A')
->and($unitType->description)->toBe('Lange beschrijving')
->and($unitType->description_html)->toBe('<p>Lange beschrijving</p>')
->and($unitType->count)->toBe(22)
->and($unitType->price_from)->toBe(317600)
->and($unitType->price_to)->toBe(423300)
->and($unitType->lot_area_from)->toBe(50)
->and($unitType->lot_area_to)->toBe(80)
->and($unitType->living_area_from)->toBe(79)
->and($unitType->living_area_to)->toBe(79)
->and($unitType->volume_from)->toBe(200)
->and($unitType->volume_to)->toBe(240)
->and($unitType->bedrooms_from)->toBe(2)
->and($unitType->bedrooms_to)->toBe(2)
->and($unitType->rooms_from)->toBe(3)
->and($unitType->rooms_to)->toBe(3)
->and($unitType->ownership)->toBe('Eigendom')
->and($unitType->tenure)->toBe('Koopwoning');
});
63 changes: 63 additions & 0 deletions tests/Requests/UnitTypes/GetUnitTypeRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use NieuwbouwOffice\PhpSdk\Data\UnitType;
use NieuwbouwOffice\PhpSdk\NieuwbouwOffice;
use NieuwbouwOffice\PhpSdk\Requests\UnitTypes\GetUnitTypeRequest;
use Saloon\Enums\Method;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

it('uses the GET method', function () {
expect((new GetUnitTypeRequest('proj-1', 'unit-9'))->getMethod())->toBe(Method::GET);
Expand All @@ -18,3 +22,62 @@
expect((new GetUnitTypeRequest('proj-1', 'unit-9'))->resolveEndpoint())
->toBe('/projects/proj-1/projectwoningen/unit-9/');
});

it('creates a UnitType DTO from the response', function () {
$mockClient = new MockClient([
GetUnitTypeRequest::class => MockResponse::make([
'data' => [
'object' => [
'Projectwoning_UUId' => '631c2a57a0ce5edc989c67d3661f4711',
'Projectwoning_Online' => '-1',
'Plandeel' => null,
'Projectwoning_Titel' => 'Rug-aan-rug woning',
'Woning_Type' => 'Tussenwoning',
'Projectwoning_Volgorde' => '1',
'Projectwoning_Beschrijving_Kort' => null,
'Projectwoning_Beschrijving_Lang' => null,
'Projectwoning_Beschrijving_HTML' => null,
'Projectwoning_Aantal' => '22',
'Projectwoning_PrijsVan' => '317600',
'Projectwoning_PrijsTot' => '423300',
'Projectwoning_KavelOppVan' => null,
'Projectwoning_KavelOppTot' => null,
'Projectwoning_WoonOppVan' => '79',
'Projectwoning_WoonOppTot' => '79',
'Projectwoning_InhoudVan' => null,
'Projectwoning_InhoudTot' => null,
'Projectwoning_SlaapkamersVan' => '2',
'Projectwoning_SlaapkamersTot' => '2',
'Eigendom' => 'Eigendom',
'Huurkoop' => 'Koopwoning',
'NrWoningen' => '22',
'NrMedia' => '7',
'Projectwoning_KamersVan' => null,
'Projectwoning_KamersTot' => null,
],
],
]),
]);

$connector = new NieuwbouwOffice('test-token');
$connector->withMockClient($mockClient);

$unitType = $connector->send(new GetUnitTypeRequest('proj-1', '631c2a57a0ce5edc989c67d3661f4711'))->dto();

expect($unitType)->toBeInstanceOf(UnitType::class)
->and($unitType->uuid)->toBe('631c2a57a0ce5edc989c67d3661f4711')
->and($unitType->is_online)->toBeTrue()
->and($unitType->title)->toBe('Rug-aan-rug woning')
->and($unitType->kind)->toBe('Tussenwoning')
->and($unitType->order)->toBe(1)
->and($unitType->count)->toBe(22)
->and($unitType->price_from)->toBe(317600)
->and($unitType->price_to)->toBe(423300)
->and($unitType->living_area_from)->toBe(79)
->and($unitType->bedrooms_from)->toBe(2)
->and($unitType->rooms_from)->toBeNull()
->and($unitType->ownership)->toBe('Eigendom')
->and($unitType->tenure)->toBe('Koopwoning')
->and($unitType->home_count)->toBe(22)
->and($unitType->media_count)->toBe(7);
});
58 changes: 58 additions & 0 deletions tests/Requests/UnitTypes/GetUnitTypesRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use NieuwbouwOffice\PhpSdk\Data\UnitType;
use NieuwbouwOffice\PhpSdk\NieuwbouwOffice;
use NieuwbouwOffice\PhpSdk\Requests\UnitTypes\GetUnitTypesRequest;
use Saloon\Enums\Method;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

it('uses the GET method', function () {
expect((new GetUnitTypesRequest('proj-1'))->getMethod())->toBe(Method::GET);
Expand All @@ -15,3 +19,57 @@
expect((new GetUnitTypesRequest('proj-1'))->resolveEndpoint())
->toBe('/projects/proj-1/projectwoningen/');
});

it('creates an array of UnitType DTOs from the response', function () {
$mockClient = new MockClient([
GetUnitTypesRequest::class => MockResponse::make([
'data' => [
'objects' => [
[
'Projectwoning_UUId' => 'dc28a597e2661a3bf7f84737eb1f38c9',
'Projectwoning_Online' => '-1',
'Projectwoning_Titel' => 'Gezinswoning M',
'Woning_Type' => 'Tussenwoning',
'Projectwoning_Volgorde' => '2',
'Projectwoning_Beschrijving_Kort' => null,
'NrWoningen' => '2',
'NrMedia' => '6',
],
[
'Projectwoning_UUId' => 'a8caa9dc5a353a938440361e8005d06d',
'Projectwoning_Online' => '-1',
'Projectwoning_Titel' => 'Appartement',
'Woning_Type' => 'Appartement',
'Projectwoning_Volgorde' => '5',
'Projectwoning_Beschrijving_Kort' => null,
'NrWoningen' => '14',
'NrMedia' => '5',
],
],
],
'meta' => ['count' => 2],
]),
]);

$connector = new NieuwbouwOffice('test-token');
$connector->withMockClient($mockClient);

$unitTypes = $connector->send(new GetUnitTypesRequest('proj-1'))->dto();

expect($unitTypes)->toBeArray()->toHaveCount(2)
->and($unitTypes[0])->toBeInstanceOf(UnitType::class)
->and($unitTypes[0]->uuid)->toBe('dc28a597e2661a3bf7f84737eb1f38c9')
->and($unitTypes[0]->is_online)->toBeTrue()
->and($unitTypes[0]->title)->toBe('Gezinswoning M')
->and($unitTypes[0]->kind)->toBe('Tussenwoning')
->and($unitTypes[0]->order)->toBe(2)
->and($unitTypes[0]->home_count)->toBe(2)
->and($unitTypes[0]->media_count)->toBe(6)
->and($unitTypes[0]->price_from)->toBeNull()
->and($unitTypes[1])->toBeInstanceOf(UnitType::class)
->and($unitTypes[1]->uuid)->toBe('a8caa9dc5a353a938440361e8005d06d')
->and($unitTypes[1]->title)->toBe('Appartement')
->and($unitTypes[1]->kind)->toBe('Appartement')
->and($unitTypes[1]->order)->toBe(5)
->and($unitTypes[1]->home_count)->toBe(14);
});
Loading
Loading