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
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 = [];
27 * @var $manager AssertionManager
29 protected $assertionManager;
31 protected $mode = self
::MODE_ALL
;
34 * Stacks an assertion in aggregate
36 * @param AssertionInterface|string $assertion
37 * if string, must match a AssertionManager declared service (checked later)
41 public function addAssertion($assertion)
43 $this->assertions
[] = $assertion;
48 public function addAssertions(array $assertions)
50 foreach ($assertions as $assertion) {
51 $this->addAssertion($assertion);
58 * Empties assertions stack
62 public function clearAssertions()
64 $this->assertions
= [];
71 * @param AssertionManager $manager
75 public function setAssertionManager(AssertionManager
$manager)
77 $this->assertionManager
= $manager;
82 public function getAssertionManager()
84 return $this->assertionManager
;
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
96 * indicates how assertion chain result should interpreted (either 'all' or 'at_least_one')
97 * @throws InvalidArgumentException
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');
113 * Return current mode
117 public function getMode()
123 * @see \Zend\Permissions\Acl\Assertion\AssertionInterface::assert()
125 * @throws RuntimeException
128 public function assert(
130 RoleInterface
$role = null,
131 ResourceInterface
$resource = 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();
145 if ($manager = $this->getAssertionManager()) {
147 $assertion = $manager->get($assertion);
148 } catch (\Exception
$e) {
149 throw new Exception\
InvalidAssertionException(
152 . '" is not defined in assertion manager'
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
168 if ($this->getMode() == self
::MODE_AT_LEAST_ONE
&& $result) {
169 // one true is enough
174 if ($this->getMode() == self
::MODE_ALL
) {
175 // none of the assertions returned false