Code Duplication    Length = 38-39 lines in 2 locations

src/Forms/TimeField.php 1 location

@@ 162-200 (lines=39) @@
159
     *
160
     * @return IntlDateFormatter
161
     */
162
    protected function getFrontendFormatter()
163
    {
164
        if ($this->getHTML5() && $this->timeFormat && $this->timeFormat !== DBTime::ISO_TIME) {
165
            throw new \LogicException(
166
                'Please opt-out of HTML5 processing of ISO 8601 times via setHTML5(false) if using setTimeFormat()'
167
            );
168
        }
169
170
        if ($this->getHTML5() && $this->timeLength) {
171
            throw new \LogicException(
172
                'Please opt-out of HTML5 processing of ISO 8601 times via setHTML5(false) if using setTimeLength()'
173
            );
174
        }
175
176
        if ($this->getHTML5() && $this->locale) {
177
            throw new \LogicException(
178
                'Please opt-out of HTML5 processing of ISO 8601 times via setHTML5(false) if using setLocale()'
179
            );
180
        }
181
182
        $formatter =  IntlDateFormatter::create(
183
            $this->getLocale(),
184
            IntlDateFormatter::NONE,
185
            $this->getTimeLength(),
186
            $this->getTimezone()
187
        );
188
189
        if ($this->getHTML5()) {
190
            // Browsers expect ISO 8601 times, localisation is handled on the client
191
            $formatter->setPattern(DBTime::ISO_TIME);
192
            // Don't invoke getTimeFormat() directly to avoid infinite loop
193
        } elseif ($this->timeFormat) {
194
            $ok = $formatter->setPattern($this->timeFormat);
195
            if (!$ok) {
196
                throw new InvalidArgumentException("Invalid time format {$this->timeFormat}");
197
            }
198
        }
199
        return $formatter;
200
    }
201
202
    /**
203
     * Get a time formatter for the ISO 8601 format

src/Forms/DateField.php 1 location

@@ 221-258 (lines=38) @@
218
     * @throws \LogicException
219
     * @return IntlDateFormatter
220
     */
221
    protected function getFrontendFormatter()
222
    {
223
        if ($this->getHTML5() && $this->dateFormat && $this->dateFormat !== DBDate::ISO_DATE) {
224
            throw new \LogicException(
225
                'Please opt-out of HTML5 processing of ISO 8601 dates via setHTML5(false) if using setDateFormat()'
226
            );
227
        }
228
229
        if ($this->getHTML5() && $this->dateLength) {
230
            throw new \LogicException(
231
                'Please opt-out of HTML5 processing of ISO 8601 dates via setHTML5(false) if using setDateLength()'
232
            );
233
        }
234
235
        if ($this->getHTML5() && $this->locale) {
236
            throw new \LogicException(
237
                'Please opt-out of HTML5 processing of ISO 8601 dates via setHTML5(false) if using setLocale()'
238
            );
239
        }
240
241
        $formatter = IntlDateFormatter::create(
242
            $this->getLocale(),
243
            $this->getDateLength(),
244
            IntlDateFormatter::NONE
245
        );
246
247
        if ($this->getHTML5()) {
248
            // Browsers expect ISO 8601 dates, localisation is handled on the client
249
            $formatter->setPattern(DBDate::ISO_DATE);
250
        } elseif ($this->dateFormat) {
251
            // Don't invoke getDateFormat() directly to avoid infinite loop
252
            $ok = $formatter->setPattern($this->dateFormat);
253
            if (!$ok) {
254
                throw new InvalidArgumentException("Invalid date format {$this->dateFormat}");
255
            }
256
        }
257
        return $formatter;
258
    }
259
260
    /**
261
     * Get a date formatter for the ISO 8601 format