BookingInputFilter::getStartTimeValidators()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTime\InputFilter;
4
5
use Zend\Filter\StringTrim;
6
use Zend\Filter\StripTags;
7
use Zend\Validator\DateStep;
8
use Zend\Validator\GreaterThan;
9
use Zend\Validator\LessThan;
10
use Zend\Validator\StringLength;
11
use Zend\Validator\ValidatorInterface;
12
use JhFlexiTime\Options\BookingOptionsInterface;
13
use JhFlexiTime\Filter\DateTimeFormatter;
14
use Zend\Validator\Date;
15
use Zend\InputFilter\InputFilter;
16
17
/**
18
 * Class BookingInputFilter
19
 * @package JhFlexiTime\InputFilter
20
 * @author Aydin Hassan <[email protected]>
21
 */
22
class BookingInputFilter extends InputFilter
23
{
24
25
    //TODO: Get from module config
26
    protected $config = [
27
        'date-format'   => 'd-m-Y',
28
        'step'          => '900', //15 x 60 - 15 Min's
29
    ];
30
31
    /**
32
     * @param ValidatorInterface $uniqueBookingValidator
33
     * @param ValidatorInterface $userExistsValidator
34
     * @param BookingOptionsInterface $bookingOptions
35
     */
36
    public function __construct(
37
        ValidatorInterface $uniqueBookingValidator,
38
        ValidatorInterface $userExistsValidator,
39
        BookingOptionsInterface $bookingOptions
40
    ) {
41
42
        //user
43
        $user = new Input('user');
44
        $user->setRequired(true);
45
        $user->getFilterChain()
46
            ->attach(new StripTags())
47
            ->attach(new StringTrim());
48
49
        $user->getValidatorChain()
50
            ->attach(new StringLength([
51
                'encoding' => 'UTF-8',
52
                'min'      => 1,
53
                'max'      => 512,
54
            ]))
55
            ->attach($userExistsValidator);
56
57
        $this->add($user);
58
59
        //date
60
        $date = new Input('date');
61
        $date->setRequired(true);
62
        $date->getFilterChain()
63
            ->attach(new DateTimeFormatter(['format' => 'd-m-Y']));
64
        $date->getValidatorChain()
65
            ->attach($uniqueBookingValidator)
66
            ->attach(new Date(['format' => 'd-m-Y']));
67
68
        $this->add($date);
69
70
        $startTimeValidators = $this->getStartTimeValidators();
71
72
        if ($bookingOptions->getMinStartTime()) {
73
            $startTimeValidators[] = new GreaterThan([
74
                'min'       => $bookingOptions->getMinStartTime(),
75
                'inclusive' => true,
76
            ]);
77
        }
78
79
        if ($bookingOptions->getMaxStartTime()) {
80
            $startTimeValidators[] = new LessThan([
81
                'max'       => $bookingOptions->getMaxStartTime(),
82
                'inclusive' => true,
83
            ]);
84
        }
85
86
        //start time
87
        $startTime = new Input('startTime');
88
        $startTime->setRequired(true);
89
        $startTime->getFilterChain()
90
            ->attach(new StringTrim());
91
92
        foreach ($startTimeValidators as $validator) {
93
            $startTime->getValidatorChain()->attach($validator);
94
        }
95
        $this->add($startTime);
96
97
        $endTimeValidators = $this->getEndTimeValidators();
98
99
        if ($bookingOptions->getMinEndTime()) {
100
            $endTimeValidators[] = new GreaterThan([
101
                'min'       => $bookingOptions->getMinEndTime(),
102
                'inclusive' => true,
103
            ]);
104
        }
105
106
        if ($bookingOptions->getMaxEndTime()) {
107
            $endTimeValidators[] = new LessThan([
108
                'max'       => $bookingOptions->getMaxEndTime(),
109
                'inclusive' => true,
110
            ]);
111
        }
112
113
        //end time
114
        $endTime = new Input('endTime');
115
        $endTime->setRequired(true);
116
        $endTime->getFilterChain()
117
            ->attach(new StringTrim());
118
119
        foreach ($endTimeValidators as $validator) {
120
            $endTime->getValidatorChain()->attach($validator);
121
        }
122
        $this->add($endTime);
123
124
        //notes
125
        $notes = new Input('notes');
126
        $notes->setRequired(false);
127
        $notes->getFilterChain()
128
            ->attach(new StripTags())
129
            ->attach(new StringTrim());
130
131
        $notes->getValidatorChain()
132
            ->attach(new StringLength([
133
                'encoding' => 'UTF-8',
134
                'min'      => 1,
135
                'max'      => 512,
136
            ]));
137
138
        $this->add($notes);
139
    }
140
141
    /**
142
     * @return ValidatorInterface[]
143
     */
144
    protected function getStartTimeValidators()
145
    {
146
        return [
147
            new Date([
148
                'format' => 'H:i'
149
            ]),
150
            new DateStep([
151
                'format'    => 'H:i',
152
                'baseValue' => '00:00',
153
                'step'      => new \DateInterval("PT{$this->config['step']}S"),
154
            ]),
155
        ];
156
    }
157
158
    /**
159
     * @return ValidatorInterface[]
160
     */
161
    protected function getEndTimeValidators()
162
    {
163
        return [
164
            new Date([
165
                'format' => 'H:i'
166
            ]),
167
            new DateStep([
168
                'format'    => 'H:i',
169
                'baseValue' => '00:00',
170
                'step'      => new \DateInterval("PT{$this->config['step']}S"),
171
            ]),
172
        ];
173
    }
174
}
175