Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractTemporalTransformer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 getDbFormat
n/a
0 / 0
n/a
0 / 0
0
 includesTime
n/a
0 / 0
n/a
0 / 0
0
 transformRead
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 transformWrite
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Engine\Application\Transformer;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Engine\Application\Formatter\DateTimeFormatter;
12use App\Core\Engine\Domain\Model\FieldMetadata;
13
14/**
15 * Base Abstract Temporal (Date/Time) Transformer.
16 *
17 * Provides shared temporal read formatting and parsing.
18 *
19 * @package App\Core\Engine\Application\Transformer
20 */
21abstract class AbstractTemporalTransformer implements UiTypeTransformerInterface
22{
23    /**
24     * Returns database storage date/time format string.
25     */
26    abstract protected function getDbFormat(): string;
27
28    /**
29     * Whether temporal field includes time component.
30     */
31    abstract protected function includesTime(): bool;
32
33    /** {@inheritdoc} */
34    public function transformRead(mixed $rawValue, FieldMetadata $field): string
35    {
36        if ($rawValue === null || $rawValue === '') {
37            return '';
38        }
39
40        return DateTimeFormatter::format(
41            $rawValue,
42            $field->getDisplayFormat(),
43            'pl',
44            $this->includesTime()
45        );
46    }
47
48    /** {@inheritdoc} */
49    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
50    {
51        if ($inputValue === null || $inputValue === '') {
52            return null;
53        }
54
55        $date = DateTimeFormatter::parse($inputValue);
56
57        return $date !== null ? $date->format($this->getDbFormat()) : null;
58    }
59}