ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Subject.php
Go to the documentation of this file.
1 <?php
3 
4 class Subject
5 {
9  protected $element;
10 
14  protected $useCollection = [];
15 
19  protected $usedInCollection = [];
20 
24  protected $useNestingLimit;
25 
33  {
34  $this->element = $element;
35  $this->useNestingLimit = $useNestingLimit;
36  }
37 
41  public function getElement()
42  {
43  return $this->element;
44  }
45 
49  public function getElementId()
50  {
51  return $this->element->getAttribute('id');
52  }
53 
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 
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 
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 
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 
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 
143  {
144  $elements = array_map(function(Usage $usage) {
145  return $usage->getSubject()->getElement();
147 
148  $this->usedInCollection = [];
149  $this->useCollection = [];
150 
151  return $elements;
152  }
153 }
__construct(\DOMElement $element, $useNestingLimit)
Subject constructor.
Definition: Subject.php:32
clearInternalAndGetAffectedElements()
Clear the internal arrays (to free up memory as they can get big) and return all the child usages DOM...
Definition: Subject.php:142
hasInfiniteLoop(array $subjects=[], $level=1)
Definition: Subject.php:60