src/Patch.php
public function apply($targetDocument, $patchDocument)
    {
        if ($targetDocument === null || !is_object($targetDocument) || is_array($targetDocument)) {
            $targetDocument = new \stdClass();
        }

        if ($patchDocument === null || !is_object($patchDocument) || is_array($patchDocument)) {
            return $patchDocument;
        }

        foreach ($patchDocument as $key => $value) {
            if ($value === null) {
                unset($targetDocument->$key);
            } else {
                if (!isset($targetDocument->$key)) {
                    $targetDocument->$key = null;
                }
                $targetDocument->$key = $this->apply(
                    $targetDocument->$key,
                    $value
                );
            }
        }

        return $targetDocument;
    }
public function apply($targetDocument, $patchDocument)
    {
        if ($targetDocument === null || !is_object($targetDocument) || is_array($targetDocument)) {
            $targetDocument = new \stdClass();
        }

        if ($patchDocument === null || !is_object($patchDocument) || is_array($patchDocument)) {
            return $patchDocument;
        }

        foreach ($patchDocument as $key => $value) {
            if ($value === null) {
                unset($targetDocument->$key);
            } else {
                if (!isset($targetDocument->$key)) {
                    $targetDocument->$key = null;
                }
                $targetDocument->$key = $this->apply(
                    $targetDocument->$key,
                    $value
                );
            }
        }

        return $targetDocument;
    }
Apply patch on target document.

@param  mixed $targetDocument
@param  mixed $patchDocument
@return mixed