|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelPlus\Extension\Specs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
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) |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (is_string($pathOrSpec)) { |
|
34
|
1 |
|
$spec = new InputSpec(app('specs'), app('translator'), $pathOrSpec); |
|
35
|
|
|
} |
|
36
|
|
|
else { |
|
37
|
|
|
$spec = $pathOrSpec; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
return new static($spec, $in); |
|
41
|
|
|
} |
|
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() |
|
74
|
|
|
{ |
|
75
|
2 |
|
return app('request')->only($this->spec->attributes()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function passes() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->validator->passes(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function fails() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->validator->fails(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return \Illuminate\Support\MessageBag |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function errors() |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->validator->errors(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return \Illuminate\Validation\Validator |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function validator() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->validator; |
|
108
|
|
|
} |
|
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) |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->in[$key]; |
|
120
|
|
|
} |
|
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) |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->in[$key] = $value; |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function input() |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->in; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the instance as an array. |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function toArray() |
|
147
|
|
|
{ |
|
148
|
1 |
|
return $this->input(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|