ValidationResultProcessor::chooseErrorMessage()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 25
c 3
b 0
f 1
rs 8.8571
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
/**
4
 * @author: Abdul Qureshi. <[email protected]>
5
 * 
6
 * This file has been modified from the original source.
7
 * See original here:
8
 *
9
 * @link: https://github.com/progsmile/request-validator
10
 */
11
namespace TheSupportGroup\Common\Validator\Helpers;
12
13
use TheSupportGroup\Common\ValidationInterop\ValidationProviderInterface;
14
use TheSupportGroup\Common\Validator\Contracts\Helpers\FieldsErrorBagInterface;
15
use TheSupportGroup\Common\Validator\Contracts\Helpers\ValidationResultProcessorInterface;
16
use TheSupportGroup\Common\Validator\Rules\BaseRule;
17
use TheSupportGroup\Common\Validator\Validator;
18
19
class ValidationResultProcessor implements ValidationResultProcessorInterface
20
{
21
    /** @var array|null all errors bag */
22
    public $fieldsErrorBag = null;
23
24
    /**
25
     * @param array $userMessages
26
     */
27
    public function __construct(
28
        FieldsErrorBagInterface $fieldsErrorBag,
29
        array $userMessages = []
30
    ) {
31
        $this->fieldsErrorBag = $fieldsErrorBag;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fieldsErrorBag of type object<TheSupportGroup\C...ieldsErrorBagInterface> is incompatible with the declared type array|null of property $fieldsErrorBag.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->fieldsErrorBag->setUserMessages($userMessages);
33
    }
34
35
    /**
36
     * Returns messages count.
37
     *
38
     * @return int
39
     */
40
    public function count()
41
    {
42
        return count($this->fieldsErrorBag->getErrorMessages());
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
43
    }
44
45
    /**
46
     * If such $field contains in.
47
     *
48
     * @param $fieldName
49
     *
50
     * @return bool
51
     */
52
    public function hasErrors($fieldName)
53
    {
54
        return (bool) count($this->fieldsErrorBag->getErrorMessages($fieldName));
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
55
    }
56
57
    /**
58
     * Get flat messages array, or all messages from field.
59
     *
60
     * @param string $field
61
     *
62
     * @return array
63
     */
64
    public function getErrors($field = '')
65
    {
66
        if ($field) {
67
            return isset($this->fieldsErrorBag->getErrorMessages()[$field]) ? $this->fieldsErrorBag->getErrorMessages()[$field] : [];
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
        }
69
70
        $messages = [];
71
72
        // Pass in the variable.
73
        $errorMessages = $this->fieldsErrorBag->getErrorMessages();
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
74
        array_walk_recursive($errorMessages, function ($message) use (&$messages) {
75
            $messages[] = $message;
76
        });
77
78
        return $messages;
79
    }
80
81
    /**
82
     * Get 2d array with fields and messages.
83
     *
84
     * @return array
85
     */
86
    public function getRawErrors()
87
    {
88
        return $this->fieldsErrorBag->getErrorMessages();
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
89
    }
90
91
    /**
92
     * For each rule get it's first message.
93
     *
94
     * @return array
95
     */
96
    public function firsts()
97
    {
98
        $messages = [];
99
        foreach ($this->fieldsErrorBag->getErrorMessages() as $fieldsMessages) {
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
100
            foreach ($fieldsMessages as $fieldMessage) {
101
                $messages[] = $fieldMessage;
102
                break;
103
            }
104
        }
105
106
        return $messages;
107
    }
108
109
    /**
110
     * Returns first message from $field or error messages array.
111
     *
112
     * @param string $field
113
     *
114
     * @return mixed
115
     */
116
    public function first($field = '')
117
    {
118
        if (isset($this->fieldsErrorBag->getErrorMessages()[$field])) {
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
            $message = reset($this->fieldsErrorBag->getErrorMessages()[$field]);
0 ignored issues
show
Bug introduced by
The method getErrorMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
120
        } else {
121
            $firstMessages = $this->firsts();
122
            $message = reset($firstMessages);
123
        }
124
125
        return $message;
126
    }
127
128
    /**
129
     * Choosing error message: custom or default.
130
     *
131
     * @param $instance
132
     * 
133
     * @return $this
134
     */
135
    public function chooseErrorMessage(BaseRule $instance)
136
    {
137
        list($fieldName, $ruleValue, $ruleParams) = $instance->getParams();
138
        $ruleErrorFormat = $fieldName.'.'.lcfirst($instance->getRuleName());
139
140
        if (isset($this->fieldsErrorBag->getUserMessages()[$ruleErrorFormat])) {
0 ignored issues
show
Bug introduced by
The method getUserMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
141
            $ruleErrorMessage = $this->fieldsErrorBag->getUserMessages()[$ruleErrorFormat];
0 ignored issues
show
Bug introduced by
The method getUserMessages cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
142
        } else {
143
            $ruleErrorMessage = $instance->getMessage();
144
        }
145
146
        $this->fieldsErrorBag->add(
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
147
            $fieldName,
148
            strtr(
149
                $ruleErrorMessage,
150
                [
151
                    ':field:' => $fieldName,
152
                    ':rule:' => $ruleValue,
153
                    ':param:' => $ruleParams,
154
                ]
155
            )
156
        );
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get messages.
163
     *
164
     * @param $fieldName
165
     *
166
     * @return FieldsErrorBag
167
     */
168
    public function __get($fieldName)
169
    {
170
        return $this->fieldsErrorBag->setField($fieldName);
0 ignored issues
show
Bug introduced by
The method setField cannot be called on $this->fieldsErrorBag (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
    }
172
}
173