Code Duplication    Length = 23-23 lines in 2 locations

src/Forms/DatetimeField.php 1 location

@@ 181-203 (lines=23) @@
178
     * @param string $datetime
179
     * @return string The formatted date, or null if not a valid date
180
     */
181
    public function frontendToInternal($datetime)
182
    {
183
        if (!$datetime) {
184
            return null;
185
        }
186
        $fromFormatter = $this->getFrontendFormatter();
187
        $toFormatter = $this->getInternalFormatter();
188
189
        // Try to parse time with seconds
190
        $timestamp = $fromFormatter->parse($datetime);
191
192
        // Try to parse time without seconds, since that's a valid HTML5 submission format
193
        // See https://html.spec.whatwg.org/multipage/infrastructure.html#times
194
        if ($timestamp === false && $this->getHTML5()) {
195
            $fromFormatter->setPattern(str_replace(':ss', '', $fromFormatter->getPattern()));
196
            $timestamp = $fromFormatter->parse($datetime);
197
        }
198
199
        if ($timestamp === false) {
200
            return null;
201
        }
202
        return $toFormatter->format($timestamp) ?: null;
203
    }
204
205
    /**
206
     * Get date formatter with the standard locale / date format

src/Forms/TimeField.php 1 location

@@ 384-406 (lines=23) @@
381
     * @param string $time
382
     * @return string The formatted time, or null if not a valid time
383
     */
384
    protected function frontendToInternal($time)
385
    {
386
        if (!$time) {
387
            return null;
388
        }
389
        $fromFormatter = $this->getFrontendFormatter();
390
        $toFormatter = $this->getInternalFormatter();
391
        $timestamp = $fromFormatter->parse($time);
392
393
        // Try to parse time without seconds, since that's a valid HTML5 submission format
394
        // See https://html.spec.whatwg.org/multipage/infrastructure.html#times
395
        if ($timestamp === false && $this->getHTML5()) {
396
            $fromFormatter->setPattern(str_replace(':ss', '', DBTime::ISO_TIME));
397
            $timestamp = $fromFormatter->parse($time);
398
        }
399
400
        // If timestamp still can't be detected, we've got an invalid time
401
        if ($timestamp === false) {
402
            return null;
403
        }
404
405
        return $toFormatter->format($timestamp);
406
    }
407
408
    /**
409
     * Convert the internal time representation (ISO 8601) to a format used by the frontend,