PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
optimole-wp / vendor / enshrined / svg-sanitize / src / ElementReference / Subject.php

Subject.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization trunk, at vendor/enshrined/svg-sanitize/src/ElementReference/Subject.php

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