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;
4
 
5
use Illuminate\Support\Fluent;
6
 
7
class ConditionalRules
8
{
9
    /**
10
     * The boolean condition indicating if the rules should be added to the attribute.
11
     *
12
     * @var callable|bool
13
     */
14
    protected $condition;
15
 
16
    /**
17
     * The rules to be added to the attribute.
18
     *
19
     * @var array|string|\Closure
20
     */
21
    protected $rules;
22
 
23
    /**
24
     * The rules to be added to the attribute if the condition fails.
25
     *
26
     * @var array|string|\Closure
27
     */
28
    protected $defaultRules;
29
 
30
    /**
31
     * Create a new conditional rules instance.
32
     *
33
     * @param  callable|bool  $condition
34
     * @param  array|string|\Closure  $rules
35
     * @param  array|string|\Closure  $defaultRules
36
     * @return void
37
     */
38
    public function __construct($condition, $rules, $defaultRules = [])
39
    {
40
        $this->condition = $condition;
41
        $this->rules = $rules;
42
        $this->defaultRules = $defaultRules;
43
    }
44
 
45
    /**
46
     * Determine if the conditional rules should be added.
47
     *
48
     * @param  array  $data
49
     * @return bool
50
     */
51
    public function passes(array $data = [])
52
    {
53
        return is_callable($this->condition)
54
                    ? call_user_func($this->condition, new Fluent($data))
55
                    : $this->condition;
56
    }
57
 
58
    /**
59
     * Get the rules.
60
     *
61
     * @param  array  $data
62
     * @return array
63
     */
64
    public function rules(array $data = [])
65
    {
66
        return is_string($this->rules)
67
                    ? explode('|', $this->rules)
68
                    : value($this->rules, new Fluent($data));
69
    }
70
 
71
    /**
72
     * Get the default rules.
73
     *
74
     * @param  array  $data
75
     * @return array
76
     */
77
    public function defaultRules(array $data = [])
78
    {
79
        return is_string($this->defaultRules)
80
                    ? explode('|', $this->defaultRules)
81
                    : value($this->defaultRules, new Fluent($data));
82
    }
83
}