EntityWrapper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\Proxy\Proxy;
  5. /**
  6. * Wraps entity or proxy for more convenient
  7. * manipulation
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Tool.Wrapper
  11. * @subpackage EntityWrapper
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class EntityWrapper
  16. {
  17. /**
  18. * Wrapped Entity
  19. *
  20. * @var object
  21. */
  22. protected $entity;
  23. /**
  24. * EntityManager instance
  25. *
  26. * @var \Doctrine\ORM\EntityManager
  27. */
  28. protected $em;
  29. /**
  30. * Entity metadata
  31. *
  32. * @var \Doctrine\ORM\Mapping\ClassMetadata
  33. */
  34. protected $meta;
  35. /**
  36. * Entity identifier
  37. *
  38. * @var array
  39. */
  40. private $identifier = false;
  41. /**
  42. * True if entity or proxy is loaded
  43. *
  44. * @var boolean
  45. */
  46. private $initialized = false;
  47. /**
  48. * Wrapp entity
  49. *
  50. * @param object $entity
  51. * @param \Doctrine\ORM\EntityManager $em
  52. */
  53. public function __construct($entity, EntityManager $em)
  54. {
  55. $this->em = $em;
  56. $this->entity = $entity;
  57. $this->meta = $em->getClassMetadata(get_class($this->entity));
  58. }
  59. /**
  60. * Get property value
  61. *
  62. * @param string $property
  63. * @return mixed
  64. */
  65. public function getPropertyValue($property)
  66. {
  67. $this->initialize();
  68. return $this->meta->getReflectionProperty($property)->getValue($this->entity);
  69. }
  70. /**
  71. * Populates the entity with given property values
  72. *
  73. * @param array $data
  74. * @return \Gedmo\Tool\Wrapper\EntityWrapper
  75. */
  76. public function populate(array $data)
  77. {
  78. foreach ($data as $field => $value) {
  79. $this->setPropertyValue($field, $value);
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Set the property
  85. *
  86. * @param string $property
  87. * @param mixed $value
  88. * @return \Gedmo\Tool\Wrapper\EntityWrapper
  89. */
  90. public function setPropertyValue($property, $value)
  91. {
  92. $this->initialize();
  93. $this->meta->getReflectionProperty($property)->setValue($this->entity, $value);
  94. return $this;
  95. }
  96. /**
  97. * Checks if identifier is valid
  98. *
  99. * @return boolean
  100. */
  101. public function hasValidIdentifier()
  102. {
  103. $result = true;
  104. foreach ($this->getIdentifier(false) as $field => $id) {
  105. if (!$id) {
  106. $result = false;
  107. break;
  108. }
  109. }
  110. return $result;
  111. }
  112. /**
  113. * Get entity class name
  114. *
  115. * @return string
  116. */
  117. public function getClassName()
  118. {
  119. return $this->meta->name;
  120. }
  121. /**
  122. * Get the entity identifier, single or composite
  123. *
  124. * @param boolean $single
  125. * @return array|mixed
  126. */
  127. public function getIdentifier($single = true)
  128. {
  129. if (false === $this->identifier) {
  130. if ($this->entity instanceof Proxy) {
  131. $uow = $this->em->getUnitOfWork();
  132. if ($uow->isInIdentityMap($this->entity)) {
  133. $this->identifier = $uow->getEntityIdentifier($this->entity);
  134. } else {
  135. $this->initialize();
  136. }
  137. }
  138. if (false === $this->identifier) {
  139. $this->identifier = array();
  140. foreach ($this->meta->identifier as $name) {
  141. $this->identifier[$name] = $this->getPropertyValue($name);
  142. }
  143. }
  144. }
  145. if ($single) {
  146. return reset($this->identifier);
  147. }
  148. return $this->identifier;
  149. }
  150. /**
  151. * Initialize the entity if it is proxy
  152. * required when is detached or not initialized
  153. */
  154. protected function initialize()
  155. {
  156. if (!$this->initialized) {
  157. if ($this->entity instanceof Proxy) {
  158. $uow = $this->em->getUnitOfWork();
  159. if (!$this->entity->__isInitialized__) {
  160. $this->entity->__load();
  161. }
  162. }
  163. }
  164. }
  165. }