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 |
|
public function __construct() |
15
|
|
|
{ |
16
|
4 |
|
$this->resetHandlers(); |
17
|
4 |
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
4 |
|
public function resetHandlers() |
21
|
|
|
{ |
22
|
4 |
|
$this->references = []; |
23
|
4 |
|
$this->handlers = []; |
24
|
4 |
|
self::$instance = null; |
25
|
4 |
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function __call($method, $arguments = []) |
29
|
|
|
{ |
30
|
|
|
$handlers = new Handler($arguments); |
31
|
|
|
$handler = $handlers->build($method, $arguments); |
32
|
|
|
$this->pushHandler($handler); |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
public function pushHandler($handler) |
37
|
|
|
{ |
38
|
4 |
|
array_unshift($this->handlers, $handler); |
39
|
4 |
|
$this->clearResults(); |
40
|
4 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function toArray() |
47
|
|
|
{ |
48
|
|
|
return $this->process()->toArray(); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function process() |
52
|
|
|
{ |
53
|
4 |
|
foreach ($this->handlers as $handler) { |
54
|
4 |
|
array_unshift($this->references, ['name' => $handler->getName(), 'value' => $handler->getValue()]); |
55
|
4 |
|
$this->processWrappers($handler); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
if (self::$instance === null) { |
59
|
4 |
|
$reflection = new ReflectionClass('devtoolboxuk\\cerberus\\Models\\CerberusModel'); |
60
|
4 |
|
self::$instance = $reflection->newInstance($this->references, $this->score, $this->result); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return self::$instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function hasScore() |
67
|
|
|
{ |
68
|
|
|
return $this->process()->hasScore(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
4 |
|
public function getResult() |
73
|
|
|
{ |
74
|
4 |
|
return $this->process()->getResult(); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
public function getScore() |
78
|
|
|
{ |
79
|
4 |
|
return $this->process()->getScore(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |