Rs\Json\Patch\Operations\Remove::perform PHP Method

perform() public method

public perform ( string $targetDocument ) : string
$targetDocument string
return string
    public function perform($targetDocument)
    {
        $pointer = new Pointer($targetDocument);
        try {
            $pointer->get($this->getPath());
        } catch (NonexistentValueReferencedException $e) {
            return $targetDocument;
        }
        $targetDocument = json_decode($targetDocument);
        $this->remove($targetDocument, $this->getPointerParts());
        return json_encode($targetDocument, JSON_UNESCAPED_UNICODE);
    }

Usage Example

Example #1
0
 /**
  * @param  string $targetDocument
  *
  * @throws \Rs\Json\Patch\InvalidOperationException
  * @throws \Rs\Json\Pointer\InvalidJsonException
  * @throws \Rs\Json\Pointer\InvalidPointerException
  * @throws \Rs\Json\Pointer\NonWalkableJsonException
  * @throws \RuntimeException
  * @return string
  */
 public function perform($targetDocument)
 {
     $pointer = new Pointer($targetDocument);
     try {
         $get = $pointer->get($this->getFrom());
     } catch (NonexistentValueReferencedException $e) {
         return $targetDocument;
     }
     if ($this->getFrom() === $this->getPath()) {
         return $targetDocument;
     }
     $removeOperation = new \stdClass();
     $removeOperation->path = $this->getFrom();
     $remove = new Remove($removeOperation);
     $targetDocument = $remove->perform($targetDocument);
     $addOperation = new \stdClass();
     $addOperation->path = $this->getPath();
     $addOperation->value = $get;
     $add = new Add($addOperation);
     return $add->perform($targetDocument);
 }