From 73899ce9878462d93cac5778dceb61d8caf498e2 Mon Sep 17 00:00:00 2001 From: Yusuf Kandemir Date: Thu, 27 Dec 2018 09:36:42 +0300 Subject: [PATCH] Moved document parsing logic to DocumentParser --- src/MicrodataDOMDocument.php | 47 -------------------------- src/MicrodataDocumentParser.php | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 47 deletions(-) delete mode 100644 src/MicrodataDOMDocument.php create mode 100644 src/MicrodataDocumentParser.php diff --git a/src/MicrodataDOMDocument.php b/src/MicrodataDOMDocument.php deleted file mode 100644 index bf3c47d..0000000 --- a/src/MicrodataDOMDocument.php +++ /dev/null @@ -1,47 +0,0 @@ -xpath->query('//*[@itemscope and not(@itemprop)]'); - } - - /** - * {@inheritdoc} - * Also assigns $xpath with DOMXPath of freshly loaded DOMDocument - */ - public function loadHTML($source, $options = 0) - { - $return = parent::loadHTML($source, $options); - - $this->xpath = new \DOMXPath($this); - - return $return; - } - - /** - * {@inheritdoc} - * Also assigns $xpath with DOMXPath of freshly loaded DOMDocument - */ - public function loadHTMLFile($filename, $options = 0) - { - $return = parent::loadHTMLFile($filename, $options); - - $this->xpath = new \DOMXPath($this); - - return $return; - } -} diff --git a/src/MicrodataDocumentParser.php b/src/MicrodataDocumentParser.php new file mode 100644 index 0000000..a590c4f --- /dev/null +++ b/src/MicrodataDocumentParser.php @@ -0,0 +1,59 @@ +dom = $dom; + $this->xpath = new \DOMXPath($this->dom); + + $this->elementParser = $elementParser ?? new MicrodataElementParser; + } + + /** + * Parses microdata and returns result as object + * + * @return \stdClass + */ + public function parse() : \stdClass + { + $result = new \stdClass; + + $result->items = []; + + foreach ($this->getTopLevelItems() as $item) { + $result->items[] = $this->elementParser->parse($item); + } + + return $result; + } + + /** + * Finds top level items in document + * + * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-top-level-microdata-item + * + * @return \DOMNodeList + */ + protected function getTopLevelItems() : \DOMNodeList + { + return $this->xpath->query('//*[@itemscope and not(@itemprop)]'); + } +}