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

Set dynamic properties with predicate #13

Merged
merged 1 commit into from
Jun 12, 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
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"./vendor/bin/infection --show-mutations -v"
],
"ci": [
"@autoload",
"@parallel cs psalm tests",
"@parallel infection"
]
Expand Down
3 changes: 3 additions & 0 deletions docs/reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ try {
}
```

**Note:** When setting a predicate on a dynamic class, new properties will always be added.
The next time you call `properties_get` on the same object, the predicate knows about the new property and will take it into account whilst filtering.

#### property_get

Detects the value of a property for a given object.
Expand Down
14 changes: 10 additions & 4 deletions src/Reflect/properties_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException;
use VeeWee\Reflecta\Reflect\Type\ReflectedClass;
use VeeWee\Reflecta\Reflect\Type\ReflectedProperty;
use function Psl\Dict\diff_by_key;
use function Psl\Dict\filter;
use function Psl\Dict\intersect_by_key;
use function Psl\Dict\merge;
use function Psl\Iter\reduce_with_keys;

/**
Expand All @@ -15,17 +18,20 @@
* @param null|Closure(ReflectedProperty): bool $predicate
*
* @return T
*@throws UnreflectableException
* @throws UnreflectableException
*
* @template T of object
*
*/
function properties_set(object $object, array $values, Closure|null $predicate = null): object
{
$properties = ReflectedClass::fromObject($object)->properties($predicate);
$class = ReflectedClass::fromObject($object);

$allProperties = $class->properties();
$filteredProperties = $predicate ? filter($allProperties, $predicate) : $allProperties;
$newValues = $class->isDynamic() ? diff_by_key($values, $allProperties) : [];

return reduce_with_keys(
intersect_by_key($values, $properties),
merge($newValues, intersect_by_key($values, $filteredProperties)),
/**
* @param T $object
* @return T
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/Iso/ObjectDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public function test_it_can_use_alternate_lens(): void
private int $y = 200;
};

$iso = object_data(X::class, properties(property_visibility(Visibility::Public)));
$iso = object_data($x::class, properties(property_visibility(Visibility::Public)));

$expectedData = ['z' => 100];
$expectedInstance = new X();
$expectedInstance->z = 100;
$expectedInstance = clone $x;

$instance = $iso->from($expectedData);
$actualData = $iso->to($instance);
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/Reflect/PropertiesSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace VeeWee\Reflecta\UnitTests\Reflect;

use PHPUnit\Framework\TestCase;
use stdClass;
use VeeWee\Reflecta\Reflect\Type\ReflectedProperty;
use VeeWee\Reflecta\TestFixtures\Dynamic;
use VeeWee\Reflecta\TestFixtures\X;
Expand Down Expand Up @@ -34,4 +35,37 @@ public function test_it_can_set_with_predicate_filter(): void
static::assertSame($actual->x, '456');
static::assertSame($actual->y, '123');
}

public function test_it_can_hydrate_new_props_on_std_class(): void
{
$x = new stdClass();
$x->foo = 'foo';

$actual = properties_set($x, ['bar' => 'bar']);

static::assertNotSame($x, $actual);
static::assertInstanceOf(stdClass::class, $actual);
static::assertSame($actual->foo, 'foo');
static::assertSame($actual->bar, 'bar');
}

public function test_it_can_hydrate_new_props_on_std_class_with_predicate(): void
{
$x = new stdClass();
$x->foo = 'foo';

$actual = properties_set(
$x,
[
'foo' => 'baz',
'bar' => 'bar',
],
static fn (ReflectedProperty $property): bool => false
);

static::assertNotSame($x, $actual);
static::assertInstanceOf(stdClass::class, $actual);
static::assertSame($actual->foo, 'foo');
static::assertSame($actual->bar, 'bar');
}
}