Skip to content

Commit 4c87cf9

Browse files
committed
Upd: json support
1 parent 533522a commit 4c87cf9

11 files changed

+523
-21
lines changed

src/ArgumentsRepository.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ final class ArgumentsRepository
2525

2626
/**
2727
*
28-
* @param iterable<mixed>|object|string ...$args
28+
* @param iterable<mixed>|object ...$args
2929
*/
3030
public function __construct(...$args)
3131
{
3232
// Unpacking named arguments
3333
$input = sizeof(func_get_args()) === 1 ? $args[0] : $args;
34-
34+
3535
if (!is_array($input)) {
36-
if (is_string($input)) {
37-
$input = json_decode($input, true);
38-
} else {
39-
$input = (array)$input;
40-
}
36+
$input = (array)$input;
4137
}
4238

4339
$this->args = $input;

src/Hydrator.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct()
3131
* Create instance T class
3232
*
3333
* @param class-string<T> $class
34-
* @param iterable<mixed>|object|string ...$args
34+
* @param iterable<mixed>|object ...$args
3535
*
3636
* @return null|T
3737
* @throws ClassNotFoundException|InstantiableClassException|ReflectionException|InvalidArgumentException
@@ -78,4 +78,22 @@ public function createMultiple(array $classes, array $args): ?array
7878
}
7979
return $result;
8080
}
81+
82+
/**
83+
* Create instance T class
84+
*
85+
* @param class-string<T> $class
86+
* @param string $json
87+
*
88+
* @return null|T
89+
* @throws ClassNotFoundException
90+
* @throws InvalidArgumentException
91+
* @throws ReflectionException
92+
*/
93+
public function createFromJson(string $class, string $json): mixed
94+
{
95+
$data = json_decode($json, true);
96+
97+
return $this->create($class, $data);
98+
}
8199
}

src/InstanceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class InstanceFactory
2626

2727
/**
2828
* @param class-string<T> $class
29-
* @param iterable<mixed>|object|string ...$args
29+
* @param iterable<mixed>|object ...$args
3030
*
3131
* @return mixed
3232
* @throws ClassNotFoundException|InstantiableClassException|ReflectionException|InvalidArgumentException

tests/Integration/ClassTransformerFromArrayTest.php

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Tests\Integration\DTO\BasketDTO;
1212
use Tests\Integration\DTO\ProductDTO;
1313
use Tests\Integration\DTO\PurchaseDTO;
14+
use Tests\Integration\DTO\Full\PurchaseDTO as FullPurchaseDto;
1415
use Tests\Integration\DTO\EmptyClassDto;
1516
use Tests\Integration\DTO\ArrayScalarDTO;
1617
use Tests\Integration\DTO\UserEmptyTypeDTO;
@@ -204,10 +205,7 @@ public function testTripleRecursiveArray(): void
204205
}
205206
}
206207
}
207-
208-
/**
209-
* @throws ReflectionException|ClassNotFoundException
210-
*/
208+
211209
public function testEmptyTypeObject(): void
212210
{
213211
$data = $this->getBaseArrayData();
@@ -219,4 +217,159 @@ public function testEmptyTypeObject(): void
219217
self::assertEquals($data['email'], $userDTO->email);
220218
self::assertEquals($data['balance'], $userDTO->balance);
221219
}
220+
221+
public function testFull(): void
222+
{
223+
$data = $this->getPurcheseObject();
224+
$object = (new Hydrator)->create(FullPurchaseDto::class, $data);
225+
$this->assertEquals($data['user']['id'], $object->user->id);
226+
}
227+
228+
public function getPurcheseObject(): array
229+
{
230+
return [
231+
'products' => [
232+
[
233+
'id' => 1,
234+
'name' => 'phone',
235+
'price' => 43.03,
236+
'description' => 'test description for phone',
237+
'count' => 123
238+
],
239+
[
240+
'id' => 2,
241+
'name' => 'bread',
242+
'price' => 10.56,
243+
'description' => 'test description for bread',
244+
'count' => 321
245+
],
246+
[
247+
'id' => 3,
248+
'name' => 'book',
249+
'price' => 5.5,
250+
'description' => 'test description for book',
251+
'count' => 333
252+
],
253+
[
254+
'id' => 4,
255+
'name' => 'PC',
256+
'price' => 100,
257+
'description' => 'test description for PC',
258+
'count' => 7
259+
]
260+
],
261+
'user' => [
262+
'id' => 1,
263+
'contact' => 'fake@mail.com',
264+
'balance' => 10012.23,
265+
'type' => 'admin',
266+
'realAddress' => 'test address',
267+
'createdAt' => '2023-04-10',
268+
],
269+
'createdAt' => '2023-04-10',
270+
'address' => $this->getAddress()
271+
];
272+
}
273+
274+
private function getAddress()
275+
{
276+
return [
277+
"source" => "мск сухонска 11/-89",
278+
"result" => "г Москва, ул Сухонская, д 11, кв 89",
279+
"postal_code" => "127642",
280+
"country" => "Россия",
281+
"country_iso_code" => "RU",
282+
"federal_district" => "Центральный",
283+
"region_fias_id" => "0c5b2444-70a0-4932-980c-b4dc0d3f02b5",
284+
"region_kladr_id" => "7700000000000",
285+
"region_iso_code" => "RU-MOW",
286+
"region_with_type" => "г Москва",
287+
"region_type" => "г",
288+
"region_type_full" => "город",
289+
"region" => "Москва",
290+
"area_fias_id" => null,
291+
"area_kladr_id" => null,
292+
"area_with_type" => null,
293+
"area_type" => null,
294+
"area_type_full" => null,
295+
"area" => null,
296+
"city_fias_id" => null,
297+
"city_kladr_id" => null,
298+
"city_with_type" => null,
299+
"city_type" => null,
300+
"city_type_full" => null,
301+
"city" => null,
302+
"city_area" => "Северо-восточный",
303+
"city_district_fias_id" => null,
304+
"city_district_kladr_id" => null,
305+
"city_district_with_type" => "р-н Северное Медведково",
306+
"city_district_type" => "р-н",
307+
"city_district_type_full" => "район",
308+
"city_district" => "Северное Медведково",
309+
"settlement_fias_id" => null,
310+
"settlement_kladr_id" => null,
311+
"settlement_with_type" => null,
312+
"settlement_type" => null,
313+
"settlement_type_full" => null,
314+
"settlement" => null,
315+
"street_fias_id" => "95dbf7fb-0dd4-4a04-8100-4f6c847564b5",
316+
"street_kladr_id" => "77000000000283600",
317+
"street_with_type" => "ул Сухонская",
318+
"street_type" => "ул",
319+
"street_type_full" => "улица",
320+
"street" => "Сухонская",
321+
"house_fias_id" => "5ee84ac0-eb9a-4b42-b814-2f5f7c27c255",
322+
"house_kladr_id" => "7700000000028360004",
323+
"house_type" => "д",
324+
"house_type_full" => "дом",
325+
"house" => "11",
326+
"block_type" => null,
327+
"block_type_full" => null,
328+
"block" => null,
329+
"flat_fias_id" => "f26b876b-6857-4951-b060-ec6559f04a9a",
330+
"flat_type" => "кв",
331+
"flat_type_full" => "квартира",
332+
"flat" => "89",
333+
"flat_area" => "34.6",
334+
"square_meter_price" => "239953",
335+
"flat_price" => "8302374",
336+
"postal_box" => null,
337+
"fias_id" => "f26b876b-6857-4951-b060-ec6559f04a9a",
338+
"fias_code" => "77000000000000028360004",
339+
"fias_level" => "9",
340+
"kladr_id" => "7700000000028360004",
341+
"capital_marker" => "0",
342+
"okato" => "45280583000",
343+
"oktmo" => "45362000",
344+
"tax_office" => "7715",
345+
"tax_office_legal" => "7715",
346+
"timezone" => "UTC+3",
347+
"geo_lat" => "55.8782557",
348+
"geo_lon" => "37.65372",
349+
"beltway_hit" => "IN_MKAD",
350+
"beltway_distance" => null,
351+
"qc_geo" => 0,
352+
"qc_complete" => 0,
353+
"qc_house" => 2,
354+
"qc" => 0,
355+
"unparsed_parts" => null,
356+
"metro" => [
357+
[
358+
"distance" => 1.1,
359+
"line" => "Калужско-Рижская",
360+
"name" => "Бабушкинская"
361+
],
362+
[
363+
"distance" => 1.2,
364+
"line" => "Калужско-Рижская",
365+
"name" => "Медведково"
366+
],
367+
[
368+
"distance" => 2.5,
369+
"line" => "Калужско-Рижская",
370+
"name" => "Свиблово"
371+
]
372+
]
373+
];
374+
}
222375
}

tests/Integration/ClassTransformerFromJsonTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testBaseArray(): void
3535
{
3636
$data = $this->getBaseArrayData();
3737

38-
$userDTO = (new Hydrator())->create(UserDTO::class, json_encode($data));
38+
$userDTO = (new Hydrator())->createFromJson(UserDTO::class, json_encode($data));
3939

4040
self::assertInstanceOf(UserDTO::class, $userDTO);
4141
self::assertEquals($data['id'], $userDTO->id);
@@ -58,7 +58,7 @@ public function testConstructFormatArray(): void
5858
'address_test' => 'example address',
5959
'color' => 'White'
6060
];
61-
$userDTO = (new Hydrator())->create(ConstructDto::class, json_encode($data));
61+
$userDTO = (new Hydrator())->createFromJson(ConstructDto::class, json_encode($data));
6262
self::assertInstanceOf(ConstructDto::class, $userDTO);
6363
self::assertEquals($data['id'], $userDTO->id);
6464
self::assertEquals($data['email'], $userDTO->email);
@@ -76,7 +76,7 @@ public function testConstructFormatArray(): void
7676
public function testEmptyClass(): void
7777
{
7878
$data = $this->getBaseArrayData();
79-
$instance = (new Hydrator())->create(EmptyClassDto::class, json_encode($data));
79+
$instance = (new Hydrator())->createFromJson(EmptyClassDto::class, json_encode($data));
8080
self::assertInstanceOf(EmptyClassDto::class, $instance);
8181
}
8282

@@ -89,7 +89,7 @@ public function testScalarArray(): void
8989
'stringList' => [100, 200, 300],
9090
'intList' => [400, 500, 600]
9191
];
92-
$dto = (new Hydrator())->create(ArrayScalarDTO::class, json_encode($data));
92+
$dto = (new Hydrator())->createFromJson(ArrayScalarDTO::class, json_encode($data));
9393
self::assertInstanceOf(ArrayScalarDTO::class, $dto);
9494
self::assertIsString($dto->stringList[0]);
9595
self::assertEquals($dto->stringList[0], '100');
@@ -107,7 +107,7 @@ public function testNullArray(): void
107107
'products' => null
108108
];
109109

110-
$userDTO = (new Hydrator())->create(ArrayScalarDTO::class, json_encode($data));
110+
$userDTO = (new Hydrator())->createFromJson(ArrayScalarDTO::class, json_encode($data));
111111

112112
self::assertInstanceOf(ArrayScalarDTO::class, $userDTO);
113113
}
@@ -164,7 +164,7 @@ public function testTransformMultiple(): void
164164
public function testRecursiveArray(): void
165165
{
166166
$data = $this->getRecursiveArrayData();
167-
$purchaseDTO = (new Hydrator())->create(PurchaseDTO::class, json_encode($data));
167+
$purchaseDTO = (new Hydrator())->createFromJson(PurchaseDTO::class, json_encode($data));
168168

169169
self::assertInstanceOf(PurchaseDTO::class, $purchaseDTO);
170170
self::assertInstanceOf(UserDTO::class, $purchaseDTO->user);
@@ -193,7 +193,7 @@ public function testTripleRecursiveArray(): void
193193
{
194194
$data = $this->getTripleRecursiveArray();
195195

196-
$basketDTO = (new Hydrator())->create(BasketDTO::class, json_encode($data));
196+
$basketDTO = (new Hydrator())->createFromJson(BasketDTO::class, json_encode($data));
197197

198198
foreach ($basketDTO->orders as $key => $purchase) {
199199
self::assertInstanceOf(PurchaseDTO::class, $purchase);
@@ -223,7 +223,7 @@ public function testEmptyTypeObject(): void
223223
{
224224
$data = $this->getBaseArrayData();
225225

226-
$userDTO = (new Hydrator())->create(UserEmptyTypeDTO::class, json_encode($data));
226+
$userDTO = (new Hydrator())->createFromJson(UserEmptyTypeDTO::class, json_encode($data));
227227

228228
self::assertInstanceOf(UserEmptyTypeDTO::class, $userDTO);
229229
self::assertEquals($data['id'], $userDTO->id);

0 commit comments

Comments
 (0)