Make handling of absolute uris changable through a callback

This commit is contained in:
Yusuf Kandemir 2018-12-17 20:15:13 +03:00
parent a4a5cce8d8
commit dcd54585cc
2 changed files with 31 additions and 6 deletions

View File

@ -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 * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-property-value for details of algorithm
* *
* @param callable $absoluteUriHandler
*
* @return $this|string * @return $this|string
*/ */
public function getPropertyValue() public function getPropertyValue(callable $absoluteUriHandler = null)
{ {
if ($this->hasAttribute('itemscope')) { if ($this->hasAttribute('itemscope')) {
return $this; return $this;
@ -106,8 +108,6 @@ class MicrodataDOMElement extends \DOMElement
return $this->getAttribute('content'); return $this->getAttribute('content');
} }
$base = $this->ownerDocument->documentURI;
$value = ''; $value = '';
if (\array_key_exists($this->tagName, self::$tagNameLookup)) { if (\array_key_exists($this->tagName, self::$tagNameLookup)) {
@ -115,7 +115,7 @@ class MicrodataDOMElement extends \DOMElement
$value = $this->getAttribute($attribute); $value = $this->getAttribute($attribute);
if (!empty($value) && \in_array($attribute, self::$absoluteAttributes) && !$this->isAbsoluteUri($value)) { if (!empty($value) && \in_array($attribute, self::$absoluteAttributes) && !$this->isAbsoluteUri($value)) {
$value = $base . $value; $value = $absoluteUriHandler($value, $this->ownerDocument->documentURI);
} }
} }

View File

@ -7,16 +7,31 @@ class MicrodataParser
/** @var MicrodataDOMDocument */ /** @var MicrodataDOMDocument */
protected $dom; 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. * MicrodataParser constructor.
* *
* @param MicrodataDOMDocument $dom * @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); $dom->registerNodeClass(\DOMElement::class, MicrodataDOMElement::class);
$this->dom = $dom; $this->dom = $dom;
$this->absoluteUriHandler = $absoluteUriHandler ?: function ($value, $base) {
return $base . $value;
};
} }
/** /**
@ -96,7 +111,7 @@ class MicrodataParser
$properties = new \stdClass; $properties = new \stdClass;
foreach ($item->getProperties() as $element) { foreach ($item->getProperties() as $element) {
$value = $element->getPropertyValue(); $value = $element->getPropertyValue($this->absoluteUriHandler);
if ($this->isItem($value)) { if ($this->isItem($value)) {
foreach ($memory as $memory_item) { foreach ($memory as $memory_item) {
@ -121,6 +136,16 @@ class MicrodataParser
return $result; 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 * Check if the given parameter is a MicrodataDOMElement and has itemscope attribute
* *