LaravelValidator::hasError()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php namespace Rocket\UI\Forms\ValidatorAdapters;
2
3
use Illuminate\Validation\Validator;
4
5
class LaravelValidator implements ValidatorInterface
6
{
7
    /**
8
     * @var Validator
9
     */
10
    protected $validator;
11
    protected $data;
12
    protected $defaults;
13
14
    /**
15
     * @var \Illuminate\Session\Store
16
     */
17
    protected $session;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function __construct($validator, $data, $defaults)
23
    {
24
        $this->validator = $validator;
25
        $this->data = $data;
26
        $this->defaults = $defaults;
27
    }
28
29
    /**
30
     * @return \Illuminate\Session\Store
31
     */
32
    protected function getSession()
33
    {
34
        //TODO :: do that by injection
35
        if (!$this->session) {
36
            $this->session = app('session');
37
        }
38
39
        return $this->session;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function hasError($name)
46
    {
47
        // The errors must be taken from the session, or else we have errors even if the form wasn't sent
48
        $session = $this->getSession();
49
        if ($session->has('errors')) {
50
            return $session->get('errors')->has($name);
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getErrors($name)
60
    {
61
        // The errors must be taken from the session, or else we have errors even if the form wasn't sent
62
        $session = $this->getSession();
63
        if ($session->has('errors')) {
64
            $errors = $session->get('errors');
65
66
            if ($errors->has($name)) {
67
                return $errors->get($name);
68
            }
69
        }
70
71
        return '';
72
    }
73
74
    /**
75
     * Transform key from array to dot syntax.
76
     *
77
     * @param  string $key
78
     *
79
     * @return string
80
     */
81
    protected function transformKey($key)
82
    {
83
        return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
84
    }
85
86
    /**
87
     * Get the current value.
88
     *
89
     * With the following priority:
90
     * 1. If the field was posted, take that value
91
     * 2. If there is a model that has a value, take it
92
     * 3. If there is a value defined when showing the field
93
     * 4. If there is a default set in the validator
94
     *
95
     * @param string $name
96
     * @param string $default
97
     * @return mixed
98
     */
99
    public function getValue($name, $default = '')
100
    {
101
        // 1.
102
        $old = $this->getSession()->getOldInput($this->transformKey($name));
103
        if (!is_null($old)) {
104
            return $old;
105
        }
106
107
        // 2.
108
        if (!empty($this->data) && $value = data_get($this->model, $this->transformKey($name))) {
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
            return $value;
110
        }
111
112
        // 3.
113
        if (!empty($default)) {
114
            return $default;
115
        }
116
117
        // 4.
118
        if (!empty($this->defaults) && $value = data_get($this->defaults, $this->transformKey($name))) {
119
            return $value;
120
        }
121
122
        return '';
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function isRequired($name)
129
    {
130
        $rules = $this->validator->getRules($name);
131
        if (array_key_exists($name, $rules)) {
132
            return is_array($rules[$name]) ? in_array('required', $rules[$name]) :
133
                str_contains($rules[$name], 'required');
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public static function supports($object)
143
    {
144
        return $object instanceof Validator;
0 ignored issues
show
Bug introduced by
The class Illuminate\Validation\Validator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
145
    }
146
}
147