Dates::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
namespace Tuum\Form;
3
4
use Traversable;
5
use Tuum\Form\Data\Inputs;
6
use Tuum\Form\Lists\DatesComposer;
7
use Tuum\Form\Lists\Lists;
8
use Tuum\Form\Tags\Composite;
9
use Tuum\Form\Tags\Select;
10
11
/**
12
 * Class Date
13
 *
14
 * @package Tuum\Form
15
 */
16
class Dates
17
{
18
    /**
19
     * @var Traversable|array
20
     */
21
    private $years;
22
23
    /**
24
     * @var Traversable|array
25
     */
26
    private $months;
27
28
    /**
29
     * @var Traversable|array
30
     */
31
    private $days;
32
33
    /**
34
     * @var Traversable|array
35
     */
36
    private $hours;
37
38
    /**
39
     * @var Traversable|array
40
     */
41
    private $minutes;
42
43
    /**
44
     * @var Traversable|array
45
     */
46
    private $seconds;
47
48
    /**
49
     * @var Inputs
50
     */
51
    private $inputs;
52
53
    /**
54
     * @var string
55
     */
56
    private $default_class;
57
58
    /**
59
     * @var DatesComposer
60
     */
61
    public $datesComposer;
62
63
    /**
64
     * constructor
65
     *
66
     * @param array $options
67
     */
68
    public function __construct($options = [])
69
    {
70
        foreach( ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] as $field) {
71
            if (isset($options[$field])) {
72
                $this->$field = $options[$field];
73
            }
74
        }
75
        $this->datesComposer = new DatesComposer($this);
76
    }
77
78
    /**
79
     * @param Inputs $inputs
80
     * @return $this
81
     */
82
    public function setInputs($inputs)
83
    {
84
        $this->inputs = $inputs;
85
        $this->datesComposer->setInputs($inputs);
86
        return $this;
87
    }
88
89
    /**
90
     * sets default class name for composite selects.
91
     *
92
     * @param string $class
93
     * @return Dates
94
     */
95
    public function setClass($class)
96
    {
97
        $this->default_class = $class;
98
        return $this;
99
    }
100
101
    /**
102
     * @param Traversable|array $years
103
     * @return $this
104
     */
105
    public function setYear($years)
106
    {
107
        $this->years = $years;
108
        return $this;
109
    }
110
111
    /**
112
     * @param Traversable|array $months
113
     * @return $this
114
     */
115
    public function setMonth($months)
116
    {
117
        $this->months = $months;
118
        return $this;
119
    }
120
121
    /**
122
     * @param Traversable|array $day
123
     * @return $this
124
     */
125
    public function setDay($day)
126
    {
127
        $this->days = $day;
128
        return $this;
129
    }
130
131
    /**
132
     * @param Traversable|array $list
133
     * @return $this
134
     */
135
    public function setHour($list)
136
    {
137
        $this->hours = $list;
138
        return $this;
139
    }
140
141
    /**
142
     * @param Traversable|array $list
143
     * @return $this
144
     */
145
    public function setMinute($list)
146
    {
147
        $this->minutes = $list;
148
        return $this;
149
    }
150
151
    /**
152
     * @param Traversable|array $list
153
     * @return $this
154
     */
155
    public function setSecond($list)
156
    {
157
        $this->seconds = $list;
158
        return $this;
159
    }
160
161
    /**
162
     * @param string $name
163
     * @param string $value
164
     * @return Select
165
     */
166
    public function selYear($name, $value = null)
167
    {
168
        $years = $this->years ?: Lists::years();
169
        return $this->makeSelect($name, $years, $value);
170
    }
171
172
    /**
173
     * @param string $name
174
     * @param string $value
175
     * @return Select
176
     */
177
    public function selDay($name, $value = null)
178
    {
179
        $days = $this->days ?: Lists::days();
180
        return $this->makeSelect($name, $days, $value);
181
    }
182
183
    /**
184
     * @param string $name
185
     * @param string $value
186
     * @return Select
187
     */
188
    public function selMonth($name, $value = null)
189
    {
190
        $months = $this->months ?: Lists::months();
191
        return $this->makeSelect($name, $months, $value);
192
    }
193
194
    /**
195
     * @param string $name
196
     * @param string $value
197
     * @return Select
198
     */
199
    public function selHour($name, $value = null)
200
    {
201
        $hour = $this->hours ?: Lists::hours();
202
        return $this->makeSelect($name, $hour, $value);
203
    }
204
205
    /**
206
     * @param string $name
207
     * @param string $value
208
     * @return Select
209
     */
210
    public function selMinute($name, $value = null)
211
    {
212
        $minutes = $this->minutes ?: Lists::minutes();
213
        return $this->makeSelect($name, $minutes, $value);
214
    }
215
216
    /**
217
     * @param string $name
218
     * @param string $value
219
     * @return Select
220
     */
221
    public function selSecond($name, $value = null)
222
    {
223
        $seconds = $this->seconds ?: Lists::seconds();
224
        return $this->makeSelect($name, $seconds, $value);
225
    }
226
227
    /**
228
     * @param string            $name
229
     * @param array|Traversable $list
230
     * @param string            $value
231
     * @return Select
232
     */
233
    private function makeSelect($name, $list, $value)
234
    {
235
        $value  = $this->inputs ? $this->inputs->raw($name, $value) : $value;
236
        $select = new Select($name, $list, $value);
237
        if ($this->default_class) {
238
            $select->class($this->default_class);
239
        }
240
        return $select;
241
    }
242
243
    /**
244
     * @param string $name
245
     * @param array  $fields
246
     * @param string $format
247
     * @param string $value
248
     * @return Composite
249
     */
250
    public function makeComposite($name, $fields, $format, $value)
251
    {
252
        $value = $this->inputs ? $this->inputs->raw($name, $value) : $value;
253
        return (new Composite($name, $fields, $format))->value($value);
254
    }
255
    
256
    /**
257
     * @param string      $name
258
     * @param string|null $value
259
     * @param string|null $ymd_format
260
     * @return Composite
261
     */
262
    public function dateYMD($name, $value = null, $ymd_format = null)
263
    {
264
        return $this->datesComposer->dateYMD($name, $value, $ymd_format);
265
    }
266
    
267
    /**
268
     * @param string      $name
269
     * @param string|null $value
270
     * @param string|null $ym_format
271
     * @return Composite
272
     */
273
    public function dateYM($name, $value = null, $ym_format = null)
274
    {
275
        return $this->datesComposer->dateYM($name, $value, $ym_format);
276
    }
277
278
    /**
279
     * @param string      $name
280
     * @param string|null $value
281
     * @param string|null $hi_format
282
     * @return Composite
283
     */
284
    public function timeHi($name, $value = null, $hi_format = null)
285
    {
286
        return $this->datesComposer->timeHi($name, $value, $hi_format);
287
    }
288
289
    /**
290
     * @param string      $name
291
     * @param string|null $value
292
     * @param string|null $his_format
293
     * @return Composite
294
     */
295
    public function timeHis($name, $value = null, $his_format = null)
296
    {
297
        return $this->datesComposer->timeHis($name, $value, $his_format);
298
    }
299
}