EMongoModifier.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * EMongoModifier.php
  4. *
  5. * PHP version 5.2+
  6. *
  7. * @author Philippe Gaultier <pgaultier@ibitux.com>
  8. * @copyright 2011 Ibitux http://www.ibitux.com
  9. * @license http://www.yiiframework.com/license/ BSD license
  10. * @version xxx
  11. * @category ext
  12. * @package ext.YiiMongoDbSuite
  13. *
  14. */
  15. /**
  16. * EMongoModifier class
  17. *
  18. * This class is a helper for building MongoDB atomic updates
  19. *
  20. * 1. addCond method
  21. * $criteriaObject->addCond($fieldName, $operator, $vale); // this will produce fieldName <operator> value
  22. *
  23. * For modifiers list {@see EMongoModifier::$modifiers}
  24. *
  25. * @author Philippe Gaultier <pgaultier@ibitux.com>
  26. * @since v1.3.6
  27. */
  28. class EMongoModifier extends CComponent
  29. {
  30. /**
  31. * @since v1.3.6
  32. * @var array $modifiers supported modifiers
  33. */
  34. public static $modifiers = array(
  35. 'inc' => '$inc',
  36. 'set' => '$set',
  37. 'unset' => '$unset',
  38. 'push' => '$push',
  39. 'pushAll' => '$pushAll',
  40. 'addToSet' => '$addToSet',
  41. 'pop' => '$pop',
  42. 'pull' => '$pull',
  43. 'pullAll' => '$pullAll',
  44. 'rename' => '$rename',
  45. );
  46. private $_fields = array();
  47. /**
  48. * Constructor
  49. * Modifier sample:
  50. *
  51. * <PRE>
  52. * 'modifier' = array(
  53. * 'fieldName1'=>array('inc' => $incValue),
  54. * 'fieldName2'=>array('set' => $targetValue),
  55. * 'fieldName3'=>array('unset' => 1),
  56. * 'fieldName4'=>array('push' => $pushedValue),
  57. * 'fieldName5'=>array('pushAll' => array($pushedValue1, $pushedValue2)),
  58. * 'fieldName6'=>array('addToSet' => $addedValue),
  59. * 'fieldName7'=>array('pop' => 1),
  60. * 'fieldName8'=>array('pop' => -1),
  61. * 'fieldName9'=>array('pull' => $removedValue),
  62. * 'fieldName10'=>array('pullAll' => array($removedValue1, $removedValue2)),
  63. * 'fieldName11'=>array('rename' => $newFieldName),
  64. * );
  65. * </PRE>
  66. * @param array $modifier basic definition of modifiers
  67. * @since v1.3.6
  68. */
  69. public function __construct($modifier=null)
  70. {
  71. if(is_array($modifier))
  72. {
  73. foreach($modifier as $fieldName=>$rules)
  74. {
  75. foreach($rules as $mod=>$value) {
  76. $this->_fields[$fieldName] = array(self::$modifiers[$mod] => $value);
  77. }
  78. }
  79. }
  80. else if($modifier instanceof EMongoModifier)
  81. $this->mergeWith($modifier);
  82. }
  83. /**
  84. * Compute modifier to be able to initiate request
  85. * @return array
  86. */
  87. public function getModifiers()
  88. {
  89. $modifier = array();
  90. foreach($this->_fields as $fieldName=>$rule)
  91. {
  92. foreach($rule as $operator=>$value)
  93. {
  94. if(isset($modifier[$operator]) && is_array($modifier[$operator]))
  95. {
  96. $modifier[$operator] = array_merge($modifier[$operator], array($fieldName=>$value));
  97. } else {
  98. $modifier[$operator] = array($fieldName=>$value);
  99. }
  100. }
  101. }
  102. return $modifier;
  103. }
  104. public function getFields()
  105. {
  106. return $this->_fields;
  107. }
  108. /**
  109. * Add a new set of modifiers to current modifiers. If modifiers has already been
  110. * added for specific field, they will be overwritten.
  111. *
  112. * @param EMongoModifier $modifier modifier to merge into current object
  113. * @return EMongoModifier
  114. */
  115. public function mergeWith($modifier)
  116. {
  117. if(is_array($modifier))
  118. $modifier = new EMongoModifier($modifier);
  119. else if(empty($modifier))
  120. return $this;
  121. foreach($modifier->getFields() as $fieldName=>$rule)
  122. {
  123. $this->_fields[$fieldName] = $rule;
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Add a new modifier rule to specific field
  129. * @param string $fieldName name of the field we want to update
  130. * @param string $modifier type of the modifier @see EMongoModifier::$modifiers
  131. * @param mixed $value value used by the modifier
  132. * @return EMongoModifier
  133. */
  134. public function addModifier($fieldName, $modifier, $value)
  135. {
  136. $this->_fields[$fieldName] = array(self::$modifiers[$modifier]=>$value);
  137. return $this;
  138. }
  139. /**
  140. * Check if we have modifiers to apply
  141. * @return boolean
  142. */
  143. public function getCanApply() {
  144. if(count($this->_fields) > 0) {
  145. return true;
  146. } else {
  147. return false;
  148. }
  149. }
  150. }