Completed
Push — master ( a905e5...18f374 )
by Alexey
03:04 queued 11s
created

BaseObject   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 99
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 3 1
A writeElements() 0 11 3
B validate() 0 33 9
A addError() 0 7 2
A write() 0 2 1
A writeTag() 0 4 2
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
    /**
110
     * @param string $tagName
111
     * @param mixed $value
112
     * @param XMLWriter $writer
113
     * @param null|bool $notWriteCondition
114
     */
115
    public function writeTag($tagName, $value, $writer, $notWriteCondition = null): void
116
    {
117
        if ($notWriteCondition !== $value) {
118
            $writer->writeElement($tagName, $value);
119
        }
120
    }
121
}
122