| 1 |
<?php |
| 2 |
|
| 3 |
namespace WindPressDeps\enshrined\svgSanitize\ElementReference; |
| 4 |
|
| 5 |
class Subject |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var \DOMElement |
| 9 |
*/ |
| 10 |
protected $element; |
| 11 |
/** |
| 12 |
* @var Usage[] |
| 13 |
*/ |
| 14 |
protected $useCollection = []; |
| 15 |
/** |
| 16 |
* @var Usage[] |
| 17 |
*/ |
| 18 |
protected $usedInCollection = []; |
| 19 |
/** |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $useNestingLimit; |
| 23 |
/** |
| 24 |
* Subject constructor. |
| 25 |
* |
| 26 |
* @param \DOMElement $element |
| 27 |
* @param int $useNestingLimit |
| 28 |
*/ |
| 29 |
public function __construct(\DOMElement $element, $useNestingLimit) |
| 30 |
{ |
| 31 |
$this->element = $element; |
| 32 |
$this->useNestingLimit = $useNestingLimit; |
| 33 |
} |
| 34 |
/** |
| 35 |
* @return \DOMElement |
| 36 |
*/ |
| 37 |
public function getElement() |
| 38 |
{ |
| 39 |
return $this->element; |
| 40 |
} |
| 41 |
/** |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function getElementId() |
| 45 |
{ |
| 46 |
return $this->element->getAttribute('id'); |
| 47 |
} |
| 48 |
/** |
| 49 |
* @param array $subjects Previously processed subjects |
| 50 |
* @param int $level The current level of nesting. |
| 51 |
* @return bool |
| 52 |
* @throws \enshrined\svgSanitize\Exceptions\NestingException |
| 53 |
*/ |
| 54 |
public function hasInfiniteLoop(array $subjects = [], $level = 1) |
| 55 |
{ |
| 56 |
if ($level > $this->useNestingLimit) { |
| 57 |
throw new \WindPressDeps\enshrined\svgSanitize\Exceptions\NestingException('Nesting level too high, aborting', 1570713498, null, $this->getElement()); |
| 58 |
} |
| 59 |
if (in_array($this, $subjects, \true)) { |
| 60 |
return \true; |
| 61 |
} |
| 62 |
$subjects[] = $this; |
| 63 |
foreach ($this->useCollection as $usage) { |
| 64 |
if ($usage->getSubject()->hasInfiniteLoop($subjects, $level + 1)) { |
| 65 |
return \true; |
| 66 |
} |
| 67 |
} |
| 68 |
return \false; |
| 69 |
} |
| 70 |
/** |
| 71 |
* @param Subject $subject |
| 72 |
*/ |
| 73 |
public function addUse(Subject $subject) |
| 74 |
{ |
| 75 |
if ($subject === $this) { |
| 76 |
throw new \LogicException('Cannot add self usage', 1570713416); |
| 77 |
} |
| 78 |
$identifier = $subject->getElementId(); |
| 79 |
if (isset($this->useCollection[$identifier])) { |
| 80 |
$this->useCollection[$identifier]->increment(); |
| 81 |
return; |
| 82 |
} |
| 83 |
$this->useCollection[$identifier] = new Usage($subject); |
| 84 |
} |
| 85 |
/** |
| 86 |
* @param Subject $subject |
| 87 |
*/ |
| 88 |
public function addUsedIn(Subject $subject) |
| 89 |
{ |
| 90 |
if ($subject === $this) { |
| 91 |
throw new \LogicException('Cannot add self as usage', 1570713417); |
| 92 |
} |
| 93 |
$identifier = $subject->getElementId(); |
| 94 |
if (isset($this->usedInCollection[$identifier])) { |
| 95 |
$this->usedInCollection[$identifier]->increment(); |
| 96 |
return; |
| 97 |
} |
| 98 |
$this->usedInCollection[$identifier] = new Usage($subject); |
| 99 |
} |
| 100 |
/** |
| 101 |
* @param bool $accumulated |
| 102 |
* @return int |
| 103 |
*/ |
| 104 |
public function countUse($accumulated = \false) |
| 105 |
{ |
| 106 |
$count = 0; |
| 107 |
foreach ($this->useCollection as $use) { |
| 108 |
$useCount = $use->getSubject()->countUse(); |
| 109 |
$count += $use->getCount() * ($accumulated ? 1 + $useCount : max(1, $useCount)); |
| 110 |
} |
| 111 |
return $count; |
| 112 |
} |
| 113 |
/** |
| 114 |
* @return int |
| 115 |
*/ |
| 116 |
public function countUsedIn() |
| 117 |
{ |
| 118 |
$count = 0; |
| 119 |
foreach ($this->usedInCollection as $usedIn) { |
| 120 |
$count += $usedIn->getCount() * max(1, $usedIn->getSubject()->countUsedIn()); |
| 121 |
} |
| 122 |
return $count; |
| 123 |
} |
| 124 |
/** |
| 125 |
* Clear the internal arrays (to free up memory as they can get big) |
| 126 |
* and return all the child usages DOMElement's |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function clearInternalAndGetAffectedElements() |
| 131 |
{ |
| 132 |
$elements = array_map(function (Usage $usage) { |
| 133 |
return $usage->getSubject()->getElement(); |
| 134 |
}, $this->useCollection); |
| 135 |
$this->usedInCollection = []; |
| 136 |
$this->useCollection = []; |
| 137 |
return $elements; |
| 138 |
} |
| 139 |
} |
| 140 |
|