Skip to content

Commit

Permalink
Add adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Oct 12, 2023
1 parent dd921c0 commit e27501c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Support/DataClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Spatie\LaravelData\Contracts\TransformableData;
use Spatie\LaravelData\Contracts\ValidateableData;
use Spatie\LaravelData\Contracts\WrappableData;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\ProvidedNameMapper;
use Spatie\LaravelData\Resolvers\NameMappersResolver;
use Spatie\LaravelData\Support\Lazy\CachedLazy;
Expand Down Expand Up @@ -88,7 +87,8 @@ protected static function resolveAttributes(
->map(fn (ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance());

$parent = $class->getParentClass();
if ($parent !== false && $class->getName() !== Data::class) {

if ($parent !== false) {
$attributes = $attributes->merge(static::resolveAttributes($parent));
}

Expand Down
32 changes: 16 additions & 16 deletions tests/Support/DataClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,22 @@ public function __construct(
});

it('resolves parent attributes', function () {
$dataClass = DataClass::create(new ReflectionClass(ChildData::class));
#[MapName(SnakeCaseMapper::class)]
#[WithTransformer(DateTimeInterfaceTransformer::class, 'd-m-Y')]
#[WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d')]
class TestRecursiveAttributesParentData extends Data
{
}

class TestRecursiveAttributesChildData extends TestRecursiveAttributesParentData
{
public function __construct(
public DateTimeInterface $dateTime
) {
}
}

$dataClass = DataClass::create(new ReflectionClass(TestRecursiveAttributesChildData::class));

expect($dataClass->attributes)
->toHaveCount(3)
Expand Down Expand Up @@ -136,18 +151,3 @@ public static function fromDummyModel(DummyModel $model)
return new self($model->id);
}
}

#[MapName(SnakeCaseMapper::class)]
#[WithTransformer(DateTimeInterfaceTransformer::class, 'd-m-Y')]
#[WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d')]
class ParentData extends Data
{
}

class ChildData extends ParentData
{
public function __construct(
public DateTimeInterface $dateTime
) {
}
}

0 comments on commit e27501c

Please sign in to comment.