• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

narokishi / object-validator / 234

pending completion
234

cron

travis-ci-com

narokishi
Declare strict types

98 of 101 relevant lines covered (97.03%)

14.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/AbstractObjectValidator.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Narokishi\ObjectValidator;
5

6
use Narokishi\ObjectValidator\Rules\Collection;
7

8
/**
9
 * Class AbstractObjectValidator
10
 * @package Aggregate\GenericView\RequestValidator
11
 */
12
abstract class AbstractObjectValidator
13
{
14
    /**
15
     * @var \stdClass
16
     */
17
    protected $object;
18

19
    /**
20
     * @var string[]
21
     */
22
    protected $errors;
23

24
    /**
25
     * @return array
26
     */
27
    abstract public function getRules(): array;
28

29
    /**
30
     * AbstractRequestValidator constructor.
31
     *
32
     * @param \stdClass $request
33
     */
34
    final public function __construct(\stdClass $object)
35
    {
36
        $this->object = $object;
14✔
37
        $this->validate();
14✔
38
    }
14✔
39

40
    final public function validate(): void
41
    {
42
        $errors = [];
14✔
43

44
        /**
45
         * @var string $property
46
         * @var string|string[]|array $rules
47
         */
48
        foreach ($this->getRules() as $property => $rules) {
14✔
49
            $value = isset($this->object->$property) ? $this->object->$property : null;
14✔
50
            $rules = is_array($rules) ? $rules : [$rules];
14✔
51

52
            if (array_key_exists(RuleConst::RULE_COLLECTION, $rules)) {
14✔
53
                if (is_array($value)) {
14✔
54
                    /**
55
                     * @var integer|string $key
56
                     * @var \stdClass $item
57
                     */
58
                    foreach ($value as $key => $item) {
2✔
59
                        /**
60
                         * @var string $collectionProperty
61
                         * @var string|string[] $collectionRules
62
                         */
63
                        foreach ($rules[RuleConst::RULE_COLLECTION] as $collectionProperty => $collectionRules) {
2✔
64
                            $collectionValue = isset($value[$key]->$collectionProperty)
2✔
65
                                ? $value[$key]->$collectionProperty : null;
2✔
66

67
                            if ($validationErrors = $this->validateRules(
2✔
68
                                $collectionValue,
2✔
69
                                $collectionRules
2✔
70
                            )) {
71
                                $errors["$property.$key.$collectionProperty"] = $validationErrors;
1✔
72
                            }
73
                        }
74
                    }
75
                } else {
76
                    $errors[$property] = [Collection::DEFAULT_ERROR_MESSAGE];
14✔
77
                }
78
            } elseif ($validationErrors = $this->validateRules($value, $rules)) {
14✔
79
                $errors[$property] = $validationErrors;
13✔
80
            }
81
        }
82

83
        $this->errors = $errors;
14✔
84
    }
14✔
85

86
    /**
87
     * @param mixed $value
88
     * @param array $rules
89
     * @return array
90
     * @throws \LogicException
91
     */
92
    final private function validateRules($value, array $rules): array
93
    {
94
        $errors = [];
14✔
95

96
        /** @var string $rule */
97
        foreach ($rules as $rule) {
14✔
98
            /** @var AbstractRule $rule */
99
            $rule = new $rule($value);
14✔
100

101
            if (!$rule instanceof AbstractRule) {
14✔
102
                throw new \LogicException(
×
103
                    "$rule is not defined"
×
104
                );
105
            }
106

107
            if (!$rule->isValid()) {
14✔
108
                $errors[] = $rule->getError();
13✔
109
            }
110
        }
111

112
        return $errors;
14✔
113
    }
114

115
    /**
116
     * @return string[]
117
     */
118
    final public function getErrors(): array
119
    {
120
        return $this->errors;
10✔
121
    }
122

123
    /**
124
     * @return bool
125
     */
126
    final public function isNotValid(): bool
127
    {
128
        return !empty($this->errors);
8✔
129
    }
130

131
    /**
132
     * @example $prefix = 'compensation', output: 'compensation.payer', 'compensation.amount'
133
     *
134
     * @param string $prefix
135
     * @return $this
136
     */
137
    final public function applyPrefix(string $prefix): self
138
    {
139
        if ($this->isNotValid()) {
4✔
140
            $errors = [];
4✔
141

142
            foreach ($this->errors as $property => $error) {
4✔
143
                $errors["$prefix.$property"] = $error;
4✔
144
            }
145

146
            $this->errors = $errors;
4✔
147
        }
148

149
        return $this;
4✔
150
    }
151

152
    /**
153
     * @throws ValidationException
154
     */
155
    final public function withThrow(): void
156
    {
157
        if ($this->isNotValid()) {
4✔
158
            throw new ValidationException('', $this->errors);
4✔
159
        }
160
    }
×
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc