Passed
Push — master ( d7d8c0...c26592 )
by Mads
03:35
created

ValidateTrait::getValidatorFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Napp\Core\Api\Validation;
4
5
use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
6
use Illuminate\Contracts\Validation\Validator;
7
use Napp\Core\Api\Exceptions\Exceptions\ValidationException;
8
9
trait ValidateTrait
10
{
11
    /**
12
     * @param array $attributes
13
     * @param array $rules
14
     * @return void
15
     * @throws ValidationException
16
     */
17
    public static function validate(array $attributes, array $rules)
18
    {
19
        /** @var Validator $validator */
20
        $validator = static::getValidatorFactory()->make($attributes, $rules);
21
        if (true === $validator->fails()) {
22
            $message = $validator->messages()->first();
23
24
            $exception = new ValidationException;
25
            $exception->statusMessage = $exception->statusMessage . ': ' . $message;
26
27
            throw $exception;
28
        }
29
    }
30
31
    /**
32
     * @return ValidatorFactory
33
     */
34
    protected static function getValidatorFactory()
35
    {
36
        return app(ValidatorFactory::class);
37
    }
38
}
39