|
1
|
|
|
<?php |
|
2
|
|
|
namespace SpareParts\Overseer\Assembly; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use SpareParts\Overseer\Context\IIdentityContext; |
|
6
|
|
|
use SpareParts\Overseer\Context\IVotingContext; |
|
7
|
|
|
use SpareParts\Overseer\StrategyEnum; |
|
8
|
|
|
use SpareParts\Overseer\Voter\IVoter; |
|
9
|
|
|
|
|
10
|
|
|
class VotingAbilityAwareAssembly extends VotingAssembly implements IVotingAbilityAwareAssembly |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $subjectClassName; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $contextClassName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $actionList; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \SpareParts\Overseer\StrategyEnum $strategy |
|
31
|
|
|
* @param IVoter[] $voters |
|
32
|
|
|
* @param string|string[] $actions |
|
33
|
|
|
* @param string $contextClassName |
|
34
|
|
|
* @param string $subjectClassName |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
StrategyEnum $strategy, |
|
38
|
|
|
array $voters, |
|
39
|
|
|
$actions = null, |
|
40
|
|
|
$contextClassName = IIdentityContext::class, |
|
41
|
|
|
$subjectClassName = null |
|
42
|
|
|
) { |
|
43
|
|
|
parent::__construct($strategy, $voters); |
|
44
|
|
|
|
|
45
|
|
|
$this->actionList = (array) $actions; |
|
46
|
|
|
$this->subjectClassName = $subjectClassName; |
|
47
|
|
|
$this->contextClassName = $contextClassName; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $actionName |
|
53
|
|
|
* @param object $subject |
|
54
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $context |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function canVoteOn($actionName, $subject, IVotingContext $context) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->subjectClassName && !($subject instanceof $this->subjectClassName)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($this->contextClassName && !($context instanceof $this->contextClassName)) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->actionList && !in_array($actionName, $this->actionList)) { |
|
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.