Skip to content

Commit 3a0861a

Browse files
committed
WIP
1 parent ed88dad commit 3a0861a

File tree

6 files changed

+179
-6
lines changed

6 files changed

+179
-6
lines changed

src/DTO/Agency.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Flatfox\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
7+
class Agency
8+
{
9+
public function __construct(
10+
public string|null $name,
11+
public string|null $name_2,
12+
public string|null $street,
13+
public string|null $zipcode,
14+
public string|null $city,
15+
public string|null $country,
16+
public string|null $logo_url,
17+
public string|null $logo_url_org_logo_m,
18+
) {
19+
}
20+
21+
public static function fromJson(array $data): self
22+
{
23+
return new self(
24+
name: Arr::get($data, 'name'),
25+
name_2: Arr::get($data, 'name'),
26+
street: Arr::get($data, 'street'),
27+
zipcode: Arr::get($data, 'zipcode'),
28+
city: Arr::get($data, 'city'),
29+
country: Arr::get($data, 'country'),
30+
logo_url: Arr::get($data, 'logo.url'),
31+
logo_url_org_logo_m: Arr::get($data, 'logo.url_org_logo_m'),
32+
);
33+
}
34+
}

src/DTO/Attribute.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodebarAg\Flatfox\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
7+
class Attribute
8+
{
9+
public function __construct(
10+
public string|null $name,
11+
) {
12+
}
13+
14+
public static function fromJson(array $data): self
15+
{
16+
return new self(
17+
name: Arr::get($data, 'name'),
18+
);
19+
}
20+
}

src/DTO/Document.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CodebarAg\Flatfox\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
7+
class Document
8+
{
9+
public function __construct(
10+
public int|null $pk,
11+
public string|null $url,
12+
public int|null $ordering,
13+
public string|null $caption,
14+
) {
15+
}
16+
17+
public static function fromJson(array $data): self
18+
{
19+
return new self(
20+
pk: Arr::get($data, 'pk'),
21+
url: Arr::get($data, 'url'),
22+
ordering: Arr::get($data, 'ordering'),
23+
caption: Arr::get($data, 'caption'),
24+
);
25+
}
26+
}

src/DTO/Image.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace CodebarAg\Flatfox\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
7+
class Image
8+
{
9+
public function __construct(
10+
public int|null $pk,
11+
public string|null $caption,
12+
public string|null $url,
13+
public string|null $url_thumb_m,
14+
public string|null $url_listing_search,
15+
public string|null $search_url,
16+
public int|null $ordering,
17+
public int|null $width,
18+
public int|null $height,
19+
) {
20+
}
21+
22+
public static function fromJson(array $data): self
23+
{
24+
return new self(
25+
pk: Arr::get($data, 'pk'),
26+
caption: Arr::get($data, 'caption'),
27+
url: Arr::get($data, 'url'),
28+
url_thumb_m: Arr::get($data, 'url_thumb_m'),
29+
url_listing_search: Arr::get($data, 'url_listing_search'),
30+
search_url: Arr::get($data, 'search_url'),
31+
ordering: Arr::get($data, 'ordering'),
32+
width: Arr::get($data, 'width'),
33+
height: Arr::get($data, 'height'),
34+
);
35+
}
36+
}

src/DTO/User.php renamed to src/DTO/Listing.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
78

8-
class User
9+
class Listing
910
{
1011
public function __construct(
1112
public int $pk,
@@ -41,7 +42,7 @@ public function __construct(
4142
public int|null $space_display,
4243
public string|null $number_of_rooms,
4344
public int|null $floor,
44-
public null $attributes,
45+
public Collection|null $attributes,
4546
public bool $is_furnished,
4647
public bool $is_temporary,
4748
public bool $is_selling_furniture,
@@ -54,6 +55,18 @@ public function __construct(
5455
public int|null $year_built,
5556
public int|null $year_renovated,
5657
public string|null $moving_date_type,
58+
public Carbon|null $moving_date,
59+
public string|null $video_url,
60+
public string|null $tour_url,
61+
public string|null $website_url,
62+
public string|null $live_viewing_url,
63+
public int|null $cover_image,
64+
public null $images,
65+
public null $documents,
66+
public null $agency,
67+
public bool $reserved,
68+
public string|null $rent_title,
69+
public int|null $livingspace,
5770
public Carbon|null $published,
5871
public Carbon|null $created,
5972
) {
@@ -96,7 +109,7 @@ public static function fromJson(array $data): self
96109
space_display: Arr::get($data, 'space_display'),
97110
number_of_rooms: Arr::get($data, 'number_of_rooms'),
98111
floor: Arr::get($data, 'floor'),
99-
attributes: null,
112+
attributes: Arr::has($data, 'attributes') ? self::attributes(Arr::get($data, 'attributes')) : null,
100113
is_furnished: Arr::get($data, 'is_furnished'),
101114
is_temporary: Arr::get($data, 'is_temporary'),
102115
is_selling_furniture: Arr::get($data, 'is_selling_furniture'),
@@ -109,9 +122,44 @@ public static function fromJson(array $data): self
109122
year_built: Arr::get($data, 'year_built'),
110123
year_renovated: Arr::get($data, 'year_renovated'),
111124
moving_date_type: Arr::get($data, 'moving_date_type'),
112-
125+
video_url: Arr::get($data, 'video_url'),
126+
tour_url: Arr::get($data, 'tour_url'),
127+
website_url: Arr::get($data, 'website_url'),
128+
live_viewing_url: Arr::get($data, 'live_viewing_url'),
129+
cover_image: Arr::get($data, 'cover_image'),
130+
images: Arr::has($data, 'images') ? self::attributes(Arr::get($data, 'images')) : null,
131+
documents: Arr::has($data, 'documents') ? self::attributes(Arr::get($data, 'documents')) : null,
132+
agency: Arr::has($data, 'agency') ? Agency::fromJson(Arr::get($data, 'agency')) : null,
133+
reserved: Arr::get($data, 'reserved'),
134+
rent_title: Arr::get($data, 'rent_title'),
135+
livingspace: Arr::get($data, 'livingspace'),
136+
moving_date: Arr::get($data, 'moving_date') ? Carbon::parse(Arr::get($data, 'moving_date')) : null,
113137
published: Arr::get($data, 'published') ? Carbon::parse(Arr::get($data, 'published')) : null,
114138
created: Arr::get($data, 'created') ? Carbon::parse(Arr::get($data, 'created')) : null,
115139
);
116140
}
141+
142+
protected static function attributes(array $attributes): Collection
143+
{
144+
return collect($attributes)->map(function ($result) {
145+
return Attribute::fromJson($result);
146+
});
147+
148+
}
149+
150+
protected static function images(array $images): Collection
151+
{
152+
return collect($images)->map(function ($result) {
153+
return Image::fromJson($result);
154+
});
155+
156+
}
157+
158+
protected static function documents(array $documents): Collection
159+
{
160+
return collect($documents)->map(function ($result) {
161+
return Document::fromJson($result);
162+
});
163+
164+
}
117165
}

src/Response/PublicListing.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace CodebarAg\Flatfox\Response;
44

5+
use CodebarAg\Flatfox\DTO\Listing;
56
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
68
use Saloon\Contracts\Response;
79

810
class PublicListing
@@ -11,7 +13,7 @@ public function __construct(
1113
public int $count,
1214
public string|null $next,
1315
public string|null $previous,
14-
public array|null $results,
16+
public Collection|null $results,
1517
) {
1618
}
1719

@@ -23,7 +25,14 @@ public static function fromResponse(Response $response): self
2325
Arr::get($data, 'count'),
2426
Arr::get($data, 'next'),
2527
Arr::get($data, 'previous'),
26-
Arr::get($data, 'results'),
28+
Arr::has($data, 'results') ? self::prepareResults(Arr::get($data, 'results')) : null,
2729
);
2830
}
31+
32+
protected static function prepareResults(array $results): Collection
33+
{
34+
return collect($results)->map(function ($result) {
35+
return Listing::fromJson($result);
36+
});
37+
}
2938
}

0 commit comments

Comments
 (0)