1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ThallesDella\GateKeeper\GateKeeper; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Gate Keeper | Class Actions [ GATE KEEPER ] |
9
|
|
|
* |
10
|
|
|
* @category GateKeeper |
11
|
|
|
* @package ThallesDella\GateKeeper |
12
|
|
|
* @author Thalles D. koester <[email protected]> |
13
|
|
|
* @license https://choosealicense.com/licenses/mit/ MIT |
14
|
|
|
* @link https://github.com/thallesdella/gate-keeper |
15
|
|
|
*/ |
16
|
|
|
class Actions |
17
|
|
|
{ |
18
|
|
|
/** @const int Success Action */ |
19
|
|
|
public const SUCCESS_ACTION = 0; |
20
|
|
|
|
21
|
|
|
/** @const int Failure Action */ |
22
|
|
|
public const FAILURE_ACTION = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var stdClass |
26
|
|
|
*/ |
27
|
|
|
private $success; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var stdClass |
31
|
|
|
*/ |
32
|
|
|
private $failure; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
private $return; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Actions constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$buf = new stdClass(); |
45
|
|
|
$buf->action = null; |
46
|
|
|
$buf->action_type = null; |
47
|
|
|
|
48
|
|
|
$this->failure = $buf; |
49
|
|
|
$this->success = $buf; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $func |
54
|
|
|
* @param int $type |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function registerFunction(string $func, int $type): void |
59
|
|
|
{ |
60
|
|
|
if ($type == self::SUCCESS_ACTION) { |
61
|
|
|
$this->success->action = $func; |
62
|
|
|
$this->success->action_type = 'func'; |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$this->failure->action = $func; |
66
|
|
|
$this->failure->action_type = 'func'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $class |
71
|
|
|
* @param string $method |
72
|
|
|
* @param int $type |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function registerClass($class, string $method, int $type): void |
77
|
|
|
{ |
78
|
|
|
$arr = [$class, $method]; |
79
|
|
|
if ($type == self::SUCCESS_ACTION) { |
80
|
|
|
$this->success->action = $arr; |
81
|
|
|
$this->success->action_type = 'class'; |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
$this->failure->action = $arr; |
85
|
|
|
$this->failure->action_type = 'class'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function callSuccess(): bool |
92
|
|
|
{ |
93
|
|
|
$this->return = call_user_func($this->success->action); |
94
|
|
|
return $this->verifyResult(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function callFailure(): bool |
101
|
|
|
{ |
102
|
|
|
$this->return = call_user_func($this->failure->action); |
103
|
|
|
return $this->verifyResult(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
private function verifyResult(): bool |
110
|
|
|
{ |
111
|
|
|
return (!$this->return ? false : true); |
112
|
|
|
} |
113
|
|
|
} |