1<?php
2namespace enshrined\svgSanitize;
3
4class Helper
5{
6    /**
7     * @param \DOMElement $element
8     * @return string|null
9     */
10    public static function getElementHref(\DOMElement $element)
11    {
12        if ($element->hasAttribute('href')) {
13            return $element->getAttribute('href');
14        }
15        if ($element->hasAttributeNS('http://www.w3.org/1999/xlink', 'href')) {
16            return $element->getAttributeNS('http://www.w3.org/1999/xlink', 'href');
17        }
18        return null;
19    }
20
21    /**
22     * @param string $href
23     * @return string|null
24     */
25    public static function extractIdReferenceFromHref($href)
26    {
27        if (!is_string($href) || strpos($href, '#') !== 0) {
28            return null;
29        }
30        return substr($href, 1);
31    }
32
33    /**
34     * @param \DOMElement $needle
35     * @param \DOMElement $haystack
36     * @return bool
37     */
38    public static function isElementContainedIn(\DOMElement $needle, \DOMElement $haystack)
39    {
40        if ($needle === $haystack) {
41            return true;
42        }
43        foreach ($haystack->childNodes as $childNode) {
44            if (!$childNode instanceof \DOMElement) {
45                continue;
46            }
47            if (self::isElementContainedIn($needle, $childNode)) {
48                return true;
49            }
50        }
51        return false;
52    }
53}
54