public function extract(Document $document, Uri $ref)
    {
        $uri     = $this->resolver->resolve($document->getBaseUri(), $ref);
        $doc     = $this->getDocument($uri, $document);
        $result  = $doc->pointer($uri->getFragment());
        $baseUri = $doc->getBaseUri();

        // the extracted fragment gets merged into the root document so we must
        // resolve all $ref keys to the base uri so that the root document knows
        // where to find the $ref values
        array_walk_recursive($result, function (&$item, $key) use ($baseUri) {
            if ($key == '$ref') {
                $item = $this->resolver->resolve($baseUri, new Uri($item))->toString();
            }
        });

        return $result;
    }