Skip to content

Commit 091e7c9

Browse files
Merge pull request #118 from WendellAdriel/feat/skip-on-transform
Add new SkipOnTransform attribute
2 parents 5d4715b + ac94ab9 commit 091e7c9

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/Attributes/SkipOnTransform.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WendellAdriel\ValidatedDTO\Attributes;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_PROPERTY)]
10+
final class SkipOnTransform {}

src/SimpleDTO.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use WendellAdriel\ValidatedDTO\Attributes\Provide;
2626
use WendellAdriel\ValidatedDTO\Attributes\Receive;
2727
use WendellAdriel\ValidatedDTO\Attributes\Rules;
28+
use WendellAdriel\ValidatedDTO\Attributes\SkipOnTransform;
2829
use WendellAdriel\ValidatedDTO\Casting\ArrayCast;
2930
use WendellAdriel\ValidatedDTO\Casting\Castable;
3031
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
@@ -71,6 +72,9 @@ abstract class SimpleDTO implements BaseDTO, CastsAttributes, JsonSerializable
7172
/** @internal */
7273
protected array $dtoMapTransform = [];
7374

75+
/** @internal */
76+
protected array $dtoSkipOnTransform = [];
77+
7478
/** @internal */
7579
private static array $classReflections = [];
7680

@@ -345,6 +349,10 @@ protected function buildDataForExport(): array
345349
if (! array_key_exists($property, $data) && isset($this->{$property})) {
346350
$data[$property] = $this->{$property};
347351
}
352+
353+
if (in_array($property, $this->dtoSkipOnTransform)) {
354+
unset($data[$property]);
355+
}
348356
}
349357

350358
return $this->mapDTOData($mapping, $data);
@@ -388,6 +396,10 @@ private function buildAttributesData(): void
388396
$this->dtoCasts[$property] = $attributeInstance;
389397
}
390398

399+
$this->dtoSkipOnTransform = array_keys(
400+
$this->getPropertiesForAttribute($publicProperties, SkipOnTransform::class)
401+
);
402+
391403
$classReflection = $this->classReflection($this::class);
392404
$classAttributes = collect($classReflection->getAttributes());
393405
$lazyAttribute = $classAttributes->first(
@@ -612,6 +624,7 @@ private function isforbiddenProperty(string $property): bool
612624
'dtoCasts',
613625
'dtoMapData',
614626
'dtoMapTransform',
627+
'dtoSkipOnTransform',
615628
'lazyValidation',
616629
]);
617630
}

tests/Datasets/SkipDTO.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WendellAdriel\ValidatedDTO\Tests\Datasets;
6+
7+
use WendellAdriel\ValidatedDTO\Attributes\Cast;
8+
use WendellAdriel\ValidatedDTO\Attributes\Rules;
9+
use WendellAdriel\ValidatedDTO\Attributes\SkipOnTransform;
10+
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
11+
use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts;
12+
use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults;
13+
use WendellAdriel\ValidatedDTO\Concerns\EmptyRules;
14+
use WendellAdriel\ValidatedDTO\ValidatedDTO;
15+
16+
final class SkipDTO extends ValidatedDTO
17+
{
18+
use EmptyCasts,
19+
EmptyDefaults,
20+
EmptyRules;
21+
22+
#[Rules(['required', 'string'])]
23+
public string $name;
24+
25+
#[Cast(IntegerCast::class)]
26+
#[SkipOnTransform]
27+
#[Rules(['required', 'integer'])]
28+
public int $age;
29+
}

tests/Unit/SkipOnTransformTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use WendellAdriel\ValidatedDTO\Tests\Datasets\SkipDTO;
6+
use WendellAdriel\ValidatedDTO\ValidatedDTO;
7+
8+
beforeEach(function () {
9+
$this->name = fake()->name;
10+
$this->age = fake()->numberBetween(1, 20);
11+
});
12+
13+
it('skip attribute when transforming to array', function () {
14+
$dto = new SkipDTO([
15+
'name' => $this->name,
16+
'age' => $this->age,
17+
]);
18+
19+
expect($dto)->toBeInstanceOf(ValidatedDTO::class)
20+
->and($dto->name)->toBe($this->name)
21+
->and($dto->toArray())->toBe(['name' => $this->name]);
22+
});
23+
24+
it('skip attribute when transforming to json', function () {
25+
$dto = new SkipDTO([
26+
'name' => $this->name,
27+
'age' => $this->age,
28+
]);
29+
30+
expect($dto)->toBeInstanceOf(ValidatedDTO::class)
31+
->and($dto->name)->toBe($this->name)
32+
->and($dto->toJson())->toBe('{"name":"' . $this->name . '"}');
33+
});

0 commit comments

Comments
 (0)