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

mimmi20 / laminasviewrenderer-bootstrap-form / 12160906017

04 Dec 2024 01:37PM UTC coverage: 97.314%. Remained the same
12160906017

push

github

web-flow
Merge pull request #310 from mimmi20/updates

upgrade to PHP 8.3

60 of 61 new or added lines in 40 files covered. (98.36%)

38 existing lines in 7 files now uncovered.

2174 of 2234 relevant lines covered (97.31%)

25.52 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-2024, 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
/** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */
34
final class FormDateTimeSelect extends BaseFormDateTimeSelect implements FormIndentInterface, FormRenderInterface
35
{
36
    use FormDateSelectTrait;
37
    use FormTrait;
38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

124
        $markups = [];
2✔
125

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

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

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

164
        $result = [];
2✔
165

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

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

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

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

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

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

184
        return $result;
2✔
185
    }
186

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

217
        $result = [];
2✔
218

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

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

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

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

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

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

237
        return $result;
2✔
238
    }
239

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

270
        $result = [];
1✔
271

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

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

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

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

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

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

290
        return $result;
1✔
291
    }
292
}
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

© 2025 Coveralls, Inc