Make handling of absolute uris changable through a callback
This commit is contained in:
parent
a4a5cce8d8
commit
dcd54585cc
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user