1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus; |
4
|
|
|
|
5
|
|
|
use devtoolboxuk\cerberus\handlers\Handler; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class CerberusService extends AbstractCerberus implements CerberusInterface |
9
|
|
|
{ |
10
|
|
|
private static $instance = null; |
11
|
|
|
protected $handlers = []; |
12
|
|
|
protected $references = []; |
13
|
|
|
|
14
|
4 |
|
function __construct() |
|
|
|
|
15
|
|
|
{ |
16
|
4 |
|
$this->resetHandlers(); |
17
|
4 |
|
} |
18
|
|
|
|
19
|
4 |
|
public function resetHandlers() |
20
|
|
|
{ |
21
|
4 |
|
$this->references = []; |
22
|
4 |
|
$this->handlers = []; |
23
|
4 |
|
self::$instance = null; |
24
|
4 |
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __call($method, $arguments = []) |
28
|
|
|
{ |
29
|
|
|
$handlers = new Handler($arguments); |
30
|
|
|
$handler = $handlers->build($method, $arguments); |
31
|
|
|
$this->pushHandler($handler); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public function pushHandler($handler) |
36
|
|
|
{ |
37
|
4 |
|
array_unshift($this->handlers, $handler); |
38
|
4 |
|
$this->clearResults(); |
39
|
4 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function toArray() |
46
|
|
|
{ |
47
|
|
|
return $this->process()->toArray(); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function process() |
51
|
|
|
{ |
52
|
4 |
|
foreach ($this->handlers as $handler) { |
53
|
4 |
|
array_unshift($this->references, ['name' => $handler->getName(), 'value' => $handler->getValue()]); |
54
|
4 |
|
$this->processWrappers($handler); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
if (self::$instance === null) { |
58
|
4 |
|
$reflection = new ReflectionClass('devtoolboxuk\\cerberus\\Models\\CerberusModel'); |
59
|
4 |
|
self::$instance = $reflection->newInstance($this->references, $this->score, $this->result); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return self::$instance; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function hasScore() |
66
|
|
|
{ |
67
|
|
|
return $this->process()->hasScore(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
4 |
|
public function getResult() |
72
|
|
|
{ |
73
|
4 |
|
return $this->process()->getResult(); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function getScore() |
77
|
|
|
{ |
78
|
4 |
|
return $this->process()->getScore(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.