/** * 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."); } } } } }