1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Scientist\Blind; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Scientist\Experiment; |
8
|
|
|
use Scientist\Study; |
9
|
|
|
|
10
|
|
|
trait DecoratorTrait |
11
|
|
|
{ |
12
|
|
|
/** @var Study */ |
13
|
|
|
private $study; |
14
|
|
|
/** @var Experiment[] */ |
15
|
|
|
private $experiments = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $method |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
4 |
|
private function getExperimentNameForMethod($method) |
22
|
|
|
{ |
23
|
4 |
|
return sprintf("%s::%s", $this->study->getName(), $method); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $attribute |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
6 |
|
private function getExperimentNameForAttribute($attribute) |
31
|
|
|
{ |
32
|
6 |
|
return sprintf("%s::\$%s", $this->study->getName(), $attribute); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* @return Experiment |
38
|
|
|
*/ |
39
|
8 |
|
private function getExperiment($name) { |
40
|
8 |
|
return $this->experiments[$name]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* DecoratorTrait constructor. |
45
|
|
|
* @param Study $study |
46
|
|
|
* @param Experiment[] $experiments |
47
|
|
|
*/ |
48
|
14 |
|
function __construct(Study $study, array $experiments = []) |
|
|
|
|
49
|
|
|
{ |
50
|
14 |
|
$this->study = $study; |
51
|
14 |
|
foreach($experiments as $experiment) { |
52
|
14 |
|
$this->experiments[$experiment->getName()] = $experiment; |
53
|
14 |
|
} |
54
|
14 |
|
} |
55
|
|
|
|
56
|
4 |
|
function __call($name, $arguments) |
|
|
|
|
57
|
|
|
{ |
58
|
4 |
|
$experiment = $this->getExperiment($this->getExperimentNameForMethod($name)); |
59
|
4 |
|
return call_user_func_array([$experiment, 'run'], $arguments); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
function __get($name) |
|
|
|
|
63
|
|
|
{ |
64
|
3 |
|
$experiment = $this->getExperiment($this->getExperimentNameForAttribute($name)); |
65
|
3 |
|
return call_user_func_array([$experiment, 'run'], [$name]); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
function __set($name, $value) |
|
|
|
|
69
|
|
|
{ |
70
|
4 |
|
$experiment = $this->getExperiment($this->getExperimentNameForAttribute($name)); |
71
|
4 |
|
return call_user_func_array([$experiment, 'run'], [$name, $value]); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
function __isset($name) |
|
|
|
|
75
|
|
|
{ |
76
|
1 |
|
$value = $this->__get($name); |
77
|
1 |
|
return $value !== null; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
function __unset($name) |
|
|
|
|
81
|
|
|
{ |
82
|
1 |
|
$this->__set($name, null); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
function __toString() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$experiment = $this->getExperiment($this->getExperimentNameForMethod('__toString')); |
88
|
|
|
return call_user_func([$experiment, 'run']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function __invoke() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$arguments = func_get_args(); |
94
|
|
|
$experiment = $this->getExperiment($this->getExperimentNameForMethod('__invoke')); |
95
|
|
|
return call_user_func_array([$experiment, 'run'], $arguments); |
96
|
|
|
} |
97
|
|
|
} |
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.