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