Passed
Push — master ( 93c0a5...b2d39c )
by Stephen
54s queued 11s
created

Apply::makeValidatorDataArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Monooso\Apposite\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
7
use Illuminate\Support\Facades\Validator;
8
9
abstract class Apply implements Rule
10
{
11
    /** @var bool */
12
    protected $shouldApply;
13
14
    /** @var array */
15
    protected $messages;
16
17
    /** @var array|string */
18
    protected $rules;
19
20
    /**
21
     * Initialise the custom rule
22
     *
23
     * @param bool|callable $conditional
24
     * @param array|string  $rules
25
     */
26 9
    public function __construct($conditional, $rules)
27
    {
28 9
        $this->reset();
29 9
        $this->rules = $rules;
30 9
        $this->shouldApply = (bool)(is_callable($conditional) ? $conditional() : $conditional);
31 9
    }
32
33
    /**
34
     * Reset the instance variables
35
     */
36 9
    protected function reset()
37
    {
38 9
        $this->shouldApply = false;
39 9
        $this->messages = [];
40 9
        $this->rules = [];
41 9
    }
42
43
    /**
44
     * Determine if the validation rule passes
45
     *
46
     * @param string $attribute
47
     * @param mixed  $value
48
     *
49
     * @return bool
50
     */
51 9
    public function passes($attribute, $value): bool
52
    {
53 9
        $this->messages = $this->validateIf($this->shouldApply, $this->makeValidator($attribute, $value));
54
55 9
        return count($this->messages) === 0;
56
    }
57
58
    /**
59
     * Run the given validator, if the conditional evaluates to true
60
     *
61
     * @param bool              $shouldApply
62
     * @param ValidatorContract $validator
63
     *
64
     * @return array
65
     */
66 9
    protected function validateIf(bool $shouldApply, ValidatorContract $validator): array
67
    {
68 9
        return ($shouldApply && $validator->fails()) ? $validator->errors()->all() : [];
69
    }
70
71
    /**
72
     * Build the validator instance, to validate the given attribute and value
73
     *
74
     * @param string $attribute
75
     * @param mixed  $value
76
     *
77
     * @return ValidatorContract
78
     */
79 9
    protected function makeValidator(string $attribute, $value): ValidatorContract
80
    {
81 9
        return Validator::make(
82 9
            $this->makeValidatorDataArray($attribute, $value),
83 9
            $this->makeValidatorRulesArray($attribute, $this->getRules())
84
        );
85
    }
86
87
    /**
88
     * Build the validator data array
89
     *
90
     * @param string $attribute
91
     * @param mixed  $value
92
     *
93
     * @return array
94
     */
95 9
    protected function makeValidatorDataArray(string $attribute, $value): array
96
    {
97 9
        return [$attribute => $value];
98
    }
99
100
    /**
101
     * Build the validator rules array
102
     *
103
     * @param string       $attribute
104
     * @param array|string $rules
105
     *
106
     * @return array
107
     */
108 9
    protected function makeValidatorRulesArray(string $attribute, $rules): array
109
    {
110 9
        return [$attribute => $rules];
111
    }
112
113
    /**
114
     * Get the validation rules to apply
115
     *
116
     * @return array|string
117
     */
118 9
    protected function getRules()
119
    {
120 9
        return $this->rules;
121
    }
122
123
    /**
124
     * Get the validation error messages
125
     *
126
     * @return array
127
     */
128 6
    public function message()
129
    {
130 6
        return $this->messages;
131
    }
132
}
133