Completed
Push — master ( 9785ad...b39d72 )
by Alexis
01:55
created

Configuration::__construct()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 6
nop 3
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
        if ($options instanceof AllOf) {
45
            if (empty($key)) {
46
                throw new InvalidArgumentException('The key must be set');
47
            }
48
49
            $this->rules = $options;
50
            $this->key = $key;
51
            $this->group = $group;
52
        } elseif (is_array($options)) {
53
            if (!isset($options['rules']) || !$options['rules'] instanceof AllOf) {
54
                throw new InvalidArgumentException('Validation rules are missing or invalid');
55
            }
56
57
            $this->key = $options['key'] ?? $key;
58
59
            if (!$this->hasKey()) {
60
                throw new InvalidArgumentException('The key must be set');
61
            }
62
63
            $this->group = $options['group'] ?? $group;
64
            $this->message = $options['message'] ?? null;
65
            $this->messages = $options['messages'] ?? [];
66
            $this->rules = $options['rules'];
67
        } else {
68
            throw new InvalidArgumentException(sprintf('Options must be of type %s or array, %s given', AllOf::class, gettype($options)));
69
        }
70
    }
71
72
    /**
73
     * Gets the group to use for errors and values storage.
74
     *
75
     * @return string
76
     */
77
    public function getGroup()
78
    {
79
        return $this->group;
80
    }
81
82
    /**
83
     * Gets the key to use for errors and values storage.
84
     *
85
     * @return string
86
     */
87
    public function getKey()
88
    {
89
        return $this->key;
90
    }
91
92
    /**
93
     * Gets the error message.
94
     *
95
     * @return string|null
96
     */
97
    public function getMessage()
98
    {
99
        return $this->message;
100
    }
101
102
    /**
103
     * Gets individual rules messages.
104
     *
105
     * @return string[]
106
     */
107
    public function getMessages()
108
    {
109
        return $this->messages;
110
    }
111
112
    /**
113
     * Gets the validation rules.
114
     *
115
     * @return AllOf
116
     */
117
    public function getValidationRules()
118
    {
119
        return $this->rules;
120
    }
121
122
    /**
123
     * Tells whether a group has been set.
124
     *
125
     * @return bool
126
     */
127
    public function hasGroup()
128
    {
129
        return !empty($this->group);
130
    }
131
132
    /**
133
     * Tells whether a key has been set.
134
     *
135
     * @return bool
136
     */
137
    public function hasKey()
138
    {
139
        return !empty($this->key);
140
    }
141
142
    /**
143
     * Tells whether a single message has been set.
144
     *
145
     * @return bool
146
     */
147
    public function hasMessage()
148
    {
149
        return !empty($this->message);
150
    }
151
152
    /**
153
     * Tells whether individual rules messages have been set.
154
     *
155
     * @return bool
156
     */
157
    public function hasMessages()
158
    {
159
        return !empty($this->messages);
160
    }
161
162
    /**
163
     * Sets the group to use for errors and values storage.
164
     *
165
     * @param string $group
166
     */
167
    public function setGroup($group)
168
    {
169
        $this->group = $group;
170
    }
171
172
    /**
173
     * Sets the key to use for errors and values storage.
174
     *
175
     * @param string $key
176
     */
177
    public function setKey($key)
178
    {
179
        $this->key = $key;
180
    }
181
182
    /**
183
     * Sets the error message.
184
     *
185
     * @param string $message
186
     */
187
    public function setMessage($message)
188
    {
189
        $this->message = $message;
190
    }
191
192
    /**
193
     * Sets individual rules messages.
194
     *
195
     * @param string[] $messages
196
     */
197
    public function setMessages(array $messages)
198
    {
199
        $this->messages = $messages;
200
    }
201
202
    /**
203
     * Sets the validation rules.
204
     *
205
     * @param AllOf $rules
206
     */
207
    public function setValidationRules(AllOf $rules)
208
    {
209
        $this->rules = $rules;
210
    }
211
}
212