Passed
Pull Request — master (#360)
by Valentin
04:12
created

Validator::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Validation;
13
14
final class Validator extends AbstractValidator
15
{
16
    /** @var array|\ArrayAccess */
17
    private $data;
18
19
    /**
20
     * @param array|\ArrayAccess $data
21
     * @param array              $rules
22
     * @param mixed              $context
23
     * @param RulesInterface     $ruleProvider
24
     */
25
    public function __construct($data, array $rules, $context, RulesInterface $ruleProvider)
26
    {
27
        $this->data = $data;
28
        parent::__construct($rules, $context, $ruleProvider);
29
    }
30
31
    /**
32
     * Destruct the service.
33
     */
34
    public function __destruct()
35
    {
36
        $this->data = null;
37
        parent::__destruct();
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function withData($data): ValidatorInterface
44
    {
45
        $validator = clone $this;
46
        $validator->data = $data;
47
48
        return $validator;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getValue(string $field, $default = null)
55
    {
56
        $value = $this->data[$field] ?? $default;
57
58
        if (is_object($value) && method_exists($value, 'getValue')) {
59
            return $value->getValue();
60
        }
61
62
        return $value;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function hasValue(string $field): bool
69
    {
70
        if (is_array($this->data)) {
71
            return array_key_exists($field, $this->data);
72
        }
73
74
        return isset($this->data[$field]);
75
    }
76
}
77