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;
    }