1 | <?php |
||
17 | class ComparisonManager implements ComparisonManagerInterface |
||
18 | { |
||
19 | /** @var AttributeManager **/ |
||
20 | protected $attributeManager; |
||
21 | /** @var array **/ |
||
22 | protected $comparisons = [ |
||
23 | 'array' => ArrayComparison::class, |
||
24 | 'boolean' => BooleanComparison::class, |
||
25 | 'datetime' => DatetimeComparison::class, |
||
26 | 'numeric' => NumericComparison::class, |
||
27 | 'object' => ObjectComparison::class, |
||
28 | 'user' => UserComparison::class, |
||
29 | 'string' => StringComparison::class, |
||
30 | ]; |
||
31 | /** @var array **/ |
||
32 | protected $rejectedAttributes = []; |
||
33 | |||
34 | 10 | public function __construct(AttributeManager $manager) |
|
38 | |||
39 | /** |
||
40 | * This method retrieve the comparison class, instanciate it, |
||
41 | * and then perform the configured comparison |
||
42 | * It does return a control value for special operations, |
||
43 | * but the real check is at the end of the enforce() method, |
||
44 | * when the rejected attributes are counted. |
||
45 | * |
||
46 | * If the second parameter is set to true, compare will not report errors. |
||
47 | * This is used to test a bunch of comparisons expecting not all of them true to return a granted access. |
||
48 | * In fact, this parameter is used in comparisons which need to perform comparisons on their own. |
||
49 | */ |
||
50 | 8 | public function compare(PolicyRuleAttribute $pra, bool $subComparing = false): bool |
|
83 | |||
84 | 2 | public function setDynamicAttributes(array $dynamicAttributes) |
|
88 | |||
89 | /** |
||
90 | * A dynamic attribute is a value given by the user code as an option |
||
91 | * If a policy rule attribute is dynamic, |
||
92 | * we check that the developer has given a dynamic value in the options |
||
93 | * |
||
94 | * Dynamic attributes are given with slugs as key |
||
95 | * |
||
96 | * @param string $attributeSlug |
||
97 | * @return mixed |
||
98 | * @throws \InvalidArgumentException |
||
99 | */ |
||
100 | 3 | public function getDynamicAttribute(string $attributeSlug) |
|
107 | |||
108 | public function addComparison(string $type, string $class) |
||
112 | |||
113 | 3 | public function getAttributeManager(): AttributeManager |
|
117 | |||
118 | /** |
||
119 | * This method is called when all the policy rule attributes are checked |
||
120 | * All along the comparisons, the failing attributes slugs are stored |
||
121 | * If the rejected attributes array is not empty, it means that the rule is not enforced |
||
122 | */ |
||
123 | 6 | public function getResult(): array |
|
133 | } |
||
134 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: