• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mimmi20 / laminasviewrenderer-bootstrap-form / 12571184477

01 Jan 2025 01:36PM UTC coverage: 97.361% (+0.05%) from 97.314%
12571184477

push

github

mimmi20
fix tests

2177 of 2236 relevant lines covered (97.36%)

25.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/src/FormDateTimeSelect.php
1
<?php
2

3
/**
4
 * This file is part of the mimmi20/laminasviewrenderer-bootstrap-form package.
5
 *
6
 * Copyright (c) 2021-2025, Thomas Mueller <mimmi20@live.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types = 1);
13

14
namespace Mimmi20\LaminasView\BootstrapForm;
15

16
use DateTime;
17
use IntlDateFormatter;
18
use Laminas\Form\Element\DateTimeSelect as DateTimeSelectElement;
19
use Laminas\Form\ElementInterface;
20
use Laminas\Form\Exception\DomainException;
21
use Laminas\Form\Exception\InvalidArgumentException;
22
use Laminas\Form\View\Helper\FormDateTimeSelect as BaseFormDateTimeSelect;
23
use Override;
24

25
use function array_key_exists;
26
use function get_debug_type;
27
use function implode;
28
use function is_numeric;
29
use function sprintf;
30

31
use const PHP_EOL;
32

33
final class FormDateTimeSelect extends BaseFormDateTimeSelect implements FormIndentInterface, FormRenderInterface
34
{
35
    use FormDateSelectTrait;
36
    use FormTrait;
37

38
    /**
39
     * Render a date element that is composed of six selects
40
     *
41
     * @throws InvalidArgumentException
42
     * @throws DomainException
43
     */
44
    #[Override]
6✔
45
    public function render(ElementInterface $element): string
46
    {
47
        if (!$element instanceof DateTimeSelectElement) {
6✔
48
            throw new InvalidArgumentException(
1✔
49
                sprintf(
1✔
50
                    '%s requires that the element is of type %s, but was %s',
1✔
51
                    __METHOD__,
1✔
52
                    DateTimeSelectElement::class,
1✔
53
                    get_debug_type($element),
1✔
54
                ),
1✔
55
            );
1✔
56
        }
57

58
        $name = $element->getName();
5✔
59

60
        if ($name === null || $name === '') {
5✔
61
            throw new DomainException(
3✔
62
                sprintf(
3✔
63
                    '%s requires that the element has an assigned name; none discovered',
3✔
64
                    __METHOD__,
3✔
65
                ),
3✔
66
            );
3✔
67
        }
68

69
        $shouldRenderDelimiters = $element->shouldRenderDelimiters();
2✔
70
        $selectHelper           = $this->getSelectElementHelper();
2✔
71
        $pattern                = $this->parsePattern($shouldRenderDelimiters);
2✔
72

73
        $daysOptions   = $this->getDaysOptions($pattern['day']);
2✔
74
        $monthsOptions = $this->getMonthsOptions($pattern['month']);
2✔
75
        $yearOptions   = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
2✔
76
        $hourOptions   = $this->getHoursOptions($pattern['hour']);
2✔
77
        $minuteOptions = $this->getMinutesOptions($pattern['minute']);
2✔
78

79
        $dayElement    = $element->getDayElement()->setValueOptions($daysOptions);
2✔
80
        $monthElement  = $element->getMonthElement()->setValueOptions($monthsOptions);
2✔
81
        $yearElement   = $element->getYearElement()->setValueOptions($yearOptions);
2✔
82
        $hourElement   = $element->getHourElement()->setValueOptions($hourOptions);
2✔
83
        $minuteElement = $element->getMinuteElement()->setValueOptions($minuteOptions);
2✔
84

85
        if ($element->shouldCreateEmptyOption()) {
2✔
86
            $dayElement->setEmptyOption('');
2✔
87
            $yearElement->setEmptyOption('');
2✔
88
            $monthElement->setEmptyOption('');
2✔
89
            $hourElement->setEmptyOption('');
2✔
90
            $minuteElement->setEmptyOption('');
2✔
91
        }
92

93
        $indent = $this->getIndent();
2✔
94

95
        if ($selectHelper instanceof FormIndentInterface) {
2✔
96
            $selectHelper->setIndent($indent);
2✔
97
        }
98

99
        $data                     = [];
2✔
100
        $data[$pattern['day']]    = $selectHelper->render($dayElement);
2✔
101
        $data[$pattern['month']]  = $selectHelper->render($monthElement);
2✔
102
        $data[$pattern['year']]   = $selectHelper->render($yearElement);
2✔
103
        $data[$pattern['hour']]   = $selectHelper->render($hourElement);
2✔
104
        $data[$pattern['minute']] = $selectHelper->render($minuteElement);
2✔
105

106
        if ($element->shouldShowSeconds() && array_key_exists('second', $pattern)) {
2✔
107
            $secondOptions = $this->getSecondsOptions($pattern['second']);
1✔
108
            $secondElement = $element->getSecondElement()->setValueOptions($secondOptions);
1✔
109

110
            if ($element->shouldCreateEmptyOption()) {
1✔
111
                $secondElement->setEmptyOption('');
1✔
112
            }
113

114
            $data[$pattern['second']] = $selectHelper->render($secondElement);
1✔
115
        } else {
116
            unset($pattern['second']);
1✔
117

118
            if ($shouldRenderDelimiters) {
1✔
119
                unset($pattern[4]);
1✔
120
            }
121
        }
122

123
        $markups = [];
2✔
124

125
        foreach ($pattern as $key => $value) {
2✔
126
            // Delimiter
127
            $markups[] = is_numeric($key) ? $indent . $value : $data[$value];
2✔
128
        }
129

130
        return $indent . PHP_EOL . implode(PHP_EOL, $markups) . PHP_EOL . $indent;
2✔
131
    }
132

133
    /**
134
     * Create a key => value options for hours
135
     *
136
     * @param string $pattern Pattern to use for hours
137
     *
138
     * @return array<int|string, array<string, string>>
139
     *
140
     * @throws void
141
     */
142
    #[Override]
2✔
143
    protected function getHoursOptions(string $pattern): array
144
    {
145
        $keyFormatter   = new IntlDateFormatter(
2✔
146
            $this->getLocale(),
2✔
147
            IntlDateFormatter::NONE,
2✔
148
            IntlDateFormatter::NONE,
2✔
149
            null,
2✔
150
            null,
2✔
151
            'HH',
2✔
152
        );
2✔
153
        $valueFormatter = new IntlDateFormatter(
2✔
154
            $this->getLocale(),
2✔
155
            IntlDateFormatter::NONE,
2✔
156
            IntlDateFormatter::NONE,
2✔
157
            null,
2✔
158
            null,
2✔
159
            $pattern,
2✔
160
        );
2✔
161
        $date           = new DateTime('1970-01-01 00:00:00');
2✔
162

163
        $result = [];
2✔
164

165
        for ($hour = 1; 24 >= $hour; ++$hour) {
2✔
166
            $key = $keyFormatter->format($date);
2✔
167

168
            if ($key === false) {
2✔
169
                continue;
×
170
            }
171

172
            $value = $valueFormatter->format($date);
2✔
173

174
            if ($value === false) {
2✔
175
                continue;
×
176
            }
177

178
            $result[$key] = ['value' => $key, 'label' => $value];
2✔
179

180
            $date->modify('+1 hour');
2✔
181
        }
182

183
        return $result;
2✔
184
    }
185

186
    /**
187
     * Create a key => value options for minutes
188
     *
189
     * @param string $pattern Pattern to use for minutes
190
     *
191
     * @return array<int|string, array<string, string>>
192
     *
193
     * @throws void
194
     */
195
    #[Override]
2✔
196
    protected function getMinutesOptions(string $pattern): array
197
    {
198
        $keyFormatter   = new IntlDateFormatter(
2✔
199
            $this->getLocale(),
2✔
200
            IntlDateFormatter::NONE,
2✔
201
            IntlDateFormatter::NONE,
2✔
202
            null,
2✔
203
            null,
2✔
204
            'mm',
2✔
205
        );
2✔
206
        $valueFormatter = new IntlDateFormatter(
2✔
207
            $this->getLocale(),
2✔
208
            IntlDateFormatter::NONE,
2✔
209
            IntlDateFormatter::NONE,
2✔
210
            null,
2✔
211
            null,
2✔
212
            $pattern,
2✔
213
        );
2✔
214
        $date           = new DateTime('1970-01-01 00:00:00');
2✔
215

216
        $result = [];
2✔
217

218
        for ($min = 1; 60 >= $min; ++$min) {
2✔
219
            $key = $keyFormatter->format($date);
2✔
220

221
            if ($key === false) {
2✔
222
                continue;
×
223
            }
224

225
            $value = $valueFormatter->format($date);
2✔
226

227
            if ($value === false) {
2✔
228
                continue;
×
229
            }
230

231
            $result[$key] = ['value' => $key, 'label' => $value];
2✔
232

233
            $date->modify('+1 minute');
2✔
234
        }
235

236
        return $result;
2✔
237
    }
238

239
    /**
240
     * Create a key => value options for seconds
241
     *
242
     * @param string $pattern Pattern to use for seconds
243
     *
244
     * @return array<int|string, array<string, string>>
245
     *
246
     * @throws void
247
     */
248
    #[Override]
1✔
249
    protected function getSecondsOptions(string $pattern): array
250
    {
251
        $keyFormatter   = new IntlDateFormatter(
1✔
252
            $this->getLocale(),
1✔
253
            IntlDateFormatter::NONE,
1✔
254
            IntlDateFormatter::NONE,
1✔
255
            null,
1✔
256
            null,
1✔
257
            'ss',
1✔
258
        );
1✔
259
        $valueFormatter = new IntlDateFormatter(
1✔
260
            $this->getLocale(),
1✔
261
            IntlDateFormatter::NONE,
1✔
262
            IntlDateFormatter::NONE,
1✔
263
            null,
1✔
264
            null,
1✔
265
            $pattern,
1✔
266
        );
1✔
267
        $date           = new DateTime('1970-01-01 00:00:00');
1✔
268

269
        $result = [];
1✔
270

271
        for ($sec = 1; 60 >= $sec; ++$sec) {
1✔
272
            $key = $keyFormatter->format($date);
1✔
273

274
            if ($key === false) {
1✔
275
                continue;
×
276
            }
277

278
            $value = $valueFormatter->format($date);
1✔
279

280
            if ($value === false) {
1✔
281
                continue;
×
282
            }
283

284
            $result[$key] = ['value' => $key, 'label' => $value];
1✔
285

286
            $date->modify('+1 second');
1✔
287
        }
288

289
        return $result;
1✔
290
    }
291
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc