From dcd54585cc8442a72c7c93d8e3cfcd2d52b7ee14 Mon Sep 17 00:00:00 2001 From: Yusuf Kandemir Date: Mon, 17 Dec 2018 20:15:13 +0300 Subject: [PATCH] Make handling of absolute uris changable through a callback --- src/MicrodataDOMElement.php | 8 ++++---- src/MicrodataParser.php | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/MicrodataDOMElement.php b/src/MicrodataDOMElement.php index 95acff7..d5acdb0 100644 --- a/src/MicrodataDOMElement.php +++ b/src/MicrodataDOMElement.php @@ -94,9 +94,11 @@ class MicrodataDOMElement extends \DOMElement /** * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-property-value for details of algorithm * + * @param callable $absoluteUriHandler + * * @return $this|string */ - public function getPropertyValue() + public function getPropertyValue(callable $absoluteUriHandler = null) { if ($this->hasAttribute('itemscope')) { return $this; @@ -106,8 +108,6 @@ class MicrodataDOMElement extends \DOMElement return $this->getAttribute('content'); } - $base = $this->ownerDocument->documentURI; - $value = ''; if (\array_key_exists($this->tagName, self::$tagNameLookup)) { @@ -115,7 +115,7 @@ class MicrodataDOMElement extends \DOMElement $value = $this->getAttribute($attribute); if (!empty($value) && \in_array($attribute, self::$absoluteAttributes) && !$this->isAbsoluteUri($value)) { - $value = $base . $value; + $value = $absoluteUriHandler($value, $this->ownerDocument->documentURI); } } diff --git a/src/MicrodataParser.php b/src/MicrodataParser.php index 3c3aba5..da4bd24 100644 --- a/src/MicrodataParser.php +++ b/src/MicrodataParser.php @@ -7,16 +7,31 @@ class MicrodataParser /** @var MicrodataDOMDocument */ protected $dom; + /** + * Handler will be called with $value(non-absolute uri string) and $base(base uri) parameters + * + * Should return a string value + * + * @var callable + */ + private $absoluteUriHandler; + /** * MicrodataParser constructor. * * @param MicrodataDOMDocument $dom + * @param callable $absoluteUriHandler Can be set later with MicrodataParser::setAbsoluteUriHandler() + * + * @see MicrodataParser::$absoluteUriHandler */ - public function __construct(MicrodataDOMDocument $dom) + public function __construct(MicrodataDOMDocument $dom, callable $absoluteUriHandler = null) { $dom->registerNodeClass(\DOMElement::class, MicrodataDOMElement::class); $this->dom = $dom; + $this->absoluteUriHandler = $absoluteUriHandler ?: function ($value, $base) { + return $base . $value; + }; } /** @@ -96,7 +111,7 @@ class MicrodataParser $properties = new \stdClass; foreach ($item->getProperties() as $element) { - $value = $element->getPropertyValue(); + $value = $element->getPropertyValue($this->absoluteUriHandler); if ($this->isItem($value)) { foreach ($memory as $memory_item) { @@ -121,6 +136,16 @@ class MicrodataParser return $result; } + /** + * Set absolute uri handler + * + * @param callable $handler + */ + public function setAbsoluteUriHandler(callable $handler) + { + $this->absoluteUriHandler = $handler; + } + /** * Check if the given parameter is a MicrodataDOMElement and has itemscope attribute *