1 | <?php |
||
22 | class ContextChecker |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * A unique string used to identify where this ContextChecker is being employed |
||
27 | * Is currently only used within the hook name for the filterable return value of isAllowed(). |
||
28 | * |
||
29 | * @var string $identifier |
||
30 | */ |
||
31 | private $identifier; |
||
32 | |||
33 | /** |
||
34 | * A list of values to be compared against the slug of the Context class passed to isAllowed() |
||
35 | * |
||
36 | * @var array $acceptable_values |
||
37 | */ |
||
38 | private $acceptable_values; |
||
39 | |||
40 | /** |
||
41 | * Closure that will be called to perform the evaluation within isAllowed(). |
||
42 | * If none is provided, then a simple type sensitive in_array() check will be used |
||
43 | * and return true if the incoming Context::slug() is found within the array of $acceptable_values. |
||
44 | * |
||
45 | * @var Closure $evaluation_callback |
||
46 | */ |
||
47 | private $evaluation_callback; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * ContextChecker constructor. |
||
52 | * |
||
53 | * @param string $identifier |
||
54 | * @param array $acceptable_values |
||
55 | * @param Closure|null $evaluation_callback [optional] |
||
56 | */ |
||
57 | public function __construct($identifier, array $acceptable_values, Closure $evaluation_callback = null) |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @param string $identifier |
||
67 | */ |
||
68 | private function setIdentifier($identifier) |
||
72 | |||
73 | |||
74 | /** |
||
75 | * @param array $acceptable_values |
||
76 | */ |
||
77 | private function setAcceptableValues(array $acceptable_values) |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @param Closure $evaluation_callback |
||
85 | */ |
||
86 | private function setEvaluationCallback(Closure $evaluation_callback = null) |
||
94 | |||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | protected function identifier() |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | protected function acceptableValues() |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @return Closure |
||
119 | */ |
||
120 | protected function evaluationCallback() |
||
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * Returns true if the incoming Context class slug matches one of the preset acceptable values. |
||
129 | * The result is filterable using the identifier for this ContextChecker. |
||
130 | * example: |
||
131 | * If this ContextChecker's $identifier was set to "registration-checkout-type", |
||
132 | * then the filter here would be named: |
||
133 | * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed". |
||
134 | * Other code could hook into the filter in isAllowed() using the above name |
||
135 | * and test for additional acceptable values. |
||
136 | * So if the set of $acceptable_values was: [ "initial-visit", "revisit" ] |
||
137 | * then adding a filter to |
||
138 | * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed", |
||
139 | * would allow you to perform your own conditional and allow "wait-list-checkout" as an acceptable value. |
||
140 | * example: |
||
141 | * add_filter( |
||
142 | * 'FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed', |
||
143 | * function ($is_allowed, Context $context) { |
||
144 | * return $context->slug() === 'wait-list-checkout' |
||
145 | * ? true |
||
146 | * : $is_allowed; |
||
147 | * }, |
||
148 | * 10, |
||
149 | * 2 |
||
150 | * ); |
||
151 | * |
||
152 | * @param Context $context |
||
153 | * @return boolean |
||
154 | */ |
||
155 | public function isAllowed(Context $context) |
||
168 | |||
169 | } |
||
170 |