GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ca95e4...2437e1 )
by Marceau
06:05
created

CrudFields::contextualValidationRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Akibatech\Crud\Services;
4
5
use Akibatech\Crud\Exceptions\DuplicateFieldIdentifierException;
6
use Akibatech\Crud\Exceptions\NoFieldsException;
7
use Akibatech\Crud\Fields\Field;
8
use Illuminate\Support\MessageBag;
9
use Illuminate\Validation\Validator;
10
11
/**
12
 * Class CrudFields
13
 *
14
 * @package Akibatech\Crud\Services
15
 */
16
class CrudFields
17
{
18
    /**
19
     * @var Field[]
20
     */
21
    protected $fields = [];
22
23
    /**
24
     * @var CrudEntry
25
     */
26
    protected $entry;
27
28
    /**
29
     * @var MessageBag
30
     */
31
    protected $errors;
32
33
    /**
34
     * @var MessageBag
35
     */
36
    protected $old_input;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $multipart = false;
42
43
    /**
44
     * Make staticly a new instance.
45
     *
46
     * @param   array $fields
47
     * @return  CrudFields
48
     */
49
    public static function make(array $fields)
50
    {
51
        return (new static)->add($fields);
52
    }
53
54
    /**
55
     * @param   CrudEntry $entry
56
     * @return  self
57
     */
58
    public function setEntry(CrudEntry $entry)
59
    {
60
        $this->entry = $entry;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param   void
67
     * @return  CrudEntry
68
     */
69
    public function getEntry()
70
    {
71
        return $this->entry;
72
    }
73
74
    /**
75
     * @param   Field[] $fields
76
     * @return  self
77
     * @throws  DuplicateFieldIdentifierException
78
     */
79
    public function add(array $fields)
80
    {
81
        if (is_array($fields) && count($fields) > 0)
82
        {
83
            /** @var Field $field */
84
            foreach ($fields as $field)
85
            {
86
                $this->set($field);
87
            }
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param   Field $field
95
     * @return  self
96
     * @throws  DuplicateFieldIdentifierException
97
     */
98
    public function set(Field $field)
99
    {
100
        $identifier = $field->getIdentifier();
101
102
        if ($this->has($identifier))
103
        {
104
            throw new DuplicateFieldIdentifierException("$identifier was already set.");
105
        }
106
107
        $field->setFields($this);
108
        $this->fields[$identifier] = $field;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param   void
115
     * @return  int
116
     */
117
    public function count()
118
    {
119
        return count($this->fields);
120
    }
121
122
    /**
123
     * @param   string $identifier
124
     * @return  bool
125
     */
126
    public function has($identifier)
127
    {
128
        return array_key_exists($identifier, $this->fields) === true;
129
    }
130
131
    /**
132
     * @param   null|string $identifier
133
     * @return  Field[]|Field|null
134
     */
135
    public function get($identifier = null)
136
    {
137
        if (is_null($identifier))
138
        {
139
            return $this->fields;
140
        }
141
142
        if ($this->has($identifier))
143
        {
144
            return $this->fields[$identifier];
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * @param   void
152
     * @return  \Generator
153
     */
154
    public function columns()
155
    {
156
        foreach ($this->fields as $field)
157
        {
158
            yield $field->getLabel();
159
        }
160
    }
161
162
    /**
163
     * @param   void
164
     * @return  \Generator
165
     */
166
    public function loop()
167
    {
168
        foreach ($this->fields as $field)
169
        {
170
            yield $field;
171
        }
172
    }
173
174
    /**
175
     * Returns all fields keys.
176
     *
177
     * @param   void
178
     * @return  integer[]
179
     */
180
    public function keys()
181
    {
182
        return array_keys($this->fields);
183
    }
184
185
    /**
186
     * @param   void
187
     * @return  array
188
     */
189
    public function getFieldsScripts()
190
    {
191
        $scripts = [];
192
193
        foreach ($this->fields as $field)
194
        {
195
            $scripts = array_merge($scripts, $field->getScripts());
196
        }
197
198
        return $scripts;
199
    }
200
201
    /**
202
     * @param   void
203
     * @return  array
204
     */
205
    public function getFieldsCss()
206
    {
207
        $css = [];
208
209
        foreach ($this->fields as $field)
210
        {
211
            $css = array_merge($css, $field->getCss());
212
        }
213
214
        return $css;
215
    }
216
217
    /**
218
     * Gets fields validation rules.
219
     *
220
     * @param   void
221
     * @return  array
222
     * @throws  NoFieldsException
223
     */
224
    public function validationRules()
225
    {
226
        $rules = [];
227
228
        if ($this->count() === 0)
229
        {
230
            throw new NoFieldsException();
231
        }
232
233
        foreach ($this->fields as $key => $field)
234
        {
235
            $field_rules = $field->getRules();
236
237
            if (!empty($field_rules))
238
            {
239
                $rules[$key] = $field_rules;
240
            }
241
        }
242
243
        return $rules;
244
    }
245
246
    /**
247
     * @param   Validator $validator
248
     * @return  Validator
249
     */
250
    public function contextualValidationRules(Validator $validator)
251
    {
252
        foreach ($this->fields as &$field)
253
        {
254
            $validator = $field->beforeValidation($validator);
255
        }
256
257
        return $validator;
258
    }
259
260
    /**
261
     * Hydrate fields with new data.
262
     *
263
     * @param   array $data
264
     * @return  self
265
     */
266
    public function hydrateFormData(array $data)
267
    {
268
        if (count($data) === 0)
269
        {
270
            return $this;
271
        }
272
273
        foreach ($this->fields as $field)
274
        {
275
            if (array_key_exists($field->getIdentifier(), $data))
276
            {
277
                $field->newValue($data[$field->getIdentifier()]);
278
            }
279
        }
280
281
        return $this;
282
    }
283
284
    /**
285
     * @param   void
286
     * @return  MessageBag
287
     */
288
    public function getErrors()
289
    {
290
        if (is_null($this->errors))
291
        {
292
            $this->errors = new MessageBag();
293
        }
294
295
        return $this->errors;
296
    }
297
298
    /**
299
     * @param   void
300
     * @return  MessageBag
301
     */
302
    public function getOldInput()
303
    {
304
        if (is_null($this->old_input))
305
        {
306
            $this->old_input = new MessageBag();
307
        }
308
309
        return $this->old_input;
310
    }
311
312
    /**
313
     * @param   void
314
     * @return  bool
315
     */
316
    public function getMultipart()
317
    {
318
        foreach ($this->fields as $field)
319
        {
320
            if ($field::MULTIPART === true)
321
            {
322
                return true;
323
            }
324
        }
325
326
        return false;
327
    }
328
329
    /**
330
     * Hydrates the instance with previous validation errors.
331
     *
332
     * @param   MessageBag $errors
333
     * @return  self
334
     */
335 View Code Duplication
    public function hydrateErrorsFromSession(MessageBag $errors = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
    {
337
        if (!is_null($errors))
338
        {
339
            $this->errors = $errors;
340
            return $this;
341
        }
342
343
        $request = app()->make('request');
344
345
        if ($request->session()->has('errors'))
346
        {
347
            $this->errors = $request->session()->get('errors', MessageBag::class);
348
        }
349
350
        return $this;
351
    }
352
353
    /**
354
     * Hydrates the instance with previous input.
355
     *
356
     * @param   MessageBag $old_input
357
     * @return  self
358
     */
359 View Code Duplication
    public function hydrateFieldsFromSession(MessageBag $old_input = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360
    {
361
        if (!is_null($old_input))
362
        {
363
            $this->old_input = $old_input;
364
            return $this;
365
        }
366
367
        $request = app()->make('request');
368
369
        if ($request->session()->has('_old_input'))
370
        {
371
            $this->old_input = new MessageBag($request->old());
372
        }
373
374
        return $this;
375
    }
376
}
377