-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractVoter.php
More file actions
69 lines (62 loc) · 1.76 KB
/
Copy pathAbstractVoter.php
File metadata and controls
69 lines (62 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace DesignPatterns\Behavioral\TemplateMethod;
/**
* Abstract class that defines a common algorithm of voting.
* To determine whether the attribute of a particular object is accessible call @see AbstractVoter::vote() method.
*
* It corresponds to `AbstractClass` in the Strategy pattern.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
abstract class AbstractVoter
{
const ACCESS_GRANTED = "ACCESS_GRANTED";
const ACCESS_DENIED = "ACCESS_DENIED";
const ABSTAINED = "ABSTAINED";
/**
* Check if the voting on this object is supported.
*
* @param object $object
*
* @return bool
*/
protected abstract function supportsObject($object);
/**
* Check if the voting on this attribute is supported.
*
* @param string $attribute
*
* @return bool
*/
protected abstract function supportsAttribute($attribute);
/**
* Decide whether the provided attribute of the provided object is accessible.
*
* @param $object
* @param $attribute
*
* @return bool
*/
protected abstract function hasAccess($object, $attribute);
/**
* Common algorithm of voting:
* - if the object and attribute are supported then check access,
* - abstain otherwise.
*
* This is a template method in terms of the Template method pattern.
*
* @param object $object
* @param string $attribute
*
* @return string
*/
public function vote($object, $attribute)
{
if ($this->supportsObject($object) && $this->supportsAttribute($attribute)) {
return $this->hasAccess($object, $attribute)
? self::ACCESS_GRANTED
: self::ACCESS_DENIED;
}
return self::ABSTAINED;
}
}