welcome back to dyb-tech
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
use BackedEnum;
|
||||
use ReflectionProperty;
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
use function array_map;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* PHP Enum Reflection Property - special override for backed enums.
|
||||
*/
|
||||
class EnumReflectionProperty extends ReflectionProperty
|
||||
{
|
||||
/** @var ReflectionProperty */
|
||||
private $originalReflectionProperty;
|
||||
|
||||
/** @var class-string<BackedEnum> */
|
||||
private $enumType;
|
||||
|
||||
/** @param class-string<BackedEnum> $enumType */
|
||||
public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType)
|
||||
{
|
||||
$this->originalReflectionProperty = $originalReflectionProperty;
|
||||
$this->enumType = $enumType;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Converts enum instance to its value.
|
||||
*
|
||||
* @param object|null $object
|
||||
*
|
||||
* @return int|string|int[]|string[]|null
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function getValue($object = null)
|
||||
{
|
||||
if ($object === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$enum = $this->originalReflectionProperty->getValue($object);
|
||||
|
||||
if ($enum === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fromEnum($enum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts enum value to enum instance.
|
||||
*
|
||||
* @param object $object
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setValue($object, $value = null): void
|
||||
{
|
||||
if ($value !== null) {
|
||||
$value = $this->toEnum($value);
|
||||
}
|
||||
|
||||
$this->originalReflectionProperty->setValue($object, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BackedEnum|BackedEnum[] $enum
|
||||
*
|
||||
* @return ($enum is BackedEnum ? (string|int) : (string[]|int[]))
|
||||
*/
|
||||
private function fromEnum($enum)
|
||||
{
|
||||
if (is_array($enum)) {
|
||||
return array_map(static function (BackedEnum $enum) {
|
||||
return $enum->value;
|
||||
}, $enum);
|
||||
}
|
||||
|
||||
return $enum->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string|int[]|string[] $value
|
||||
*
|
||||
* @return ($value is int|string ? BackedEnum : BackedEnum[])
|
||||
*/
|
||||
private function toEnum($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return array_map([$this->enumType, 'from'], $value);
|
||||
}
|
||||
|
||||
return $this->enumType::from($value);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
use Doctrine\Common\Proxy\Proxy;
|
||||
use ReflectionProperty;
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
/**
|
||||
* PHP Runtime Reflection Public Property - special overrides for public properties.
|
||||
*
|
||||
* @deprecated since version 3.1, use RuntimeReflectionProperty instead.
|
||||
*/
|
||||
class RuntimePublicReflectionProperty extends ReflectionProperty
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Returns the value of a public property without calling
|
||||
* `__get` on the provided $object if it exists.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function getValue($object = null)
|
||||
{
|
||||
return $object !== null ? ((array) $object)[$this->getName()] ?? null : parent::getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Avoids triggering lazy loading via `__set` if the provided object
|
||||
* is a {@see \Doctrine\Common\Proxy\Proxy}.
|
||||
*
|
||||
* @link https://bugs.php.net/bug.php?id=63463
|
||||
*
|
||||
* @param object|null $object
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function setValue($object, $value = null)
|
||||
{
|
||||
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
|
||||
parent::setValue($object, $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$originalInitializer = $object->__getInitializer();
|
||||
$object->__setInitializer(null);
|
||||
|
||||
parent::setValue($object, $value);
|
||||
|
||||
$object->__setInitializer($originalInitializer);
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
use Doctrine\Common\Proxy\Proxy as CommonProxy;
|
||||
use Doctrine\Persistence\Proxy;
|
||||
use ReflectionProperty;
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
use function ltrim;
|
||||
use function method_exists;
|
||||
|
||||
/**
|
||||
* PHP Runtime Reflection Property.
|
||||
*
|
||||
* Avoids triggering lazy loading if the provided object
|
||||
* is a {@see \Doctrine\Persistence\Proxy}.
|
||||
*/
|
||||
class RuntimeReflectionProperty extends ReflectionProperty
|
||||
{
|
||||
/** @var string */
|
||||
private $key;
|
||||
|
||||
public function __construct(string $class, string $name)
|
||||
{
|
||||
parent::__construct($class, $name);
|
||||
|
||||
$this->key = $this->isPrivate() ? "\0" . ltrim($class, '\\') . "\0" . $name : ($this->isProtected() ? "\0*\0" . $name : $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function getValue($object = null)
|
||||
{
|
||||
if ($object === null) {
|
||||
return parent::getValue($object);
|
||||
}
|
||||
|
||||
return ((array) $object)[$this->key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param object|null $object
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function setValue($object, $value = null)
|
||||
{
|
||||
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
|
||||
parent::setValue($object, $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($object instanceof CommonProxy) {
|
||||
$originalInitializer = $object->__getInitializer();
|
||||
$object->__setInitializer(null);
|
||||
|
||||
parent::setValue($object, $value);
|
||||
|
||||
$object->__setInitializer($originalInitializer);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! method_exists($object, '__setInitialized')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$object->__setInitialized(true);
|
||||
|
||||
parent::setValue($object, $value);
|
||||
|
||||
$object->__setInitialized(false);
|
||||
}
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
/**
|
||||
* PHP Typed No Default Reflection Property - special override for typed properties without a default value.
|
||||
*/
|
||||
class TypedNoDefaultReflectionProperty extends RuntimeReflectionProperty
|
||||
{
|
||||
use TypedNoDefaultReflectionPropertyBase;
|
||||
}
|
||||
Vendored
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
use Closure;
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
use function assert;
|
||||
|
||||
/**
|
||||
* PHP Typed No Default Reflection Property Base - special override for typed properties without a default value.
|
||||
*
|
||||
* @internal since version 3.1
|
||||
*/
|
||||
trait TypedNoDefaultReflectionPropertyBase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Checks that a typed property is initialized before accessing its value.
|
||||
* This is necessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
|
||||
* Should be used only for reflecting typed properties without a default value.
|
||||
*
|
||||
* @param object|null $object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function getValue($object = null)
|
||||
{
|
||||
return $object !== null && $this->isInitialized($object) ? parent::getValue($object) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Works around the problem with setting typed no default properties to
|
||||
* NULL which is not supported, instead unset() to uninitialize.
|
||||
*
|
||||
* @link https://github.com/doctrine/orm/issues/7999
|
||||
*
|
||||
* @param object|null $object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function setValue($object, $value = null)
|
||||
{
|
||||
if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) {
|
||||
$propertyName = $this->getName();
|
||||
|
||||
$unsetter = function () use ($propertyName): void {
|
||||
unset($this->$propertyName);
|
||||
};
|
||||
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
|
||||
|
||||
assert($unsetter instanceof Closure);
|
||||
|
||||
$unsetter();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
parent::setValue($object, $value);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Persistence\Reflection;
|
||||
|
||||
/**
|
||||
* PHP Typed No Default Runtime Public Reflection Property - special override for public typed properties without a default value.
|
||||
*
|
||||
* @deprecated since version 3.1, use TypedNoDefaultReflectionProperty instead.
|
||||
*/
|
||||
class TypedNoDefaultRuntimePublicReflectionProperty extends RuntimePublicReflectionProperty
|
||||
{
|
||||
use TypedNoDefaultReflectionPropertyBase;
|
||||
}
|
||||
Reference in New Issue
Block a user