Passed
Push — feature/applicant-search ( 267b27 )
by Tristan
19:40 queued 06:40
created

ValidClassificationRule::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Services\Validation\Rules;
4
5
use App\Models\Classification;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Support\Facades\Lang;
8
9
class ValidClassificationRule implements Rule
10
{
11
    /**
12
     * Validation for Classification
13
     *
14
     * @var string
15
     */
16
    const PATTERN = '^(.+)-([0-9]+)$';
17
18
    /**
19
     * Determine if the validation rule passes.
20
     *
21
     * @param  string  $attribute
22
     * @param  mixed   $value
23
     * @return boolean
24
     */
25
    public function passes($attribute, $value)
26
    {
27
        $passesRegex = preg_match('/' . self::PATTERN . '/', $value, $matches);
28
        if (!$passesRegex) {
29
            return false;
30
        }
31
        $classificationCode = strToUpper($matches[1]);
32
        return Classification::where('key', $classificationCode)->exists();
33
    }
34
35
    /**
36
     * Get the validation error message.
37
     *
38
     * @return string
39
     */
40
    public function message()
41
    {
42
        return Lang::get('validation.custom.classification');
43
    }
44
}
45