Created
June 29, 2017 12:01
-
-
Save matej21/28f29be8ae4e50ecebdbbbfc947a64e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @property-read UserRole $role {container \App\Core\Orm\EnumProperty} | |
*/ |
This file contains hidden or 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
<?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