forked from FrozenNode/Laravel-Administrator
/
Config.php
189 lines (164 loc) · 3.7 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Frozennode\Administrator\Config;
use Frozennode\Administrator\Validator;
use Frozennode\Administrator\Config\ConfigInterface;
use Illuminate\Validation\Validator as CustomValidator;
abstract class Config {
/**
* The validator instance
*
* @var \Frozennode\Administrator\Validator
*/
protected $validator;
/**
* The site's normal validator instance
*
* @var \Illuminate\Validation\Validator
*/
protected $customValidator;
/**
* The user supplied options array
*
* @var array
*/
protected $suppliedOptions = array();
/**
* The original configuration options that were supplied
*
* @var array
*/
protected $options;
/**
* The defaults property
*
* @var array
*/
protected $defaults = array();
/**
* The rules property
*
* @var array
*/
protected $rules = array();
/**
* Create a new model Config instance
*
* @param \Frozennode\Administrator\Validator $validator
* @param \Illuminate\Validation\Validator $custom_validator
* @param array $options
*/
public function __construct(Validator $validator, CustomValidator $custom_validator, array $options)
{
$this->validator = $validator;
$this->customValidator = $custom_validator;
$this->suppliedOptions = $options;
}
/**
* Validates the supplied options
*
* @return void
*/
public function validateOptions()
{
//override the config
$this->validator->override($this->suppliedOptions, $this->rules);
//if the validator failed, throw an exception
if ($this->validator->fails())
{
throw new \InvalidArgumentException('There are problems with your ' . $this->suppliedOptions['name'] . ' config: ' .
implode('. ', $this->validator->messages()->all()));
}
}
/**
* Builds the necessary fields on the object
*
* @return void
*/
public function build()
{
$options = $this->suppliedOptions;
//check the permission
$options['permission'] = isset($options['permission']) ? $options['permission']() : true;
$this->suppliedOptions = $options;
}
/**
* Config type getter
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Gets all user options
*
* @return array
*/
public function getOptions()
{
//make sure the supplied options have been merged with the defaults
if (empty($this->options))
{
//validate the options and build them
$this->validateOptions();
$this->build();
$this->options = array_merge($this->defaults, $this->suppliedOptions);
}
return $this->options;
}
/**
* Gets a config option
*
* @param string $key
*
* @return mixed
*/
public function getOption($key)
{
$options = $this->getOptions();
if (!array_key_exists($key, $options))
{
throw new \InvalidArgumentException("An invalid option was searched for in the '" . $options['name'] . "' config");
}
return $options[$key];
}
/**
* Sets the user options
*
* @param array $options
*
* @return array
*/
public function setOptions(array $options)
{
//unset the current options
$this->options = array();
//override the supplied options
$this->suppliedOptions = $options;
}
/**
* Validates the supplied data against the options rules
*
* @param array $data
* @param array $rules
* @param array $messages
*
* @param mixed
*/
public function validateData(array $data, array $rules, array $messages)
{
if ($rules)
{
$this->customValidator->setData($data);
$this->customValidator->setRules($rules);
$this->customValidator->setCustomMessages($messages);
//if the validator fails, kick back the errors
if ($this->customValidator->fails())
{
return implode('. ', $this->customValidator->messages()->all());
}
}
return true;
}
}