|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Simplario\Checker\Checker; |
|
4
|
|
|
|
|
5
|
|
|
use Simplario\Checker\ResultException\ErrorException; |
|
6
|
|
|
use Simplario\Checker\ResultException\FailException; |
|
7
|
|
|
use Simplario\Checker\ResultException\SuccessException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractChecker |
|
11
|
|
|
* |
|
12
|
|
|
* @package Simplario\Checker\Checker |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractChecker |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $ignoreTaskKeys = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $target = 'target'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $task |
|
28
|
|
|
* |
|
29
|
|
|
* @throws ErrorException |
|
30
|
|
|
* @throws SuccessException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function check(array $task) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($task as $test => $options) { |
|
35
|
|
|
|
|
36
|
|
|
if ($test === 'checker' || $test === $this->target || in_array($test, $this->ignoreTaskKeys, true)) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$method = 'test' . ucfirst($test); |
|
41
|
|
|
$this->tryMethod($method, $task, $options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// final success |
|
45
|
|
|
throw new SuccessException("Ok", $task); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $method |
|
50
|
|
|
* @param array $task |
|
51
|
|
|
* @param mixed $options |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
* @throws ErrorException |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function tryMethod($method, array $task, $options) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!method_exists($this, $method)) { |
|
59
|
|
|
throw new ErrorException("Test method not exists '{$method}'", $task); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
// if test success - throw SuccessException |
|
64
|
|
|
// if test fail - throw FailException |
|
65
|
|
|
// if test error - ErrorException |
|
66
|
|
|
call_user_func_array([$this, $method], [$task[$this->target], $options, $task]); |
|
67
|
|
|
} catch (SuccessException $ex) { |
|
68
|
|
|
// - test success |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|