Completed
Push — master ( b39d72...4d7bc0 )
by Alexis
02:02
created

Configuration::setOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace Awurth\SlimValidation;
4
5
use InvalidArgumentException;
6
use Respect\Validation\Rules\AllOf;
7
8
class Configuration
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $group;
14
15
    /**
16
     * @var string
17
     */
18
    protected $key;
19
20
    /**
21
     * @var string
22
     */
23
    protected $message;
24
25
    /**
26
     * @var string[]
27
     */
28
    protected $messages = [];
29
30
    /**
31
     * @var AllOf
32
     */
33
    protected $rules;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param AllOf|array $options
39
     * @param string $key
40
     * @param string $group
41
     */
42
    public function __construct($options, $key = null, $group = null)
43
    {
44
        $this->key = $key;
45
        $this->group = $group;
46
47
        if ($options instanceof AllOf) {
48
            $this->rules = $options;
49
        } else {
50
            $this->setOptions($options);
51
        }
52
53
        $this->validateOptions();
54
    }
55
56
    /**
57
     * Gets the group to use for errors and values storage.
58
     *
59
     * @return string
60
     */
61
    public function getGroup()
62
    {
63
        return $this->group;
64
    }
65
66
    /**
67
     * Gets the key to use for errors and values storage.
68
     *
69
     * @return string
70
     */
71
    public function getKey()
72
    {
73
        return $this->key;
74
    }
75
76
    /**
77
     * Gets the error message.
78
     *
79
     * @return string|null
80
     */
81
    public function getMessage()
82
    {
83
        return $this->message;
84
    }
85
86
    /**
87
     * Gets individual rules messages.
88
     *
89
     * @return string[]
90
     */
91
    public function getMessages()
92
    {
93
        return $this->messages;
94
    }
95
96
    /**
97
     * Gets the validation rules.
98
     *
99
     * @return AllOf
100
     */
101
    public function getValidationRules()
102
    {
103
        return $this->rules;
104
    }
105
106
    /**
107
     * Tells whether a group has been set.
108
     *
109
     * @return bool
110
     */
111
    public function hasGroup()
112
    {
113
        return !empty($this->group);
114
    }
115
116
    /**
117
     * Tells whether a key has been set.
118
     *
119
     * @return bool
120
     */
121
    public function hasKey()
122
    {
123
        return !empty($this->key);
124
    }
125
126
    /**
127
     * Tells whether a single message has been set.
128
     *
129
     * @return bool
130
     */
131
    public function hasMessage()
132
    {
133
        return !empty($this->message);
134
    }
135
136
    /**
137
     * Tells whether individual rules messages have been set.
138
     *
139
     * @return bool
140
     */
141
    public function hasMessages()
142
    {
143
        return !empty($this->messages);
144
    }
145
146
    /**
147
     * Sets the group to use for errors and values storage.
148
     *
149
     * @param string $group
150
     */
151
    public function setGroup($group)
152
    {
153
        $this->group = $group;
154
    }
155
156
    /**
157
     * Sets the key to use for errors and values storage.
158
     *
159
     * @param string $key
160
     */
161
    public function setKey($key)
162
    {
163
        $this->key = $key;
164
    }
165
166
    /**
167
     * Sets the error message.
168
     *
169
     * @param string $message
170
     */
171
    public function setMessage($message)
172
    {
173
        $this->message = $message;
174
    }
175
176
    /**
177
     * Sets individual rules messages.
178
     *
179
     * @param string[] $messages
180
     */
181
    public function setMessages(array $messages)
182
    {
183
        $this->messages = $messages;
184
    }
185
186
    /**
187
     * Sets options from an array.
188
     *
189
     * @param array $options
190
     */
191
    public function setOptions(array $options)
192
    {
193
        $availableOptions = [
194
            'group',
195
            'key',
196
            'message',
197
            'messages',
198
            'rules'
199
        ];
200
201
        foreach ($availableOptions as $option) {
202
            if (isset($options[$option])) {
203
                $this->$option = $options[$option];
204
            }
205
        }
206
    }
207
208
    /**
209
     * Sets the validation rules.
210
     *
211
     * @param AllOf $rules
212
     */
213
    public function setValidationRules(AllOf $rules)
214
    {
215
        $this->rules = $rules;
216
    }
217
218
    /**
219
     * Verifies that all mandatory options are set and valid.
220
     */
221
    public function validateOptions()
222
    {
223
        if (!$this->rules instanceof AllOf) {
224
            throw new InvalidArgumentException('Validation rules are missing or invalid');
225
        }
226
227
        if (!$this->hasKey()) {
228
            throw new InvalidArgumentException('A key must be set');
229
        }
230
    }
231
}
232