InputValidator::setMessages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
namespace AdvancedLearning\InputValidator;
4
5
use AdvancedLearning\InputValidator\Interfaces\MappableModel;
6
use const PHP_EOL;
7
use Respect\Validation\Exceptions\NestedValidationException;
8
use Respect\Validation\Exceptions\ValidationException;
9
use AdvancedLearning\InputValidator\Exceptions\InputValidationException;
10
11
/**
12
 * An interface for creating a validator for a specific model.
13
 */
14
abstract class InputValidator
15
{
16
    /**
17
     * Customise the messages returned.
18
     *
19
     * @var array
20
     */
21
    protected $messages = [
22
    ];
23
24
    /**
25
     * Returns array of rules of the format field => rule.
26
     *
27
     * @return array
28
     */
29
    abstract public function getRules();
30
31
    /**
32
     * Validate an array of data.
33
     *
34
     * @param array $data The array of data to validate.
35
     * @return boolean
36
     * @throws InputValidationException If validation fails.
37
     */
38
    public function valid(array $data)
39
    {
40
        $rules = $this->getRules();
41
        $messages = [];
42
43
        foreach ($rules as $field => $rule) {
44
            try {
45
                $rule->assert(isset($data[$field]) ? $data[$field] : null);
46
            } catch (NestedValidationException $e) {
47
                // get individual error messages
48
                foreach ($e as $validationException) {
49
                    $this->formatMessage($validationException);
50
                    $messages[$field][] = $validationException->getMainMessage();
51
                }
52
            }
53
        }
54
55
        if (!empty($messages)) {
56
            throw new InputValidationException($messages);
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Validate an instance of a model.
64
     *
65
     * @param MappableModel $model The model to validate.
66
     * @return mixed
67
     */
68
    public function validateModel(MappableModel $model)
69
    {
70
        return $this->valid($model->toMap());
71
    }
72
73
    /**
74
     * Set custom templates for messages.
75
     *
76
     * @param array $messages
77
     *
78
     * @return $this
79
     */
80
    public function setMessages(array $messages)
81
    {
82
        $this->messages = $messages;
83
        return $this;
84
    }
85
86
    /**
87
     * Sets the message template on the exception if it has been configured.
88
     *
89
     * @param ValidationException $e The exception to add message to.
90
     *
91
     * @return static
92
     */
93
    protected function formatMessage(ValidationException $e)
94
    {
95
        $type = $e->getId();
96
97
        if (!empty($this->messages[$type])) {
98
            $e->setTemplate($this->messages[$type]);
99
        }
100
101
        return $this;
102
    }
103
}
104