From 7aa25ac63df2bcabe80004ccb8337eea2b4197a5 Mon Sep 17 00:00:00 2001 From: Oleksandr Prypkhan Date: Fri, 20 Jun 2025 15:57:57 +0300 Subject: [PATCH 1/2] fix: Fill migration DTO default values in from() --- src/IsMigrationDataTransferObject.php | 28 ++++++++++- tests/MigrationTest.php | 69 +++++++++++++++++---------- tests/Stubs/MigrationStubDTO.php | 1 + 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/IsMigrationDataTransferObject.php b/src/IsMigrationDataTransferObject.php index 33d10a4..f92920b 100644 --- a/src/IsMigrationDataTransferObject.php +++ b/src/IsMigrationDataTransferObject.php @@ -56,7 +56,7 @@ public static function from($data, array $map = []) } } - return static::create()->fill($data); + return static::create()->fillOrDefaults($data); } /** @@ -79,6 +79,32 @@ public function fill(array $data) return $this; } + /** + * @return static + * + * @deprecated + * + * Fill with given data or set defaults + */ + public function fillOrDefaults(array $data) + { + foreach ((new ReflectionClass(static::class))->getProperties() as $property) { + if (!$property->hasDefaultValue()) { + continue; + } + + $keyName = $this->propertyToKey($property->name); + + if (isset($data[$keyName])) { + continue; + } + + $property->setValue($property->getDefaultValue()); + } + + return $this->fill($data); + } + /** * @deprecated */ diff --git a/tests/MigrationTest.php b/tests/MigrationTest.php index 3f67c89..a61e1d1 100644 --- a/tests/MigrationTest.php +++ b/tests/MigrationTest.php @@ -14,21 +14,24 @@ public function testRegularConstruction(): void $dto = new MigrationStubDTO('test', $stub = StubDTO::create()->setName('nested'), TestEnum::$ONE); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => null, ], $dto->all()); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => null, ], $dto->jsonSerialize()); self::assertSame([ 'name' => 'test', 'dto' => [ 'name' => 'nested', ], - 'enum' => TestEnum::$ONE, + 'enum' => TestEnum::$ONE, + 'with_default' => null, ], $dto->toArray()); self::assertSame('test', $dto->name); self::assertSame('test', $dto->getName()); @@ -59,27 +62,33 @@ public function testLegacyConstruction(): void self::assertFalse($dto->hasDto()); self::assertNull($dto->getEnum()); self::assertFalse($dto->hasEnum()); + self::assertNull($dto->getWithDefault()); + self::assertFalse($dto->hasWithDefault()); $dto->setName('test') ->setDto($stub = StubDTO::create()->setName('nested')) - ->setEnum(TestEnum::$ONE); + ->setEnum(TestEnum::$ONE) + ->setWithDefault('string'); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->all()); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->jsonSerialize()); self::assertSame([ 'name' => 'test', 'dto' => [ 'name' => 'nested', ], - 'enum' => TestEnum::$ONE, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->toArray()); self::assertSame('test', $dto->name); self::assertSame('test', $dto->getName()); @@ -90,6 +99,9 @@ public function testLegacyConstruction(): void self::assertTrue($dto->hasDto()); self::assertSame(TestEnum::$ONE, $dto->getEnum()); self::assertTrue($dto->hasEnum()); + self::assertSame('string', $dto->withDefault); + self::assertSame('string', $dto->getWithDefault()); + self::assertTrue($dto->hasWithDefault()); $this->expectExceptionMessage('Cannot modify readonly property Tests\\Stubs\\MigrationStubDTO::$enum'); @@ -99,27 +111,31 @@ public function testLegacyConstruction(): void public function testLegacyConstructionFromArray(): void { $dto = MigrationStubDTO::from([ - 'name' => 'test', - 'dto' => $stub = StubDTO::create()->setName('nested'), - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub = StubDTO::create()->setName('nested'), + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ]); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->all()); self::assertSame([ - 'name' => 'test', - 'dto' => $stub, - 'enum' => TestEnum::$ONE, + 'name' => 'test', + 'dto' => $stub, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->jsonSerialize()); self::assertSame([ 'name' => 'test', 'dto' => [ 'name' => 'nested', ], - 'enum' => TestEnum::$ONE, + 'enum' => TestEnum::$ONE, + 'with_default' => 'string', ], $dto->toArray()); self::assertSame('test', $dto->name); self::assertSame('test', $dto->getName()); @@ -130,6 +146,9 @@ public function testLegacyConstructionFromArray(): void self::assertTrue($dto->hasDto()); self::assertSame(TestEnum::$ONE, $dto->getEnum()); self::assertTrue($dto->hasEnum()); + self::assertSame('string', $dto->withDefault); + self::assertSame('string', $dto->getWithDefault()); + self::assertTrue($dto->hasWithDefault()); $this->expectExceptionMessage('Cannot modify readonly property Tests\\Stubs\\MigrationStubDTO::$enum'); diff --git a/tests/Stubs/MigrationStubDTO.php b/tests/Stubs/MigrationStubDTO.php index 546294f..03b1cfc 100644 --- a/tests/Stubs/MigrationStubDTO.php +++ b/tests/Stubs/MigrationStubDTO.php @@ -10,5 +10,6 @@ public function __construct( public string $name, public readonly StubDTO $dto, public readonly TestEnum $enum, + public string|null $withDefault = null, ) {} } From 62f9aab41759141e8fb02e4876ac48873413312e Mon Sep 17 00:00:00 2001 From: Oleksandr Prypkhan Date: Fri, 20 Jun 2025 16:01:30 +0300 Subject: [PATCH 2/2] refactor: Code style --- tests/Stubs/MigrationStubDTO.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Stubs/MigrationStubDTO.php b/tests/Stubs/MigrationStubDTO.php index 03b1cfc..8408062 100644 --- a/tests/Stubs/MigrationStubDTO.php +++ b/tests/Stubs/MigrationStubDTO.php @@ -10,6 +10,6 @@ public function __construct( public string $name, public readonly StubDTO $dto, public readonly TestEnum $enum, - public string|null $withDefault = null, + public ?string $withDefault = null, ) {} }