[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/enshrined/svg-sanitize/src/ElementReference/ -> Subject.php (source)

   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  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer