Passed
Push — master ( 3a6720...972705 )
by Alexey
01:52
created

BaseObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 XMLWriter $writer
19
 * @property array $errors
20
 */
21
class BaseObject implements Base
22
{
23
    public $writer;
24
25
    public $errors = [];
26
27
    /**
28
     * BaseObject constructor.
29
     * @param XMLWriter|null $writer
30
     */
31
    public function __construct(XMLWriter $writer = null)
32
    {
33
        $writer = $writer;
0 ignored issues
show
Unused Code introduced by
The assignment to $writer is dead and can be removed.
Loading history...
34
    }
35
36
    /**
37
     * @param string $groupTag
38
     * @param array|BaseObject[] $elements
39
     * @param XMLWriter $writer
40
     */
41
    public function writeElements(XMLWriter $writer, string $groupTag, array $elements): void
42
    {
43
        $writer->startElement($groupTag);
44
45
        foreach ($elements as $element) {
46
            if ($element instanceof Base) {
47
                $element->write($writer);
48
            }
49
        }
50
51
        $writer->endElement();
52
    }
53
54
    /**
55
     * @return bool
56
     * @throws IncorrectRuleException
57
     */
58
    public function validate(): bool
59
    {
60
        $this->errors = [];
61
        $rules = $this->rules();
62
        foreach ($rules as $rule) {
63
            if (!is_array($rule)) {
64
                throw new IncorrectRuleException('Rule must be array');
65
            }
66
67
            if (count($rule) < 2) {
68
                throw new IncorrectRuleException('Rule is not defined');
69
            }
70
71
            if (!is_string($rule[1])) {
72
                throw new IncorrectRuleException('Rule name must be a string');
73
            }
74
75
            $class = '\\iamsaint\\yml\\validators\\' . $rule[1];
76
77
            if (!class_exists($class)) {
78
                throw new IncorrectRuleException('Validator not found');
79
            }
80
81
            $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
82
83
            $validator = new $class();
84
85
            if ($validator instanceof Validator) {
86
                $validator->validate($this, $attributes, $rule[2] ?: []);
87
            }
88
        }
89
90
        return count($this->errors) === 0;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function rules(): array
97
    {
98
        return [];
99
    }
100
101
    /**
102
     * @param string $attribute
103
     * @param string $text
104
     */
105
    public function addError($attribute, $text): void
106
    {
107
        if (!array_key_exists($attribute, $this->errors)) {
108
            $this->errors[$attribute] = [];
109
        }
110
111
        $this->errors[$attribute][] = $text;
112
    }
113
114
    /**
115
     * @param XMLWriter $writer
116
     */
117
    public function write($writer): void
118
    {
119
    }
120
}
121