1 | <?php |
||
8 | class InputModel implements Arrayable |
||
9 | { |
||
10 | /** |
||
11 | * @var \LaravelPlus\Extension\Specs\InputSpec |
||
12 | */ |
||
13 | protected $spec; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $in; |
||
19 | |||
20 | /** |
||
21 | * @var \Illuminate\Validation\Validator |
||
22 | */ |
||
23 | protected $validator; |
||
24 | |||
25 | /** |
||
26 | * @param string | \LaravelPlus\Extension\Specs\InputSpec $pathOrSpec |
||
27 | * @param array $in |
||
28 | * |
||
29 | * @return static |
||
30 | */ |
||
31 | 1 | public static function make($pathOrSpec, array $in = null) |
|
42 | |||
43 | /** |
||
44 | * @param \LaravelPlus\Extension\Specs\InputSpec $spec |
||
45 | * @param array $in |
||
46 | */ |
||
47 | 5 | public function __construct(InputSpec $spec, array $in = null) |
|
48 | { |
||
49 | 5 | $this->spec = $spec; |
|
50 | 5 | $this->in = $in ?: $this->gatherInput(); |
|
51 | |||
52 | 5 | $rules = $this->spec->rules(); |
|
53 | 5 | if (!is_array($rules)) { |
|
54 | throw new InvalidArgumentException("rule specs for '$path' must array."); |
||
55 | } |
||
56 | |||
57 | 5 | $ruleMessages = $this->spec->ruleMessages(); |
|
58 | 5 | if (!is_array($ruleMessages)) { |
|
59 | throw new InvalidArgumentException("rule translation for '$path' must array."); |
||
60 | } |
||
61 | |||
62 | 5 | $labels = $this->spec->labels(); |
|
63 | 5 | if (!is_array($labels)) { |
|
64 | throw new InvalidArgumentException("rule labels for '$path' must array."); |
||
65 | } |
||
66 | |||
67 | 5 | $this->validator = app('validator')->make($this->in, $rules, $ruleMessages, $labels); |
|
68 | 5 | } |
|
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | 2 | protected function gatherInput() |
|
77 | |||
78 | /** |
||
79 | * @return bool |
||
80 | */ |
||
81 | 1 | public function passes() |
|
85 | |||
86 | /** |
||
87 | * @return bool |
||
88 | */ |
||
89 | 1 | public function fails() |
|
93 | |||
94 | /** |
||
95 | * @return \Illuminate\Support\MessageBag |
||
96 | */ |
||
97 | 1 | public function errors() |
|
101 | |||
102 | /** |
||
103 | * @return \Illuminate\Validation\Validator |
||
104 | */ |
||
105 | 1 | public function validator() |
|
109 | |||
110 | /** |
||
111 | * Dynamically retrieve attributes on the model. |
||
112 | * |
||
113 | * @param string $key |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | 1 | public function __get($key) |
|
121 | |||
122 | /** |
||
123 | * Dynamically set attributes on the model. |
||
124 | * |
||
125 | * @param string $key |
||
126 | * @param mixed $value |
||
127 | */ |
||
128 | 1 | public function __set($key, $value) |
|
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | 1 | public function input() |
|
140 | |||
141 | /** |
||
142 | * Get the instance as an array. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 1 | public function toArray() |
|
150 | } |
||
151 |