123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
-
- namespace Gedmo\Tool\Wrapper;
-
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Proxy\Proxy;
-
-
- class EntityWrapper extends AbstractWrapper
- {
-
-
- private $identifier;
-
-
-
- private $initialized = false;
-
-
-
- public function __construct($entity, EntityManager $em)
- {
- $this->om = $em;
- $this->object = $entity;
- $this->meta = $em->getClassMetadata(get_class($this->object));
- }
-
-
-
- public function getPropertyValue($property)
- {
- $this->initialize();
- return $this->meta->getReflectionProperty($property)->getValue($this->object);
- }
-
-
-
- public function setPropertyValue($property, $value)
- {
- $this->initialize();
- $this->meta->getReflectionProperty($property)->setValue($this->object, $value);
- return $this;
- }
-
-
-
- public function hasValidIdentifier()
- {
- return (bool)$this->getIdentifier();
- }
-
-
-
- public function getRootObjectName()
- {
- return $this->meta->rootEntityName;
- }
-
-
-
- public function getIdentifier($single = true)
- {
- if (!$this->identifier) {
- if ($this->object instanceof Proxy) {
- $uow = $this->om->getUnitOfWork();
- if ($uow->isInIdentityMap($this->object)) {
- $this->identifier = $uow->getEntityIdentifier($this->object);
- } else {
- $this->initialize();
- }
- }
- if (!$this->identifier) {
- $this->identifier = array();
- $incomplete = false;
- foreach ($this->meta->identifier as $name) {
- $this->identifier[$name] = $this->getPropertyValue($name);
- if (!$this->identifier[$name]) {
- $incomplete = true;
- }
- }
- if ($incomplete) {
- $this->identifier = null;
- }
- }
- }
- if ($single && is_array($this->identifier)) {
- return reset($this->identifier);
- }
- return $this->identifier;
- }
-
-
-
- protected function initialize()
- {
- if (!$this->initialized) {
- if ($this->object instanceof Proxy) {
- $uow = $this->om->getUnitOfWork();
- if (!$this->object->__isInitialized__) {
- $this->object->__load();
- }
- }
- }
- }
- }
|