-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RawValuePropertyAccessor to see how it will look in 8.4, pre supp…
…ort for lazy objects.
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping\PropertyAccessors; | ||
|
||
use Doctrine\ORM\Proxy\InternalProxy; | ||
use ReflectionProperty; | ||
|
||
use function ltrim; | ||
|
||
/** | ||
* This is a PHP 8.4 and up only class and replaces ObjectCastPropertyAccessor. | ||
* | ||
* It works based on the raw values of a property, which for a case of property hooks | ||
* is the backed value. If we kept using setValue/getValue, this would go through the hooks, | ||
* which potentially change the data. | ||
*/ | ||
class RawValuePropertyAccessor implements PropertyAccessor | ||
{ | ||
public static function fromReflectionProperty(ReflectionProperty $reflectionProperty): self | ||
{ | ||
$name = $reflectionProperty->getName(); | ||
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($reflectionProperty->getDeclaringClass()->getName(), '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name); | ||
|
||
return new self($reflectionProperty, $key); | ||
} | ||
|
||
private function __construct(private ReflectionProperty $reflectionProperty, private string $key) | ||
{ | ||
} | ||
|
||
public function setValue(object $object, mixed $value): void | ||
{ | ||
if (! ($object instanceof InternalProxy && ! $object->__isInitialized())) { | ||
$this->reflectionProperty->setRawValue($object, $value); | ||
Check failure on line 36 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php GitHub Actions / Static Analysis with Psalm (3.8.2)UndefinedMethod
Check failure on line 36 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php GitHub Actions / Static Analysis with Psalm (default)UndefinedMethod
|
||
|
||
return; | ||
} | ||
|
||
$object->__setInitialized(true); | ||
|
||
$this->reflectionProperty->setRawValue($object, $value); | ||
Check failure on line 43 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php GitHub Actions / Static Analysis with Psalm (3.8.2)UndefinedMethod
Check failure on line 43 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php GitHub Actions / Static Analysis with Psalm (default)UndefinedMethod
|
||
|
||
$object->__setInitialized(false); | ||
} | ||
|
||
public function getValue(object $object): mixed | ||
{ | ||
return ((array) $object)[$this->key] ?? null; | ||
} | ||
} |