EMongoSoftDocument.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * EMongoSoftDocument.php
  4. *
  5. * PHP version 5.2+
  6. *
  7. * @author Dariusz Górecki <darek.krk@gmail.com>
  8. * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
  9. * @copyright 2011 CleverIT http://www.cleverit.com.pl
  10. * @license http://www.yiiframework.com/license/ BSD license
  11. * @version 1.3
  12. * @category ext
  13. * @package ext.YiiMongoDbSuite
  14. * @since v1.3.4
  15. */
  16. /**
  17. * EmongoSoftDocument cass
  18. * @since v1.3.4
  19. */
  20. abstract class EMongoSoftDocument extends EMongoDocument
  21. {
  22. /**
  23. * Array that holds initialized soft attributes
  24. * @var array $softAttributes
  25. * @since v1.3.4
  26. */
  27. protected $softAttributes = array();
  28. /**
  29. * Adds soft attributes support to magic __get method
  30. * @see EMongoEmbeddedDocument::__get()
  31. * @since v1.3.4
  32. */
  33. public function __get($name)
  34. {
  35. if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
  36. {
  37. return $this->softAttributes[$name];
  38. }
  39. else
  40. return parent::__get($name);
  41. }
  42. /**
  43. * Adds soft attributes support to magic __set method
  44. * @see EMongoEmbeddedDocument::__set()
  45. * @since v1.3.4
  46. */
  47. public function __set($name, $value)
  48. {
  49. if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
  50. {
  51. $this->softAttributes[$name] = $value;
  52. }
  53. else
  54. parent::__set($name, $value);
  55. }
  56. /**
  57. * Adds soft attributes support to magic __isset method
  58. * @see EMongoEmbeddedDocument::__isset()
  59. * @since v1.3.4
  60. */
  61. public function __isset($name)
  62. {
  63. if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
  64. return true;
  65. else
  66. return parent::__isset($name);
  67. }
  68. /**
  69. * Adds soft attributes support to magic __unset method
  70. * @see CComponent::__unset()
  71. * @since v1.3.4
  72. */
  73. public function __unset($name)
  74. {
  75. if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
  76. unset($this->softAttributes[$name]);
  77. else
  78. parent::__unset($name);
  79. }
  80. /**
  81. * Initializes a soft attribute, before it can be used
  82. * @param string $name attribute name
  83. * @since v1.3.4
  84. */
  85. public function initSoftAttribute($name)
  86. {
  87. if(!array_key_exists($name, $this->softAttributes))
  88. $this->softAttributes[$name] = null;
  89. }
  90. /**
  91. * Initializes a soft attributes, from given list, before they can be used
  92. * @param mixed $attributes attribute names list
  93. * @since v1.3.4
  94. */
  95. public function initSoftAttributes($attributes)
  96. {
  97. foreach($attributes as $name)
  98. $this->initSoftAttribute($name);
  99. }
  100. /**
  101. * Return the list of attribute names of this model, with respect of initialized soft attributes
  102. * @see EMongoEmbeddedDocument::attributeNames()
  103. * @since v1.3.4
  104. */
  105. public function attributeNames()
  106. {
  107. return array_merge(array_keys($this->softAttributes), parent::attributeNames());
  108. }
  109. /**
  110. * Instantiate the model object from given document, with respect of soft attributes
  111. * @see EMongoDocument::instantiate()
  112. * @since v1.3.4
  113. */
  114. protected function instantiate($attributes)
  115. {
  116. $class=get_class($this);
  117. $model=new $class(null);
  118. $model->initEmbeddedDocuments();
  119. $model->initSoftAttributes(
  120. array_diff(
  121. array_keys($attributes),
  122. parent::attributeNames()
  123. )
  124. );
  125. $model->setAttributes($attributes, false);
  126. return $model;
  127. }
  128. /**
  129. * This method does the actual convertion to an array
  130. * Does not fire any events
  131. * @return array an associative array of the contents of this object
  132. * @since v1.3.4
  133. */
  134. protected function _toArray()
  135. {
  136. $arr = parent::_toArray();
  137. foreach($this->softAttributes as $key => $value)
  138. $arr[$key]=$value;
  139. return $arr;
  140. }
  141. /**
  142. * Return the actual list of soft attributes being used by this model
  143. * @return array list of initialized soft attributes
  144. * @since v1.3.4
  145. */
  146. public function getSoftAttributeNames()
  147. {
  148. return array_diff(
  149. array_keys($this->softAttributes),
  150. parent::attributeNames()
  151. );
  152. }
  153. }