Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to hydrate non-initialized readonly properties #85

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Yii Hydrator Change Log

## 1.2.1 under development
## 1.3.0 under development

- no changes in this release.
- Enh #85: Allow to hydrate non-initialized readonly properties (@vjik)

## 1.2.0 April 03, 2024

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0.0",
"rector/rector": "1.0.*",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.23",
Expand Down
4 changes: 2 additions & 2 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function hydrate(object $object, array|DataInterface $data = []): void

$this->hydrateInternal(
$object,
ReflectionFilter::filterProperties($reflectionClass),
ReflectionFilter::filterProperties($object, $reflectionClass),
$data
);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public function create(string $class, array|DataInterface $data = []): object

$this->hydrateInternal(
$object,
ReflectionFilter::filterProperties($reflectionClass, $excludeProperties),
ReflectionFilter::filterProperties($object, $reflectionClass, $excludeProperties),
$data
);

Expand Down
3 changes: 2 additions & 1 deletion src/Internal/ReflectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class ReflectionFilter
* @return array<string, ReflectionProperty>
*/
public static function filterProperties(
object $object,
ReflectionClass $reflectionClass,
array $propertyNamesToFilter = []
): array {
Expand All @@ -30,7 +31,7 @@ public static function filterProperties(
continue;
}

if ($property->isReadOnly()) {
if ($property->isReadOnly() && $property->isInitialized($object)) {
continue;
}
$propertyName = $property->getName();
Expand Down
22 changes: 22 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Yiisoft\Hydrator\Tests\Support\Classes\FromPredefinedArrayClass;
use Yiisoft\Hydrator\Tests\Support\Classes\InvalidDataResolverClass;
use Yiisoft\Hydrator\Tests\Support\Classes\NestedModel\UserModel;
use Yiisoft\Hydrator\Tests\Support\Classes\NonInitializedReadonlyProperties;
use Yiisoft\Hydrator\Tests\Support\Classes\ObjectPropertyModel\ObjectPropertyModel;
use Yiisoft\Hydrator\Tests\Support\Classes\ObjectPropertyModel\RedCar;
use Yiisoft\Hydrator\Tests\Support\Classes\PreparePropertyClass;
Expand Down Expand Up @@ -747,4 +748,25 @@ public function testReadonlyObject(): void
$this->assertSame(3, $object->c);
$this->assertSame(4, $object->d);
}

public function testCreateNonInitializedReadonlyProperties(): void
{
$hydrator = new Hydrator();

$object = $hydrator->create(NonInitializedReadonlyProperties::class, ['a' => 1, 'b' => 2]);

$this->assertSame('1', $object->a);
$this->assertSame('2', $object->b);
}

public function testHydrateNonInitializedReadonlyProperties(): void
{
$hydrator = new Hydrator();

$object = new NonInitializedReadonlyProperties('3');
$hydrator->hydrate($object, ['a' => 1, 'b' => 2]);

$this->assertSame('1', $object->a);
$this->assertSame('3', $object->b);
}
}
15 changes: 15 additions & 0 deletions tests/Support/Classes/NonInitializedReadonlyProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\Support\Classes;

final class NonInitializedReadonlyProperties
{
public readonly string $a;

public function __construct(
public readonly string $b,
) {
}
}
Loading