Completed
Push — master ( 73f026...dc38fa )
by Sebastian
04:16
created

ValidationServiceProvider::boot()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 26
rs 8.8571
cc 2
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace App\Providers;
4
5
use App\Models\Enums\TagType;
6
use Illuminate\Support\ServiceProvider;
7
use Validator;
8
9
class ValidationServiceProvider extends ServiceProvider
10
{
11
    public function register()
12
    {
13
    }
14
15
    public function boot()
16
    {
17
        Validator::extend('tags_exist', function ($attribute, $tagNames, $parameters) {
18
            list($tagType) = $parameters;
19
            $tagType = new TagType($tagType);
20
21
            $exisitingTagNames = app(\App\Repositories\TagRepository::class)
22
                ->getAllWithType($tagType)
23
                ->lists('name')
24
                ->toArray();
25
26
            if (!\Spatie\values_in_array($tagNames, $exisitingTagNames)) {
27
                return false;
28
            }
29
30
            return true;
31
        });
32
33
        Validator::extend('enum', function ($attribute, $value, $parameters) {
34
35
            /** @var \App\Foundation\Models\Enums\Enum $class */
36
            $class = $parameters[0];
37
38
            return $class::isValid($value);
39
        });
40
    }
41
}
42