Beispiel #1
0
 /**
  * Return an integer less than, equal to, or greater than zero if the
  * this instance is considered to be respectively less than, equal to,
  * or greater than the $other rule;
  *
  * @param PolicyRule $other
  * @return integer
  */
 public function compare(PolicyRule $other)
 {
     if ($this->getSpecificity() === $other->getSpecificity()) {
         return 0;
     } elseif ($this->getSpecificity() > $other->getSpecificity()) {
         return 1;
     } else {
         return -1;
     }
 }
 private function loadJsonString($policyName, $data)
 {
     $rawRules = json_decode($data);
     if ($rawRules === NULL) {
         $jsonErrorCode = json_last_error();
         $jsonErrorMessage = $this->getJsonErrorReason($jsonErrorCode);
         throw new \UnexpectedValueException(sprintf("%s: error while reading policy file `%s`. Reason: %s (%d)", __CLASS__, $policyName, $jsonErrorMessage, $jsonErrorCode), 1327572840);
     }
     if (!is_array($rawRules)) {
         throw new \UnexpectedValueException(sprintf("%s: invalid policy file `%s`.", __CLASS__, $policyName), 1327572841);
     }
     foreach ($rawRules as $rawRule) {
         $ruleObject = PolicyRule::createFromObject($rawRule);
         // skip existing "final" rule:
         if (isset($this->rules[$ruleObject->getIdentifier()])) {
             if ($this->rules[$ruleObject->getIdentifier()]->isFinal()) {
                 continue;
             } else {
                 $this->getLog()->notice(sprintf('%s: rule#%d is overridden in policy `%s`', __CLASS__, $ruleObject->getIdentifier(), $policyName));
             }
         }
         $this->rules[$ruleObject->getIdentifier()] = $ruleObject;
     }
     // reverse sorting:
     $this->rules->uasort(function (PolicyRule $a, PolicyRule $b) {
         return -$a->compare($b);
     });
     return $this;
 }