Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit e5a7fa4

Browse files
author
Andrey Helldar
authored
Merge pull request #4 from TheDragonCode/2.x
Added the ability to apply casts
2 parents e964aa4 + 30bda9e commit e5a7fa4

File tree

5 files changed

+168
-3
lines changed

5 files changed

+168
-3
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,57 @@ return $instance->qwerty;
110110
// null
111111
```
112112

113+
### Casts
114+
115+
```php
116+
namespace App\DTO;
117+
118+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
119+
use DragonCode\Support\Facades\Helpers\Str;
120+
121+
class YourInstance extends DataTransferObject
122+
{
123+
public $foo;
124+
125+
public $bar;
126+
127+
public $baz;
128+
129+
public $qwerty;
130+
131+
protected $map = [
132+
'data.foo' => 'foo',
133+
'data.bar' => 'bar',
134+
];
135+
136+
protected function castFoo($value)
137+
{
138+
return Str::upper($value);
139+
}
140+
}
141+
142+
$instance = YourInstance::make([
143+
'data' => [
144+
'foo' => 'Foo',
145+
'bar' => 'Bar'
146+
],
147+
'baz' => 'Baz'
148+
]);
149+
150+
151+
return $instance->foo;
152+
// FOO
153+
154+
return $instance->bar;
155+
// Bar
156+
157+
return $instance->baz;
158+
// Baz
159+
160+
return $instance->qwerty;
161+
// null
162+
```
163+
113164
[badge_downloads]: https://img.shields.io/packagist/dt/dragon-code/simple-data-transfer-object.svg?style=flat-square
114165

115166
[badge_license]: https://img.shields.io/packagist/l/dragon-code/simple-data-transfer-object.svg?style=flat-square

src/DataTransferObject.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DragonCode\Contracts\DataTransferObject\DataTransferObject as Contract;
66
use DragonCode\SimpleDataTransferObject\Contracts\Reflection;
77
use DragonCode\Support\Concerns\Makeable;
8+
use DragonCode\Support\Facades\Helpers\Ables\Stringable;
89
use DragonCode\Support\Facades\Helpers\Arr;
910
use DragonCode\Support\Facades\Helpers\Str;
1011
use ReflectionClass;
@@ -89,10 +90,21 @@ protected function getValueByKey(array $items, string $key, string $default)
8990
protected function setValue(string $key, $value): void
9091
{
9192
if ($this->isAllow($key)) {
92-
$this->{$key} = $value;
93+
$this->{$key} = $this->cast($value, $key);
9394
}
9495
}
9596

97+
protected function cast($value, string $key)
98+
{
99+
$method = $this->getMethodName($key, 'cast');
100+
101+
if (method_exists($this, $method)) {
102+
return call_user_func([$this, $method], $value);
103+
}
104+
105+
return $value;
106+
}
107+
96108
/**
97109
* @param string $key
98110
*
@@ -127,4 +139,12 @@ protected function isAllowKey(string $key): bool
127139
{
128140
return ! in_array(Str::lower($key), $this->disallow, true);
129141
}
142+
143+
protected function getMethodName(string $key, string $prefix): string
144+
{
145+
return (string) Stringable::of($key)
146+
->trim()
147+
->start($prefix . '_')
148+
->camel();
149+
}
130150
}

tests/Fixtures/Cast.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
8+
use DragonCode\Support\Facades\Helpers\Str;
9+
10+
class Cast extends DataTransferObject
11+
{
12+
public $foo;
13+
14+
public $bar;
15+
16+
public $baz;
17+
18+
protected $map = [
19+
'wa.sd' => 'foo',
20+
'qwe.rty' => 'bar',
21+
];
22+
23+
protected function castFoo($value)
24+
{
25+
return Str::upper($value);
26+
}
27+
28+
protected function castBar($value)
29+
{
30+
return Str::lower($value);
31+
}
32+
}

tests/Unit/CastTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use DragonCode\Support\Facades\Helpers\Str;
8+
use Tests\Fixtures\Cast;
9+
use Tests\TestCase;
10+
11+
class CastTest extends TestCase
12+
{
13+
public function testMake()
14+
{
15+
$object = Cast::make([
16+
'wa' => [
17+
'sd' => $this->foo,
18+
],
19+
20+
'qwe.rty' => $this->bar,
21+
'baz' => $this->baz,
22+
]);
23+
24+
$this->assertSame(Str::upper($this->foo), $object->foo);
25+
$this->assertSame(Str::lower($this->bar), $object->bar);
26+
$this->assertSame($this->baz, $object->baz);
27+
}
28+
29+
public function testConstruct()
30+
{
31+
$object = new Cast([
32+
'wa' => [
33+
'sd' => $this->foo,
34+
],
35+
36+
'qwe.rty' => $this->bar,
37+
'baz' => $this->baz,
38+
]);
39+
40+
$this->assertSame(Str::upper($this->foo), $object->foo);
41+
$this->assertSame(Str::lower($this->bar), $object->bar);
42+
$this->assertSame($this->baz, $object->baz);
43+
}
44+
45+
public function testToArray()
46+
{
47+
$object = new Cast([
48+
'wa' => [
49+
'sd' => $this->foo,
50+
],
51+
52+
'qwe.rty' => $this->bar,
53+
'baz' => $this->baz,
54+
]);
55+
56+
$this->assertIsArray($object->toArray());
57+
58+
$this->assertSame([
59+
'foo' => Str::upper($this->foo),
60+
'bar' => Str::lower($this->bar),
61+
'baz' => $this->baz,
62+
], $object->toArray());
63+
}
64+
}

tests/Unit/MapTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public function testConstruct()
3434
'baz' => $this->baz,
3535
]);
3636

37-
$this->assertSame($this->foo, $object->foo);
38-
3937
$this->assertSame($this->foo, $object->foo);
4038
$this->assertSame($this->bar, $object->bar);
4139
$this->assertSame($this->baz, $object->baz);

0 commit comments

Comments
 (0)