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

Changed converting enum from and to raw values + added support for Ba… #638

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ protected function initProperty(PropertyMetadata $metadata, string $name, bool $
{
$this->validated[$name] = true;

if (!isset($this->data[$name]) && !array_key_exists($name, $this->data)) {
$this->data[$name] = $this->persistedId === null ? $metadata->defaultValue : null;
}

if ($metadata->wrapper !== null) {
$wrapper = $this->createPropertyWrapper($metadata);
if ($initValue || isset($this->data[$metadata->name])) {
Expand All @@ -499,9 +503,6 @@ protected function initProperty(PropertyMetadata $metadata, string $name, bool $
return;
}

if (!isset($this->data[$name]) && !array_key_exists($name, $this->data)) {
$this->data[$name] = $this->persistedId === null ? $metadata->defaultValue : null;
}

if ($this->data[$name] !== null) {
// data type coercion
Expand Down
20 changes: 15 additions & 5 deletions src/Entity/PropertyWrapper/BackedEnumWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function convertToRawValue(mixed $value): mixed
if ($value === null) return null;
$type = array_key_first($this->propertyMetadata->types);
assert($value instanceof $type);
assert($value instanceof BackedEnum);
return $value->value;
if ($value instanceof BackedEnum) {
return $value->value;
}
hrach marked this conversation as resolved.
Show resolved Hide resolved

return $value;
}


Expand All @@ -42,9 +45,16 @@ public function convertFromRawValue(mixed $value): ?BackedEnum
throw new NullValueException($this->propertyMetadata);
}

assert(is_int($value) || is_string($value));
$type = array_key_first($this->propertyMetadata->types);
assert(is_subclass_of($type, BackedEnum::class));
return $type::from($value);
if (is_int($value) || is_string($value)) {
if (is_subclass_of($type, BackedEnum::class)) {
return $type::from($value);
}
}
if ($value instanceof BackedEnum && $value instanceof $type) {
hrach marked this conversation as resolved.
Show resolved Hide resolved
return $value;
}

return null;
hrach marked this conversation as resolved.
Show resolved Hide resolved
}
}
46 changes: 26 additions & 20 deletions src/Entity/Reflection/ModifierParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Nextras\Orm\Entity\Reflection;


use BackedEnum;
use Nette\Utils\Reflection;
use Nextras\Orm\Entity\Reflection\Parser\Token;
use Nextras\Orm\Entity\Reflection\Parser\TokenLexer;
use Nextras\Orm\Entity\Reflection\Parser\TokenStream;
use Nextras\Orm\Exception\InvalidStateException;
use ReflectionClass;
use ReflectionEnum;


class ModifierParser
Expand Down Expand Up @@ -190,29 +192,33 @@ private function processKeyword(string $value, ReflectionClass $reflectionClass)
$reflection = new ReflectionClass($className);
}

$enum = [];
$constants = $reflection->getConstants();
if (str_contains($const, '*')) {
$prefix = rtrim($const, '*');
$prefixLength = strlen($prefix);
$count = 0;
foreach ($constants as $name => $constantValue) {
if (substr($name, 0, $prefixLength) === $prefix) {
$enum[$constantValue] = $constantValue;
$count += 1;
}
}
if ($count === 0) {
throw new InvalidModifierDefinitionException("No constant matches $reflection->name::$const pattern.");
}
if ($reflection->isEnum() && is_subclass_of($className, BackedEnum::class)) {
return (new ReflectionEnum($className))->getCase($const)->getValue();
hrach marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!array_key_exists($const, $constants)) {
throw new InvalidModifierDefinitionException("Constant $reflection->name::$const does not exist.");
$enum = [];
$constants = $reflection->getConstants();
if (str_contains($const, '*')) {
$prefix = rtrim($const, '*');
$prefixLength = strlen($prefix);
$count = 0;
foreach ($constants as $name => $constantValue) {
if (substr($name, 0, $prefixLength) === $prefix) {
$enum[$constantValue] = $constantValue;
$count += 1;
}
}
if ($count === 0) {
throw new InvalidModifierDefinitionException("No constant matches $reflection->name::$const pattern.");
}
} else {
if (!array_key_exists($const, $constants)) {
throw new InvalidModifierDefinitionException("Constant $reflection->name::$const does not exist.");
}
$value = $reflection->getConstant($const);
$enum[$value] = $value;
}
$value = $reflection->getConstant($const);
$enum[$value] = $value;
return array_values($enum);
}
return array_values($enum);
} else {
return $value;
}
Expand Down