1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace iamsaint\yml; |
4
|
|
|
|
5
|
|
|
use iamsaint\yml\exceptions\IncorrectRuleException; |
6
|
|
|
use iamsaint\yml\interfaces\Base; |
7
|
|
|
use iamsaint\yml\interfaces\Validator; |
8
|
|
|
use XMLWriter; |
9
|
|
|
use function array_key_exists; |
10
|
|
|
use function count; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseObject |
16
|
|
|
* @package iamsaint\yml |
17
|
|
|
* |
18
|
|
|
* @property array $errors |
19
|
|
|
*/ |
20
|
|
|
class BaseObject implements Base |
21
|
|
|
{ |
22
|
|
|
public $errors = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $groupTag |
26
|
|
|
* @param array|BaseObject[] $elements |
27
|
|
|
* @param XMLWriter $writer |
28
|
|
|
*/ |
29
|
|
|
public function writeElements(XMLWriter $writer, string $groupTag, array $elements): void |
30
|
|
|
{ |
31
|
|
|
$writer->startElement($groupTag); |
32
|
|
|
|
33
|
|
|
foreach ($elements as $element) { |
34
|
|
|
if ($element instanceof Base) { |
35
|
|
|
$element->write($writer); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$writer->endElement(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
* @throws IncorrectRuleException |
45
|
|
|
*/ |
46
|
|
|
public function validate(): bool |
47
|
|
|
{ |
48
|
|
|
$this->errors = []; |
49
|
|
|
$rules = $this->rules(); |
50
|
|
|
foreach ($rules as $rule) { |
51
|
|
|
if (!is_array($rule)) { |
52
|
|
|
throw new IncorrectRuleException('Rule must be array'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (count($rule) < 2) { |
56
|
|
|
throw new IncorrectRuleException('Rule is not defined'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!is_string($rule[1])) { |
60
|
|
|
throw new IncorrectRuleException('Rule name must be a string'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$class = '\\iamsaint\\yml\\validators\\' . $rule[1]; |
64
|
|
|
|
65
|
|
|
if (!class_exists($class)) { |
66
|
|
|
throw new IncorrectRuleException('Validator not found'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; |
70
|
|
|
|
71
|
|
|
$validator = new $class(); |
72
|
|
|
|
73
|
|
|
if ($validator instanceof Validator) { |
74
|
|
|
$validator->validate($this, $attributes, $rule[2] ?: []); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return count($this->errors) === 0; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function rules(): array |
85
|
|
|
{ |
86
|
|
|
return []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $attribute |
91
|
|
|
* @param string $text |
92
|
|
|
*/ |
93
|
|
|
public function addError($attribute, $text): void |
94
|
|
|
{ |
95
|
|
|
if (!array_key_exists($attribute, $this->errors)) { |
96
|
|
|
$this->errors[$attribute] = []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->errors[$attribute][] = $text; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param XMLWriter $writer |
104
|
|
|
*/ |
105
|
|
|
public function write($writer): void |
106
|
|
|
{ |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|