Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit d6fdbd4

Browse files
author
Mateusz Gostanski
committed
New StraightKeyParser
1 parent 99dcb1e commit d6fdbd4

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `api-client` will be documented in this file
44

5+
## 3.1.2 - 2021-03-30
6+
7+
- New StraightKeyParser
8+
59
## 3.1.1 - 2021-03-09
610

711
- Bugs fixed

src/Data/StraightKeyParser.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,63 @@
22

33
namespace Grixu\ApiClient\Data;
44

5+
use ErrorException;
56
use Grixu\ApiClient\Contracts\ResponseParser;
67
use Illuminate\Support\Carbon;
78
use Illuminate\Support\Collection;
9+
use ReflectionClass;
10+
use ReflectionProperty;
811

912
class StraightKeyParser implements ResponseParser
1013
{
14+
private $fields;
15+
1116
public function __construct(private string $dtoClass)
12-
{}
17+
{
18+
$reflection = new ReflectionClass($this->dtoClass);
19+
$this->fields = collect($reflection->getProperties(ReflectionProperty::IS_PUBLIC));
20+
}
1321

14-
public function parse(Collection $inputData): Collection
22+
public function parse(Collection $inputCollection): Collection
1523
{
1624
$parsed = collect();
1725

18-
foreach ($inputData as $data) {
19-
if (!is_array($data)) {
26+
foreach ($inputCollection as $inputData) {
27+
if (!is_array($inputData)) {
2028
continue;
2129
}
2230

23-
foreach ($data as $key => $value) {
24-
try {
25-
if (is_string($value) && strlen($value) >= 27 && Carbon::createFromTimeString($value) == true) {
26-
$data[$key] = Carbon::createFromTimeString($value);
27-
}
28-
} catch (\Exception $exception) {
29-
continue;
30-
}
31-
}
32-
33-
$dto = new $this->dtoClass($data);
31+
$preparedData = $this->parseElement($inputData);
32+
$dto = new $this->dtoClass($preparedData);
3433

3534
$parsed->push($dto);
3635
}
3736

3837
return $parsed;
3938
}
39+
40+
protected function parseElement(array $input): array
41+
{
42+
$data = [];
43+
44+
foreach ($this->fields as $field) {
45+
/** @var ReflectionProperty $field */
46+
$type = $field->getType()->getName();
47+
$fieldName = $field->getName();
48+
49+
try {
50+
if (str_contains($type, 'Enum')) {
51+
$data[$fieldName] = new $type($input[$fieldName]);
52+
} elseif ($type === 'Illuminate\Support\Carbon') {
53+
$data[$fieldName] = Carbon::createFromTimeString($input[$fieldName]);
54+
} else {
55+
$data[$fieldName] = $input[$fieldName];
56+
}
57+
} catch (ErrorException) {
58+
$data[$fieldName] = null;
59+
}
60+
}
61+
62+
return $data;
63+
}
4064
}

tests/Data/StraightKeyParserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,15 @@ public function it_do_not_replace_some_numeric_strings_to_carbon()
118118
}
119119

120120
/** @test */
121-
public function it_safely_handles_strings_longer_than_27_charaters()
121+
public function it_replaces_enums()
122122
{
123123
$inputData = collect(
124124
[
125125
[
126126
'first' => 'first entry is very long it could literally over 27 characters',
127127
'second' => 'second entry',
128128
'third' => '568845115895',
129+
'enum' => 'hello'
129130
],
130131
]
131132
);

tests/Helpers/ExampleDto.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ class ExampleDto extends DataTransferObject
1111
public string $second;
1212
public string $third;
1313
public ?Carbon $date;
14+
public ?FakeEnum $enum;
1415
public ?int $id;
1516
}

tests/Helpers/FakeEnum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Grixu\ApiClient\Tests\Helpers;
4+
5+
class FakeEnum
6+
{
7+
public function __construct(public string $value)
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)