composer package updates
[openemr.git] / vendor / zendframework / zend-permissions-acl / src / Assertion / AssertionAggregate.php
blob4c77557441aaf4cd2663044e6225f759570b90b0
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9 namespace Zend\Permissions\Acl\Assertion;
11 use Zend\Permissions\Acl\Acl;
12 use Zend\Permissions\Acl\Exception\InvalidArgumentException;
13 use Zend\Permissions\Acl\Exception\RuntimeException;
14 use Zend\Permissions\Acl\Resource\ResourceInterface;
15 use Zend\Permissions\Acl\Role\RoleInterface;
17 class AssertionAggregate implements AssertionInterface
19 const MODE_ALL = 'all';
21 const MODE_AT_LEAST_ONE = 'at_least_one';
23 protected $assertions = [];
25 /**
27 * @var $manager AssertionManager
29 protected $assertionManager;
31 protected $mode = self::MODE_ALL;
33 /**
34 * Stacks an assertion in aggregate
36 * @param AssertionInterface|string $assertion
37 * if string, must match a AssertionManager declared service (checked later)
39 * @return self
41 public function addAssertion($assertion)
43 $this->assertions[] = $assertion;
45 return $this;
48 public function addAssertions(array $assertions)
50 foreach ($assertions as $assertion) {
51 $this->addAssertion($assertion);
54 return $this;
57 /**
58 * Empties assertions stack
60 * @return self
62 public function clearAssertions()
64 $this->assertions = [];
66 return $this;
69 /**
71 * @param AssertionManager $manager
73 * @return self
75 public function setAssertionManager(AssertionManager $manager)
77 $this->assertionManager = $manager;
79 return $this;
82 public function getAssertionManager()
84 return $this->assertionManager;
87 /**
88 * Set assertion chain behavior
90 * AssertionAggregate should assert to true when:
92 * - all assertions are true with MODE_ALL
93 * - at least one assertion is true with MODE_AT_LEAST_ONE
95 * @param string $mode
96 * indicates how assertion chain result should interpreted (either 'all' or 'at_least_one')
97 * @throws InvalidArgumentException
99 * @return self
101 public function setMode($mode)
103 if ($mode != self::MODE_ALL && $mode != self::MODE_AT_LEAST_ONE) {
104 throw new InvalidArgumentException('invalid assertion aggregate mode');
107 $this->mode = $mode;
109 return $this;
113 * Return current mode
115 * @return string
117 public function getMode()
119 return $this->mode;
123 * @see \Zend\Permissions\Acl\Assertion\AssertionInterface::assert()
125 * @throws RuntimeException
126 * @return bool
128 public function assert(
129 Acl $acl,
130 RoleInterface $role = null,
131 ResourceInterface $resource = null,
132 $privilege = null
134 // check if assertions are set
135 if (! $this->assertions) {
136 throw new RuntimeException('no assertion have been aggregated to this AssertionAggregate');
139 foreach ($this->assertions as $assertion) {
140 // jit assertion mloading
141 if (! $assertion instanceof AssertionInterface) {
142 if (class_exists($assertion)) {
143 $assertion = new $assertion();
144 } else {
145 if ($manager = $this->getAssertionManager()) {
146 try {
147 $assertion = $manager->get($assertion);
148 } catch (\Exception $e) {
149 throw new Exception\InvalidAssertionException(
150 'assertion "'
151 . $assertion
152 . '" is not defined in assertion manager'
155 } else {
156 throw new RuntimeException('no assertion manager is set - cannot look up for assertions');
161 $result = (bool) $assertion->assert($acl, $role, $resource, $privilege);
163 if ($this->getMode() == self::MODE_ALL && ! $result) {
164 // on false is enough
165 return false;
168 if ($this->getMode() == self::MODE_AT_LEAST_ONE && $result) {
169 // one true is enough
170 return true;
174 if ($this->getMode() == self::MODE_ALL) {
175 // none of the assertions returned false
176 return true;
177 } else {
178 return false;