Skip to content

Instantly share code, notes, and snippets.

@matej21
Created June 29, 2017 12:01
Show Gist options
  • Save matej21/28f29be8ae4e50ecebdbbbfc947a64e4 to your computer and use it in GitHub Desktop.
Save matej21/28f29be8ae4e50ecebdbbbfc947a64e4 to your computer and use it in GitHub Desktop.
/**
* @property-read UserRole $role {container \App\Core\Orm\EnumProperty}
*/
<?php declare(strict_types = 1);
namespace App\Core\Orm;
use MabeEnum\Enum;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IPropertyContainer;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
class EnumProperty implements IPropertyContainer
{
/** @var Enum|NULL */
private $value;
/** @var IEntity */
private $entity;
/** @var PropertyMetadata */
private $propertyMetadata;
/** @var string */
private $enumClass;
public function __construct(IEntity $entity, PropertyMetadata $propertyMetadata)
{
$this->entity = $entity;
$this->propertyMetadata = $propertyMetadata;
$this->enumClass = key($this->propertyMetadata->types);
assert(class_exists($this->enumClass));
}
public function setRawValue($value)
{
$enumClass = $this->enumClass;
$this->value = $value ? $enumClass::byValue($value) : NULL;
}
public function getRawValue()
{
return $this->value ? $this->value->getValue() : NULL;
}
public function &getInjectedValue()
{
return $this->value;
}
public function hasInjectedValue(): bool
{
return $this->value !== NULL;
}
public function setInjectedValue($value)
{
if ($value !== $this->value) {
$this->entity->setAsModified($this->propertyMetadata->name);
}
$this->value = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment