Passed
Push — feature/micro-ref-email ( cb4bd3...6f3c5c )
by Tristan
05:11
created

WordLimitRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 3 1
A passes() 0 4 1
A __construct() 0 3 1
1
<?php
2
3
namespace App\Services\Validation\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Lang;
7
8
class WordLimitRule implements Rule
9
{
10
    /**
11
     * The maximum amount of words the input element can reach.
12
     *
13
     * @var number
14
     */
15
    protected $max_words;
16
17
    /**
18
     * Create a new rule instance.
19
     *
20
     * @return void
21
     */
22
    public function __construct($max_words)
23
    {
24
        $this->max_words = $max_words;
25
    }
26
27
    /**
28
     * Determine if the validation rule passes.
29
     *
30
     * @param  string  $attribute
31
     * @param  mixed  $value
32
     * @return bool
33
     */
34
    public function passes($attribute, $value)
35
    {
36
        $words = explode(' ', preg_replace('/\s+/', ' ', $value));
37
        return count($words) <= $this->max_words;
38
    }
39
40
    /**
41
     * Get the validation error message.
42
     *
43
     * @return string
44
     */
45
    public function message()
46
    {
47
        return Lang::get('validation.custom.word_limit', ['max_words' => $this->max_words]);
48
    }
49
}
50