1<?php
2
3
4namespace ComboStrap;
5
6
7use DateTime;
8
9abstract class MetadataDateTime extends Metadata
10{
11    /**
12     * @var DateTime
13     */
14    protected $dateTimeValue;
15
16
17    /**
18     * Helper function for date metadata
19     * @return array|string|null
20     */
21    public function toStoreValue()
22    {
23
24        $this->buildCheck();
25        $value = $this->getValue();
26        return $this->toPersistentDateTimeUtility($value);
27
28    }
29
30    /**
31     * @param DateTime|null $value
32     * @return $this
33     */
34    public function setValue($value): Metadata
35    {
36        if ($value === null) {
37            $this->dateTimeValue = null;
38            return $this;
39        }
40        if (!($value instanceof DateTime)) {
41            throw new ExceptionComboRuntime("The value is not a date time. Value: " . var_export($value, true));
42        }
43        $this->dateTimeValue = $value;
44        return $this;
45    }
46
47    /**
48     * @throws ExceptionCombo
49     */
50    public function setFromStoreValue($value): Metadata
51    {
52        return $this->setValue($this->fromPersistentDateTimeUtility($value));
53    }
54
55    public function toStoreDefaultValue(): ?string
56    {
57
58        return $this->toPersistentDateTimeUtility($this->getDefaultValue());
59
60    }
61
62    public function getDataType(): string
63    {
64        return DataType::DATETIME_TYPE_VALUE;
65    }
66
67
68    public function buildFromReadStore(): MetadataDateTime
69    {
70        $value = $this->getReadStore()->get($this);
71        try {
72            $this->dateTimeValue = $this->fromPersistentDateTimeUtility($value);
73        } catch (ExceptionCombo $e) {
74            LogUtility::msg($e->getMessage(), $this->getCanonical());
75        }
76        return $this;
77    }
78
79    /**
80     * @throws ExceptionCombo
81     */
82    protected function fromPersistentDateTimeUtility($value)
83    {
84        if ($value === null || $value === "") {
85            return null;
86        }
87        if (!is_string($value)) {
88            throw new ExceptionCombo("This is not a string value");
89        }
90        return Iso8601Date::createFromString($value)->getDateTime();
91    }
92
93    public function getValue(): ?DateTime
94    {
95        $this->buildCheck();
96        return $this->dateTimeValue;
97    }
98
99
100    private function toPersistentDateTimeUtility($value): ?string
101    {
102        if ($value === null) {
103            return null;
104        }
105        if (!($value instanceof DateTime)) {
106            throw new ExceptionComboRuntime("This is not a date time");
107        }
108        return Iso8601Date::createFromDateTime($value)->toString();
109    }
110
111    public function getCanonical(): string
112    {
113        return "date";
114    }
115
116    public function valueIsNotNull(): bool
117    {
118        return $this->dateTimeValue !== null;
119    }
120
121    public function buildFromStoreValue($value): Metadata
122    {
123        try {
124            $this->dateTimeValue = $this->fromPersistentDateTimeUtility($value);
125        } catch (ExceptionCombo $e) {
126            LogUtility::msg($e->getMessage(), LogUtility::LVL_MSG_ERROR, $e->getCanonical());
127        }
128        return $this;
129    }
130
131
132}
133