Completed
Push — master ( 082c11...100fd0 )
by Freek
14:50
created

ModelsExist::passes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ModelsExist implements Rule
8
{
9
    /** @var string */
10
    protected $modelClassName;
11
12
    /** @var string */
13
    protected $attribute;
14
15
    public function __construct(string $modelClassName, string $attribute = 'id')
16
    {
17
        $this->modelClassName = $modelClassName;
18
19
        $this->attribute = $attribute;
20
    }
21
22
    public function passes($attribute, $value)
23
    {
24
        $value = array_filter($value);
25
26
        $modelIds = array_unique($value);
27
28
        $modelCount = $this->modelClassName::whereIn($this->attribute, $modelIds)->count();
0 ignored issues
show
Bug introduced by Alex Vanderbist
The method whereIn cannot be called on $this->modelClassName (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
29
30
        return count($modelIds) === $modelCount;
31
    }
32
33
    public function message()
34
    {
35
        return __('validationRules.model_ids');
36
    }
37
}
38