1 | <?php |
||
16 | class Experiment |
||
17 | { |
||
18 | /** |
||
19 | * Experiment name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $name; |
||
24 | |||
25 | /** |
||
26 | * The control callback. |
||
27 | * |
||
28 | * @var callable |
||
29 | */ |
||
30 | protected $control; |
||
31 | |||
32 | /** |
||
33 | * Trial callbacks. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $trials = []; |
||
38 | |||
39 | /** |
||
40 | * Parameters for our callbacks. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $params = []; |
||
45 | |||
46 | /** |
||
47 | * Laboratory instance. |
||
48 | * |
||
49 | * @var \Scientist\Laboratory|null |
||
50 | */ |
||
51 | protected $laboratory; |
||
52 | |||
53 | /** |
||
54 | * Matcher for experiment values. |
||
55 | * |
||
56 | * @var \Scientist\Matchers\Matcher |
||
57 | */ |
||
58 | protected $matcher; |
||
59 | |||
60 | /** |
||
61 | * Execution chance. |
||
62 | * |
||
63 | * @var integer |
||
64 | */ |
||
65 | protected $chance = 100; |
||
66 | |||
67 | /** |
||
68 | * Create a new experiment. |
||
69 | * |
||
70 | * @param string $name |
||
71 | * @param \Scientist\Laboratory|null $laboratory |
||
72 | */ |
||
73 | 14 | public function __construct($name, $laboratory = null) |
|
79 | |||
80 | /** |
||
81 | * Fetch the experiment name. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 5 | public function getName() |
|
89 | |||
90 | /** |
||
91 | * Set the laboratory instance. |
||
92 | * |
||
93 | * @param \Scientist\Laboratory $laboratory |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | 6 | public function setLaboratory(Laboratory $laboratory) |
|
103 | |||
104 | /** |
||
105 | * Retrieve the laboratory instance. |
||
106 | * |
||
107 | * @return \Scientist\Laboratory|null |
||
108 | */ |
||
109 | 1 | public function getLaboratory() |
|
110 | { |
||
111 | 1 | return $this->laboratory; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Register a control callback. |
||
116 | * |
||
117 | * @param callable $callback |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | 7 | public function control(callable $callback) |
|
127 | |||
128 | /** |
||
129 | * Fetch the control callback. |
||
130 | * |
||
131 | * @return callable |
||
132 | */ |
||
133 | 6 | public function getControl() |
|
137 | |||
138 | /** |
||
139 | * Register a trial callback. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * @param callable $callback |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | 6 | public function trial($name, callable $callback) |
|
152 | |||
153 | /** |
||
154 | * Fetch a trial callback by name. |
||
155 | * |
||
156 | * @param string $name |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 2 | public function getTrial($name) |
|
164 | |||
165 | /** |
||
166 | * Fetch an array of trial callbacks. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | 5 | public function getTrials() |
|
174 | |||
175 | /** |
||
176 | * Set a matcher for this experiment. |
||
177 | * |
||
178 | * @param \Scientist\Matchers\Matcher $matcher |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | 1 | public function matcher(Matcher $matcher) |
|
188 | |||
189 | /** |
||
190 | * Get the matcher for this experiment. |
||
191 | * |
||
192 | * @return \Scientist\Matchers\Matcher |
||
193 | */ |
||
194 | 5 | public function getMatcher() |
|
198 | |||
199 | /** |
||
200 | * Set the execution chance. |
||
201 | * |
||
202 | * @param integer $chance |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | 2 | public function chance($chance) |
|
212 | |||
213 | /** |
||
214 | * Get the execution chance. |
||
215 | * |
||
216 | * @return integer |
||
217 | */ |
||
218 | 1 | public function getChance() |
|
222 | |||
223 | /** |
||
224 | * Determine whether an experiment should run based on chance. |
||
225 | * |
||
226 | * @return boolean |
||
227 | */ |
||
228 | 4 | public function shouldRun() |
|
232 | |||
233 | /** |
||
234 | * Get the experiment parameters. |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | 5 | public function getParams() |
|
242 | |||
243 | /** |
||
244 | * Execute the experiment within the laboratory. |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | 5 | public function run() |
|
249 | { |
||
250 | 5 | $this->params = func_get_args(); |
|
251 | |||
252 | 5 | if ($this->laboratory) { |
|
253 | 4 | return $this->laboratory->runExperiment($this); |
|
254 | } |
||
255 | |||
256 | 1 | return call_user_func($this->control, $this->params); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Execute the experiment and return a result. |
||
261 | * |
||
262 | * @return \Scientist\Report |
||
263 | */ |
||
264 | 1 | public function result() |
|
270 | } |
||
271 |