Passed
Push — master ( 21d6cf...01e195 )
by Alexey
01:47
created

BaseObject::validate()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 7
nop 0
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace iamsaint\yml;
4
5
use iamsaint\yml\exceptions\IncorrectRuleExceptin;
0 ignored issues
show
Bug introduced by
The type iamsaint\yml\exceptions\IncorrectRuleExceptin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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