Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation\Rules;
4
 
5
use Illuminate\Contracts\Validation\Rule;
6
use TypeError;
7
 
8
class Enum implements Rule
9
{
10
    /**
11
     * The type of the enum.
12
     *
13
     * @var string
14
     */
15
    protected $type;
16
 
17
    /**
18
     * Create a new rule instance.
19
     *
20
     * @param  string  $type
21
     * @return void
22
     */
23
    public function __construct($type)
24
    {
25
        $this->type = $type;
26
    }
27
 
28
    /**
29
     * Determine if the validation rule passes.
30
     *
31
     * @param  string  $attribute
32
     * @param  mixed  $value
33
     * @return bool
34
     */
35
    public function passes($attribute, $value)
36
    {
37
        if ($value instanceof $this->type) {
38
            return true;
39
        }
40
 
41
        if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) {
42
            return false;
43
        }
44
 
45
        try {
46
            return ! is_null($this->type::tryFrom($value));
47
        } catch (TypeError $e) {
48
            return false;
49
        }
50
    }
51
 
52
    /**
53
     * Get the validation error message.
54
     *
55
     * @return array
56
     */
57
    public function message()
58
    {
59
        $message = trans('validation.enum');
60
 
61
        return $message === 'validation.enum'
62
            ? ['The selected :attribute is invalid.']
63
            : $message;
64
    }
65
}