MongoDocumentWrapper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ODM\MongoDB\DocumentManager;
  4. use Doctrine\ODM\MongoDB\Proxy\Proxy;
  5. /**
  6. * Wraps document or proxy for more convenient
  7. * manipulation
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Tool.Wrapper
  11. * @subpackage MongoDocumentWrapper
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MongoDocumentWrapper
  16. {
  17. /**
  18. * Wrapped Document
  19. *
  20. * @var object
  21. */
  22. protected $document;
  23. /**
  24. * DocumentManager instance
  25. *
  26. * @var \Doctrine\ODM\MongoDB\DocumentManager
  27. */
  28. protected $dm;
  29. /**
  30. * Document metadata
  31. *
  32. * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
  33. */
  34. protected $meta;
  35. /**
  36. * Document identifier
  37. *
  38. * @var mixed
  39. */
  40. private $identifier = false;
  41. /**
  42. * True if document or proxy is loaded
  43. *
  44. * @var boolean
  45. */
  46. private $initialized = false;
  47. /**
  48. * Wrapp document
  49. *
  50. * @param object $document
  51. * @param \Doctrine\ODM\MongoDB\DocumentManager $dm
  52. */
  53. public function __construct($document, DocumentManager $dm)
  54. {
  55. $this->dm = $dm;
  56. $this->document = $document;
  57. $this->meta = $dm->getClassMetadata(get_class($this->document));
  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->document);
  69. }
  70. /**
  71. * Populates the document with given property values
  72. *
  73. * @param array $data
  74. * @return \Gedmo\Tool\Wrapper\MongoDocumentWrapper
  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\MongoDocumentWrapper
  89. */
  90. public function setPropertyValue($property, $value)
  91. {
  92. $this->initialize();
  93. $this->meta->getReflectionProperty($property)->setValue($this->document, $value);
  94. return $this;
  95. }
  96. /**
  97. * Checks if identifier is valid
  98. *
  99. * @return boolean
  100. */
  101. public function hasValidIdentifier()
  102. {
  103. return (bool)$this->getIdentifier();
  104. }
  105. /**
  106. * Get document class name
  107. *
  108. * @return string
  109. */
  110. public function getClassName()
  111. {
  112. return $this->meta->name;
  113. }
  114. /**
  115. * Get the document identifier, single or composite
  116. *
  117. * @param boolean $single
  118. * @return array|mixed
  119. */
  120. public function getIdentifier($single = true)
  121. {
  122. if (false === $this->identifier) {
  123. if ($this->document instanceof Proxy) {
  124. $uow = $this->dm->getUnitOfWork();
  125. if ($uow->isInIdentityMap($this->document)) {
  126. $this->identifier = (string)$uow->getDocumentIdentifier($this->document);
  127. } else {
  128. $this->initialize();
  129. }
  130. }
  131. if (false === $this->identifier) {
  132. $this->identifier = (string)$this->getPropertyValue($this->meta->identifier);
  133. }
  134. }
  135. return $this->identifier;
  136. }
  137. /**
  138. * Initialize the document if it is proxy
  139. * required when is detached or not initialized
  140. */
  141. protected function initialize()
  142. {
  143. if (!$this->initialized) {
  144. if ($this->document instanceof Proxy) {
  145. $uow = $this->dm->getUnitOfWork();
  146. if (!$this->document->__isInitialized__) {
  147. $persister = $uow->getDocumentPersister($this->meta->name);
  148. $identifier = null;
  149. if ($uow->isInIdentityMap($this->document)) {
  150. $identifier = $this->getIdentifier();
  151. } else {
  152. // this may not happen but in case
  153. $reflProperty = new \ReflectionProperty($this->document, 'identifier');
  154. $reflProperty->setAccessible(true);
  155. $identifier = $reflProperty->getValue($this->document);
  156. }
  157. $this->document->__isInitialized__ = true;
  158. $persister->load($identifier, $this->document);
  159. }
  160. }
  161. }
  162. }
  163. }