/var/www/vhosts/ihelp.ro/httpdocs/vendor/mirko-pagliai/php-tools/src
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/mirko-pagliai/php-tools/src/BodyParser.php (2558B)
*/
protected array $extractedLinks = [];
/**
* HTML tags that may contain links and therefore need to be scanned.
*
* Array with tag names as keys and attribute names as values.
* @var array
*/
protected const TAGS = [
'a' => 'href',
'area' => 'href',
'audio' => 'src',
'embed' => 'src',
'frame' => 'src',
'iframe' => 'src',
'img' => 'src',
'link' => 'href',
'script' => 'src',
'source' => 'src',
'track' => 'src',
'video' => 'src',
];
/**
* Reference url. Used to determine the relative links
* @var string
*/
protected string $url;
/**
* Constructor
* @param string|\Psr\Http\Message\StreamInterface $body Body
* @param string $url Reference url. Used to determine the relative links
*/
public function __construct($body, string $url)
{
$this->body = (string)$body;
$this->url = $url;
}
/**
* Extracts links from body
* @return array
Array of links
*/
public function extractLinks(): array
{
if ($this->extractedLinks) {
return $this->extractedLinks;
}
if (!is_html($this->body)) {
return [];
}
$crawler = new Crawler($this->body);
foreach (self::TAGS as $tag => $attribute) {
foreach ($crawler->filterXPath('//' . $tag)->extract([$attribute]) as $link) {
if ($link) {
$links[] = clean_url(url_to_absolute($this->url, $link), true, true);
}
}
}
return $this->extractedLinks = array_unique($links ?? []);
}
}