94% of 280OPs |
95% of 40Lines |
84% of 32Branches |
39% of 38Paths |
# | |
---|---|
1 |
<?php |
2 | |
3 |
namespace mageekguy\atoum\test\assertion; |
4 | |
5 |
use |
6 |
mageekguy\atoum\test\assertion |
7 |
; |
8 | |
9 |
class manager |
10 |
{ |
11 |
protected $aliaser = null; |
12 |
protected $propertyHandlers = array(); |
13 |
protected $methodHandlers = array(); |
14 |
protected $defaultHandler = null; |
15 | |
16 |
public function __construct(assertion\aliaser $aliaser = null)100% |
17 |
{ |
18 |
$this->setAliaser($aliaser); |
19 |
} |
20 | |
21 |
public function __set($event, $handler)100% |
22 |
{ |
23 |
return $this->setHandler($event, $handler); |
24 |
} |
25 | |
26 |
public function __get($event)100% |
27 |
{ |
28 |
return $this->invokePropertyHandler($event); |
29 |
} |
30 | |
31 |
public function __call($event, array $arguments)100% |
32 |
{ |
33 |
return $this->invokeMethodHandler($event, $arguments); |
34 |
} |
35 | |
36 |
public function setAliaser(assertion\aliaser $aliaser = null)100% |
37 |
{ |
38 |
$this->aliaser = $aliaser ?: new assertion\aliaser(); |
39 | |
40 |
return $this; |
41 |
} |
42 | |
43 |
public function getAliaser()100% |
44 |
{ |
45 |
return $this->aliaser; |
46 |
} |
47 | |
48 |
public function setAlias($alias, $keyword)0% |
49 |
{ |
50 |
$this->aliaser->aliasKeyword($keyword, $alias); |
51 | |
52 |
return $this; |
53 |
} |
54 | |
55 |
public function setMethodHandler($event, \closure $handler)100% |
56 |
{ |
57 |
return $this->setHandlerIn($this->methodHandlers, $event, $handler); |
58 |
} |
59 | |
60 |
public function setPropertyHandler($event, \closure $handler)100% |
61 |
{ |
62 |
return $this->setHandlerIn($this->propertyHandlers, $event, $handler); |
63 |
} |
64 | |
65 |
public function setHandler($event, \closure $handler)100% |
66 |
{ |
67 |
return $this |
68 |
->setPropertyHandler($event, $handler) |
69 |
->setMethodHandler($event, $handler) |
70 |
; |
71 |
} |
72 | |
73 |
public function setDefaultHandler(\closure $handler)100% |
74 |
{ |
75 |
$this->defaultHandler = $handler; |
76 | |
77 |
return $this; |
78 |
} |
79 | |
80 |
public function invokePropertyHandler($event)100% |
81 |
{ |
82 |
return $this->invokeHandlerFrom($this->propertyHandlers, $event); |
83 |
} |
84 | |
85 |
public function invokeMethodHandler($event, array $arguments = array())100% |
86 |
{ |
87 |
return $this->invokeHandlerFrom($this->methodHandlers, $event, $arguments); |
88 |
} |
89 | |
90 |
private function setHandlerIn(array & $handlers, $event, \closure $handler)100% |
91 |
{ |
92 |
$handlers[strtolower($event)] = $handler; |
93 | |
94 |
return $this; |
95 |
} |
96 | |
97 |
private function invokeHandlerFrom(array $handlers, $event, array $arguments = array())100% |
98 |
{ |
99 |
$handler = null; |
100 | |
101 |
$realEvent = strtolower($event); |
102 | |
103 |
if (isset($handlers[$realEvent]) === false) |
104 |
{ |
105 |
$realEvent = $this->aliaser->resolveAlias($event); |
106 |
} |
107 | |
108 |
if (isset($handlers[$realEvent]) === true) |
109 |
{ |
110 |
$handler = $handlers[$realEvent]; |
111 |
} |
112 | |
113 |
switch (true) |
114 |
{ |
115 |
case $handler === null && $this->defaultHandler === null: |
116 |
throw new assertion\manager\exception('There is no handler defined for \'' . $event . '\''); |
117 | |
118 |
case $handler !== null: |
119 |
return call_user_func_array($handler, $arguments); |
120 | |
121 |
default: |
122 |
return call_user_func_array($this->defaultHandler, array($realEvent, $arguments)); |
123 |
} |
124 |
} |
125 |
} |