1 | <?php |
||
18 | class Experiment |
||
19 | { |
||
20 | /** |
||
21 | * Experiment name. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * The control callback. |
||
29 | * |
||
30 | * @var callable |
||
31 | */ |
||
32 | protected $control; |
||
33 | |||
34 | /** |
||
35 | * Context for the control. |
||
36 | * |
||
37 | * @var mixed |
||
38 | */ |
||
39 | protected $controlContext; |
||
40 | |||
41 | /** |
||
42 | * Trial callbacks. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $trials = []; |
||
47 | |||
48 | /** |
||
49 | * Parameters for our callbacks. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $params = []; |
||
54 | |||
55 | /** |
||
56 | * Laboratory instance. |
||
57 | * |
||
58 | * @var \Scientist\Laboratory |
||
59 | */ |
||
60 | protected $laboratory; |
||
61 | |||
62 | /** |
||
63 | * Matcher for experiment values. |
||
64 | * |
||
65 | * @var \Scientist\Matchers\Matcher |
||
66 | */ |
||
67 | protected $matcher; |
||
68 | |||
69 | /** |
||
70 | * Execution chance. |
||
71 | * |
||
72 | * @var \Scientist\Chances\Chance |
||
73 | 21 | */ |
|
74 | protected $chance; |
||
75 | 21 | ||
76 | 21 | /** |
|
77 | 21 | * Create a new experiment. |
|
78 | 21 | * |
|
79 | * @param string $name |
||
80 | * @param \Scientist\Laboratory $laboratory |
||
81 | */ |
||
82 | public function __construct($name, Laboratory $laboratory) |
||
89 | |||
90 | /** |
||
91 | * Fetch the experiment name. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 1 | public function getName() |
|
99 | |||
100 | /** |
||
101 | * Retrieve the laboratory instance. |
||
102 | * |
||
103 | * @return \Scientist\Laboratory|null |
||
104 | */ |
||
105 | public function getLaboratory() |
||
109 | 14 | ||
110 | /** |
||
111 | 14 | * Register a control callback. |
|
112 | * |
||
113 | * @param callable $callback |
||
114 | * @param mixed $context |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function control(callable $callback, $context = null) |
||
125 | |||
126 | /** |
||
127 | * Fetch the control callback. |
||
128 | * |
||
129 | * @return callable |
||
130 | */ |
||
131 | public function getControl() |
||
135 | |||
136 | 12 | public function getControlContext() |
|
137 | { |
||
138 | return $this->controlContext; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Register a trial callback. |
||
143 | * |
||
144 | * @param string $name |
||
145 | * @param callable $callback |
||
146 | 2 | * |
|
147 | * @return $this |
||
148 | 2 | */ |
|
149 | public function trial($name, callable $callback, $context = null) |
||
155 | |||
156 | 12 | /** |
|
157 | * Fetch a trial callback by name. |
||
158 | 12 | * |
|
159 | * @param string $name |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function getTrial($name) |
||
167 | |||
168 | 1 | /** |
|
169 | * Fetch an array of trial callbacks. |
||
170 | 1 | * |
|
171 | * @return array |
||
172 | 1 | */ |
|
173 | public function getTrials() |
||
177 | |||
178 | /** |
||
179 | * Set a matcher for this experiment. |
||
180 | 12 | * |
|
181 | * @param \Scientist\Matchers\Matcher $matcher |
||
182 | 12 | * |
|
183 | * @return $this |
||
184 | */ |
||
185 | public function matcher(Matcher $matcher) |
||
191 | |||
192 | 2 | /** |
|
193 | * Get the matcher for this experiment. |
||
194 | 2 | * |
|
195 | * @return \Scientist\Matchers\Matcher |
||
196 | 2 | */ |
|
197 | public function getMatcher() |
||
201 | |||
202 | /** |
||
203 | * Set the execution chance. |
||
204 | 1 | * |
|
205 | * @param Chances\Chance $chance |
||
206 | 1 | * |
|
207 | * @return $this |
||
208 | */ |
||
209 | public function chance(Chance $chance) |
||
215 | |||
216 | 6 | /** |
|
217 | * Get the execution chance. |
||
218 | * |
||
219 | * @return Chances\Chance |
||
220 | */ |
||
221 | public function getChance() |
||
225 | |||
226 | 13 | /** |
|
227 | * Determine whether an experiment should run based on chance. |
||
228 | * |
||
229 | * @return boolean |
||
230 | */ |
||
231 | public function shouldRun() |
||
236 | 6 | ||
237 | /** |
||
238 | 6 | * Get the experiment parameters. |
|
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getParams() |
||
246 | 3 | ||
247 | /** |
||
248 | 3 | * Execute the experiment within the laboratory. |
|
249 | * |
||
250 | 3 | * @return mixed |
|
251 | */ |
||
252 | public function run() |
||
258 | |||
259 | /** |
||
260 | * Execute the experiment and return a report. |
||
261 | * |
||
262 | * @return \Scientist\Report |
||
263 | */ |
||
264 | public function report() |
||
270 | } |
||
271 |