Inheritance: extends DOMDocument, implements FluentDOM\Node\ParentNode, use trait FluentDOM\Node\ParentNode\Properties, use trait Node\QuerySelector\Implementation, use trait FluentDOM\Node\Xpath
示例#1
0
 /**
  * Create a namespace optimizer for the provided document. The provided
  * document will be copied.
  *
  * The second argument allows to provide namespaces and prefixes. The
  * keys of the array are the namespace uri, the values are the prefixes.
  *
  * If a namespace is not provided it is read from the source node.
  *
  * You can use the same prefix for multiple namespace uris. Empty prefixes
  * are possible (default namespace for an element).
  *
  * It is highly recommend that you always use a non-empty prefix if the
  * here are attributes in that namespace. Attributes always need a prefix
  * to make use of the namespace.
  *
  * @param \DOMDocument $document
  * @param array $namespaces
  */
 public function __construct(\DOMDocument $document, array $namespaces = [])
 {
     $this->_document = new Document();
     foreach ($document->childNodes as $node) {
         $this->_document->appendChild($this->_document->importNode($node, TRUE));
     }
     $this->_namespaceUris = $namespaces;
 }
示例#2
0
 /**
  * @see Loadable::load
  * @param \SimpleXMLElement $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return Document|Result|NULL
  */
 public function load($source, $contentType, $options = [])
 {
     if ($source instanceof \SimpleXMLElement) {
         $document = new Document();
         $document->appendChild($document->importNode(dom_import_simplexml($source), TRUE));
         return new Result($document, 'text/xml');
     }
     return NULL;
 }
示例#3
0
 /**
  * Load the json string into an DOMDocument
  *
  * @param mixed $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return Document|Result|NULL
  */
 public function load($source, $contentType, $options = [])
 {
     if (FALSE !== ($json = $this->getJson($source, $contentType))) {
         $document = new Document('1.0', 'UTF-8');
         $document->appendChild($root = $document->createElementNS(self::XMLNS, 'json:json'));
         $this->transferTo($root, $json);
         return $document;
     }
     return NULL;
 }
示例#4
0
 /**
  * @see Loadable::loadFragment
  *
  * @param string $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return DocumentFragment|NULL
  */
 public function loadFragment($source, $contentType, $options = [])
 {
     if (FALSE !== ($json = $this->getJson($source, $contentType))) {
         $document = new Document('1.0', 'UTF-8');
         $fragment = $document->createDocumentFragment();
         $this->transferTo($fragment, $json);
         return $fragment;
     }
     return NULL;
 }
示例#5
0
 /**
  * @see LoadableFragment::loadFragment
  * @param string $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return 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;
 }
 /**
  * @see Loadable::load
  * @param mixed $source
  * @param string $contentType
  * @param array $options
  * @return Document|NULL
  */
 public function load($source, $contentType, array $options = [])
 {
     if ($this->supports($contentType) && ($this->_lines = $this->getLines($source))) {
         $dom = new Document('1.0', 'UTF-8');
         $dom->registerNamespace('', $this->_namespace);
         $dom->appendElement($this->_nodeNames['root'])->append($this);
         return $dom;
     }
     return NULL;
 }
示例#7
0
 /**
  * @see Loadable::loadFragment
  *
  * @param string $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return DocumentFragment|NULL
  */
 public function loadFragment($source, $contentType, $options = [])
 {
     $hasHeaderLine = isset($options['FIELDS']) ? FALSE : isset($options['HEADER']) && $options['HEADER'];
     $this->configure($options);
     if ($this->supports($contentType) && ($lines = $this->getLines($source))) {
         $document = new Document('1.0', 'UTF-8');
         $fragment = $document->createDocumentFragment();
         $this->appendLines($fragment, $lines, $hasHeaderLine, isset($options['FIELDS']) ? $options['FIELDS'] : NULL);
         return $fragment;
     }
     return NULL;
 }
示例#8
0
 /**
  * @see Loadable::load
  * @param \PDOStatement $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return Document|Result|NULL
  */
 public function load($source, $contentType, $options = [])
 {
     if ($source instanceof \PDOStatement) {
         $document = new Document('1.0', 'UTF-8');
         $document->registerNamespace('json', self::XMLNS);
         $root = $document->appendElement('json:json');
         $source->setFetchMode(\PDO::FETCH_OBJ);
         foreach ($source as $row) {
             $child = $root->appendElement('_');
             $this->transferTo($child, $row, 1);
         }
         return new Result($document, 'text/xml');
     }
     return NULL;
 }
示例#9
0
 /**
  * Load the YAML string into an DOMDocument
  *
  * @param mixed $source
  * @param string $contentType
  * @param array $options
  * @return Document|NULL
  */
 public function load($source, $contentType, array $options = [])
 {
     if ($this->supports($contentType)) {
         if (FALSE === strpos($source, "\n")) {
             $source = file_get_contents($source);
         }
         $yaml = Dipper::parse($source);
         if (!empty($yaml) || is_array($yaml)) {
             $dom = new Document('1.0', 'UTF-8');
             $dom->appendChild($root = $dom->createElementNS(self::XMLNS, 'json:json'));
             $this->transferTo($root, $yaml);
             return $dom;
         }
     }
     return NULL;
 }
示例#10
0
 /**
  * @param Document|Element $parent
  * @param \DOMElement $node
  * @param bool $addNameAttribute
  */
 public function addNode($parent, \DOMElement $node, $addNameAttribute = FALSE)
 {
     switch ($this->getType($node)) {
         case 'object':
             $result = $parent->appendElement('json:object');
             $this->appendChildNodes($result, $node, TRUE);
             break;
         case 'array':
             $result = $parent->appendElement('json:array');
             $this->appendChildNodes($result, $node, FALSE);
             break;
         case 'number':
             $result = $parent->appendElement('json:number', $node->nodeValue);
             break;
         case 'boolean':
             $result = $parent->appendElement('json:boolean', $node->nodeValue);
             break;
         case 'null':
             $result = $parent->appendElement('json:null');
             break;
         default:
             $result = $parent->appendElement('json:string', $node->nodeValue);
             break;
     }
     if ($addNameAttribute) {
         $name = $node->localName;
         if ($node->hasAttributeNS(self::XMLNS_JSONDOM, 'name')) {
             $name = $node->getAttributeNS(self::XMLNS_JSONDOM, 'name');
         }
         $result['name'] = $name;
     }
 }
示例#11
0
 /**
  * @see Loadable::loadFragment
  *
  * @param string $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return DocumentFragment|NULL
  */
 public function loadFragment($source, $contentType, $options = [])
 {
     if ($this->supports($contentType) && !empty($source)) {
         $document = new Document();
         $document->preserveWhiteSpace = FALSE;
         $document->registerNamespace('jx', self::XMLNS_JSONX);
         $sourceFragment = $document->createDocumentFragment();
         $sourceFragment->appendXml($source);
         $target = new Document();
         $target->registerNamespace('json', self::XMLNS_JSONDOM);
         $targetFragment = $target->createDocumentFragment();
         foreach ($sourceFragment->childNodes as $node) {
             $this->transferNode($node, $targetFragment);
         }
         return $targetFragment;
     }
     return NULL;
 }
示例#12
0
 /**
  * Load a HTML5 file and copy it into a FluentDOM\Document
  *
  * @codeCoverageIgnore
  *
  * @see Loadable::load
  * @param string $source
  * @param string $contentType
  * @param array $options
  * @return Document|NULL
  */
 public function load($source, $contentType, array $options = [])
 {
     if ($this->supports($contentType)) {
         $html5 = new HTML5Support();
         if ($this->startsWith($source, '<')) {
             $dom = $html5->loadHTML($source);
         } else {
             $dom = $html5->loadHTMLFile($source);
         }
         if (!$dom instanceof Document) {
             $fd = new Document();
             if ($dom->documentElement instanceof \DOMElement) {
                 $fd->appendChild($fd->importNode($dom->documentElement, TRUE));
             }
             $dom = $fd;
         }
         $dom->registerNamespace('html', 'http://www.w3.org/1999/xhtml');
         return $dom;
     }
     return NULL;
 }
示例#13
0
 /**
  * Given a source (URL), retrieve raw data
  * and turn it into an array of arrays of product data.
  *
  * Each sub array should contain the following keys:
  *  - title (string)     - the title of the product
  *  - size (int)         - bytes of raw data
  *  - unit_price (float) - the price of the product
  *  - description        - description of the product
  *
  * @param string $source Information about where to load data from.
  *
  * @throws InvalidDataSourceException If the source is invalid.
  * @throws MalformedDataException     If the data is malformed.
  *
  * @return array
  */
 public function getProductData($source)
 {
     // We will save errors to an array.
     libxml_use_internal_errors(true);
     if (false === $this->looksLikeUrl($source)) {
         throw new InvalidDataSourceException("This isn't a URL.");
     }
     // Create a DOM parser & load the HTML.
     $dom = new DOMParser();
     $listHtml = $this->scraper->getHtml($source);
     $return = array();
     $dom->loadHTML($listHtml);
     foreach ($dom->querySelectorAll("div.product") as $product) {
         // Get the title element & price element
         $titleElement = $product->querySelector("div.productInfo")->querySelector("a");
         $priceElement = $product->querySelector("p.pricePerUnit");
         // Load the product-specific page.
         $productUrl = $titleElement->getAttributeNode("href")->textContent;
         $productHtml = $this->scraper->getHtml($productUrl);
         $productDom = new DOMParser();
         $productDom->loadHTML($productHtml);
         // Get the product description.
         // Note: this does not have a unique ID, this is very volatile.
         $descriptionElement = $productDom->querySelector("div#information")->querySelector("div.productText");
         // Set up the product data.
         $productData = array();
         $productData['title'] = trim($titleElement->textContent);
         $productData['description'] = trim($descriptionElement->textContent);
         $productData['size'] = strlen($productHtml);
         $productData['unit_price'] = (double) preg_replace('/[^0-9.]/', '', $priceElement->textContent);
         $return[] = $productData;
     }
     // Put errors in $this->errors.
     $this->errors = libxml_get_errors();
     libxml_clear_errors();
     return $return;
 }
示例#14
0
 private function loadFragmentIntoDom(\DOMDocument $document, $source, $loadOptions)
 {
     $htmlDom = new Document();
     $htmlDom->loadHTML('<html-fragment>' . $source . '</html-fragment>', $loadOptions);
     $nodes = $htmlDom->evaluate('//html-fragment[1]/node()');
     foreach ($nodes as $node) {
         if ($importedNode = $document->importNode($node, TRUE)) {
             $document->appendChild($importedNode);
         }
     }
 }
示例#15
0
 /**
  * @param string $target
  * @param string $content
  * @return \DOMProcessingInstruction
  */
 public function pi($target, $content)
 {
     return $this->_document->createProcessingInstruction($target, $content);
 }
示例#16
0
 /**
  * @param string $source
  * @param string $contentType
  * @param array|\Traversable|Options $options
  * @return \FluentDOM\DocumentFragment|null
  */
 public function loadFragment($source, $contentType, $options = [])
 {
     if ($this->supports($contentType)) {
         $dom = new Document('1.0', 'UTF-8');
         $fragment = $dom->createDocumentFragment();
         $onMapKey = $this->_onMapKey;
         if (isset($options[self::ON_MAP_KEY]) && is_callable($options[self::ON_MAP_KEY])) {
             $this->onMapKey($options[self::ON_MAP_KEY]);
         }
         $this->transferTo($fragment, json_decode($source), $this->_recursions);
         $this->_onMapKey = $onMapKey;
         return $fragment;
     }
     return NULL;
 }