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

Commit 209e932

Browse files
authored
Dev/3.2.0 (#2)
* Added generating snake_case array field names based on DTOs field names in StraightKeyParser * Updated CHANGELOG.md * Rebuilt StraightKeyParser * Updated tests after RDTO update * DTO updated to v3
1 parent d6fdbd4 commit 209e932

File tree

7 files changed

+77
-21
lines changed

7 files changed

+77
-21
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
## 3.2.0 - 2021-04-29
6+
7+
- Use snake_case naming convention in results data array instead of camelCase
8+
- Updated DTO to v3
9+
510
## 3.1.2 - 2021-03-30
611

712
- New StraightKeyParser
@@ -26,7 +31,8 @@ All notable changes to `api-client` will be documented in this file
2631
- Huge rebuild package both in concept and code layer.
2732
- Uses PHP8, dropped PHP 7.4 compatibility.
2833
- More concentrated on delivery flexible package that could be use directly in apps rather than in other packages
29-
- Made interfaces & classes for configuration, authorization, data fetching, containing & parsing to final classes delivered by user
34+
- Made interfaces & classes for configuration, authorization, data fetching, containing & parsing to final classes
35+
delivered by user
3036
- Prepared classes for config, url creating & fetching data from JSON API
3137

3238
## 2.0.1 - 2021-01-20

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"spatie/laravel-enum": "^2.2"
2626
},
2727
"require-dev": {
28-
"spatie/data-transfer-object": "^2.8",
28+
"spatie/data-transfer-object": "^3.1",
2929
"orchestra/testbench": "^6.0",
3030
"phpunit/phpunit": "^9.4"
3131
},

src/Data/StraightKeyParser.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Grixu\ApiClient\Data;
44

5-
use ErrorException;
65
use Grixu\ApiClient\Contracts\ResponseParser;
7-
use Illuminate\Support\Carbon;
86
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Str;
98
use ReflectionClass;
109
use ReflectionProperty;
1110

@@ -42,23 +41,31 @@ protected function parseElement(array $input): array
4241
$data = [];
4342

4443
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;
44+
$dtoFieldName = $field->getName();
45+
$arrayFieldName = Str::snake($dtoFieldName);
46+
47+
if ($dtoFieldName === 'relationships') {
48+
$data[$dtoFieldName] = $this->parseRelationship($input[$arrayFieldName]);
49+
} else {
50+
$data[$dtoFieldName] = $input[$arrayFieldName] ?? null;
5951
}
6052
}
6153

6254
return $data;
6355
}
56+
57+
protected function parseRelationship(array $inputRelationships): array
58+
{
59+
$relationships = [];
60+
61+
foreach ($inputRelationships as $input) {
62+
$relationship = [];
63+
foreach ($input as $key => $value) {
64+
$relationship[Str::camel($key)] = $value;
65+
}
66+
$relationships[] = $relationship;
67+
}
68+
69+
return $relationships;
70+
}
6471
}

tests/Helpers/CarbonCaster.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Grixu\ApiClient\Tests\Helpers;
4+
5+
use Illuminate\Support\Carbon;
6+
use Spatie\DataTransferObject\Caster;
7+
8+
class CarbonCaster implements Caster
9+
{
10+
public function cast(mixed $value): Carbon
11+
{
12+
return Carbon::createFromTimeString($value);
13+
}
14+
}

tests/Helpers/ExampleDto.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
namespace Grixu\ApiClient\Tests\Helpers;
44

55
use Illuminate\Support\Carbon;
6+
use Spatie\DataTransferObject\Attributes\CastWith;
67
use Spatie\DataTransferObject\DataTransferObject;
78

89
class ExampleDto extends DataTransferObject
910
{
1011
public string $first;
1112
public string $second;
1213
public string $third;
13-
public ?Carbon $date;
14-
public ?FakeEnum $enum;
15-
public ?int $id;
14+
15+
#[CastWith(CarbonCaster::class)]
16+
public Carbon|null $date;
17+
18+
#[CastWith(FakeEnumCaster::class)]
19+
public FakeEnum|null $enum;
20+
21+
public int|null $id;
1622
}

tests/Helpers/FakeEnum.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
namespace Grixu\ApiClient\Tests\Helpers;
44

5+
use JetBrains\PhpStorm\Pure;
6+
57
class FakeEnum
68
{
79
public function __construct(public string $value)
810
{
911
}
12+
13+
#[Pure]
14+
public static function make(mixed $value): static
15+
{
16+
return new static($value);
17+
}
1018
}

tests/Helpers/FakeEnumCaster.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Grixu\ApiClient\Tests\Helpers;
4+
5+
use Spatie\DataTransferObject\Caster;
6+
7+
class FakeEnumCaster implements Caster
8+
{
9+
public function cast(mixed $value): FakeEnum
10+
{
11+
if ($value instanceof FakeEnum) return $value;
12+
13+
return FakeEnum::make($value);
14+
}
15+
}

0 commit comments

Comments
 (0)