|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\core\Table\Rules; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_connect\core\ConnectException; |
|
7
|
|
|
use kalanis\kw_connect\core\Interfaces\IConnector; |
|
8
|
|
|
use kalanis\kw_connect\core\Interfaces\IRow; |
|
9
|
|
|
use kalanis\kw_table\core\Interfaces\Table\IRule; |
|
10
|
|
|
use kalanis\kw_table\core\Table\Internal\Attributes; |
|
11
|
|
|
use kalanis\kw_table\core\TableException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class DataSourceSet |
|
16
|
|
|
* @package kalanis\kw_table\core\Table\Rules |
|
17
|
|
|
* Check item in data source against multiple rules |
|
18
|
|
|
*/ |
|
19
|
|
|
class DataSourceSet implements IRule |
|
20
|
|
|
{ |
|
21
|
|
|
protected IConnector $dataSource; |
|
22
|
|
|
/** @var array<Attributes> */ |
|
23
|
|
|
protected array $rules = []; |
|
24
|
|
|
protected bool $all = true; |
|
25
|
|
|
|
|
26
|
1 |
|
public function setDataSource(IConnector $dataSource): self |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->dataSource = $dataSource; |
|
29
|
1 |
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param IRule $rule |
|
34
|
|
|
* @param string|int $key |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function addRule(IRule $rule, $key): self |
|
38
|
|
|
{ |
|
39
|
1 |
|
$this->rules[] = new Attributes($key, '', $rule); |
|
40
|
1 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param bool $all |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function allMustPass($all = true): self |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->all = (bool) $all; |
|
50
|
1 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check each item |
|
55
|
|
|
* @param string|int $value key to get data object in source |
|
56
|
|
|
* @throws ConnectException |
|
57
|
|
|
* @throws TableException |
|
58
|
|
|
* @return bool |
|
59
|
|
|
* |
|
60
|
|
|
* It is not defined what came from the data source, so for that it has check |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function validate($value = '0'): bool |
|
63
|
|
|
{ |
|
64
|
1 |
|
$trueCount = 0; |
|
65
|
1 |
|
$data = $this->dataSource->getByKey($value); |
|
66
|
|
|
|
|
67
|
1 |
|
foreach ($this->rules as $attr) { |
|
68
|
|
|
/** @var Attributes $attr */ |
|
69
|
1 |
|
if ($attr->getCondition()->validate($this->valueToCheck($data, $attr->getColumnName()))) { |
|
70
|
1 |
|
$trueCount++; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
if ((false === $this->all) && (0 < $trueCount)) { |
|
75
|
1 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (count($this->rules) == $trueCount) { |
|
79
|
1 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $data |
|
87
|
|
|
* @param string|int $key |
|
88
|
|
|
* @throws ConnectException |
|
89
|
|
|
* @return mixed|null |
|
90
|
|
|
*/ |
|
91
|
1 |
|
protected function valueToCheck($data, $key) |
|
92
|
|
|
{ |
|
93
|
1 |
|
return is_object($data) |
|
94
|
1 |
|
? ($data instanceof IRow ? $data->getValue($key) : $data->$key) |
|
95
|
1 |
|
: (is_array($data) ? $data[$key] : null ); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|