FluentDOM\Loader\Xml::loadFragment PHP Method

loadFragment() public method

See also: LoadableFragment::loadFragment
public loadFragment ( string $source, string $contentType, array | Traversable | Options $options = [] ) : DocumentFragment | null
$source string
$contentType string
$options array | Traversable | Options
return FluentDOM\DocumentFragment | null
    public function loadFragment($source, $contentType, $options = [])
    {
        if ($this->supports($contentType)) {
            $dom = new Document();
            $fragment = $dom->createDocumentFragment();
            $fragment->appendXml($source);
            return $fragment;
        }
        return NULL;
    }

Usage Example

Beispiel #1
0
 /**
  * @see Loadable::loadFragment
  *
  * @param mixed $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return DocumentFragment|NULL
  */
 public function loadFragment($source, $contentType, $options = [])
 {
     if (!$this->supports($contentType)) {
         return NULL;
     } elseif (is_string($source)) {
         $this->_xmlLoader = $this->_xmlLoader ?: new Xml();
         return $this->_xmlLoader->loadFragment($source, 'text/xml');
     } elseif ($source instanceof \SimpleXMLElement) {
         $node = dom_import_simplexml($source);
         $fragment = $node->ownerDocument->createDocumentFragment();
         $fragment->appendChild($node->cloneNode(TRUE));
         return $fragment;
     }
     throw new InvalidArgument('source', ['SimpleXMLElement', 'string']);
 }