/** * Merge this operation into a (previous) operation. * * @param IOperation $prevOp * @return IOperation */ public function mergeWith($prevOp) { if (!$prevOp) { return $this; } else { if ($prevOp instanceof SetOperation) { return new SetOperation($this->getKey(), $this->applyOn($prevOp->getValue())); } else { if ($prevOp instanceof IncrementOperation) { return new IncrementOperation($this->getKey(), $this->applyOn($prevOp->getValue())); } else { if ($prevOp instanceof DeleteOperation) { return new SetOperation($this->getKey(), $this->getValue()); } else { throw new \ErrorException("Operation incompatible with previous one."); } } } } }
/** * Merge this operation into a (previous) operation. * * @param IOperation $prevOp * @return IOperation */ public function mergeWith($prevOp) { if (!$prevOp) { return $this; } else { if ($prevOp instanceof SetOperation) { if (!is_array($prevOp->getValue())) { throw new \RuntimeException("Operation incompatible " . "with previous value."); } return new SetOperation($this->key, $this->applyOn($prevOp->getValue())); } else { if ($prevOp instanceof ArrayOperation && $this->getOpType() === $prevOp->getOpType()) { if ($this->getOpType() === "Remove") { $objects = array_merge($prevOp->getValue(), $this->getValue()); } else { $objects = $this->applyOn($prevOp->getValue()); } return new ArrayOperation($this->key, $objects, $this->getOpType()); } else { if ($prevOp instanceof DeleteOperation) { if ($this->getOpType() === "Remove") { return $prevOp; } else { return new SetOperation($this->getKey(), $this->applyOn(null)); } } else { throw new \RuntimeException("Operation incompatible with" . " previous one."); } } } } }
/** * Apply operation * * @param IOperation $operation * @return void */ private function _applyOperation($operation) { $key = $operation->getKey(); $oldval = $this->get($key); $newval = $operation->applyOn($oldval, $this); if ($newval !== null) { $this->_data[$key] = $newval; } else { if (isset($this->_data[$key])) { unset($this->_data[$key]); } } $prevOp = $this->_getPreviousOp($key); $newOp = $prevOp ? $operation->mergeWith($prevOp) : $operation; $this->_operationSet[$key] = $newOp; }