33namespace PHPCR \Shell \Query ;
44
55use PHPCR \Query \RowInterface ;
6+ use PHPCR \Shell \Query \FunctionOperand ;
67
78/**
89 * Processor for node updates
@@ -18,8 +19,22 @@ class UpdateProcessor
1819
1920 public function __construct ()
2021 {
21- $ this ->functionMap = array (
22- 'array_replace ' => function ($ operand , $ v , $ x , $ y ) {
22+ $ this ->functionMapApply = array (
23+ 'mixin_add ' => function ($ operand , $ row , $ mixinName ) {
24+ $ node = $ row ->getNode ();
25+ $ node ->addMixin ($ mixinName );
26+ },
27+ 'mixin_remove ' => function ($ operand , $ row , $ mixinName ) {
28+ $ node = $ row ->getNode ();
29+
30+ if ($ node ->isNodeType ($ mixinName )) {
31+ $ node ->removeMixin ($ mixinName );
32+ }
33+ }
34+ );
35+
36+ $ this ->functionMapSet = array (
37+ 'array_replace ' => function ($ operand , $ row , $ v , $ x , $ y ) {
2338 $ operand ->validateScalarArray ($ v );
2439 foreach ($ v as $ key => $ value ) {
2540 if ($ value === $ x ) {
@@ -29,7 +44,7 @@ public function __construct()
2944
3045 return $ v ;
3146 },
32- 'array_remove ' => function ($ operand , $ v , $ x ) {
47+ 'array_remove ' => function ($ operand , $ row , $ v , $ x ) {
3348 foreach ($ v as $ key => $ value ) {
3449 if ($ value === $ x ) {
3550 unset($ v [$ key ]);
@@ -38,7 +53,7 @@ public function __construct()
3853
3954 return array_values ($ v );
4055 },
41- 'array_append ' => function ($ operand , $ v , $ x ) {
56+ 'array_append ' => function ($ operand , $ row , $ v , $ x ) {
4257 $ operand ->validateScalarArray ($ v );
4358 $ v [] = $ x ;
4459
@@ -49,10 +64,12 @@ public function __construct()
4964
5065 // first argument is the operand
5166 array_shift ($ values );
67+ // second is the row
68+ array_shift ($ values );
5269
5370 return $ values ;
5471 },
55- 'array_replace_at ' => function ($ operand , $ current , $ index , $ value ) {
72+ 'array_replace_at ' => function ($ operand , $ row , $ current , $ index , $ value ) {
5673 if (!isset ($ current [$ index ])) {
5774 throw new \InvalidArgumentException (sprintf (
5875 'Multivalue index "%s" does not exist ' ,
@@ -81,22 +98,32 @@ public function __construct()
8198 * @param PHPCR\Query\RowInterface
8299 * @param array
83100 */
84- public function updateNode (RowInterface $ row , $ propertyData )
101+ public function updateNodeSet (RowInterface $ row , $ propertyData )
85102 {
86103 $ node = $ row ->getNode ($ propertyData ['selector ' ]);
87104 $ value = $ propertyData ['value ' ];
88105
89106 if ($ value instanceof FunctionOperand) {
90- $ value = $ this ->handleFunction ($ row , $ propertyData );
107+ $ value = $ propertyData ['value ' ];
108+ $ value = $ value ->execute ($ this ->functionMapSet , $ row );
91109 }
92110
93111 $ node ->setProperty ($ propertyData ['name ' ], $ value );
94112 }
95113
114+ public function updateNodeApply (RowInterface $ row , FunctionOperand $ apply )
115+ {
116+ if (!$ apply instanceof FunctionOperand) {
117+ throw new \InvalidArgumentException (
118+ 'Was expecting a function operand but got something else '
119+ );
120+ }
121+
122+ $ apply ->execute ($ this ->functionMapApply , $ row );
123+ }
124+
96125 private function handleFunction ($ row , $ propertyData )
97126 {
98- $ value = $ propertyData ['value ' ];
99- $ value = $ value ->execute ($ this ->functionMap , $ row );
100127
101128 return $ value ;
102129 }
0 commit comments