1<?php
2
3
4namespace ComboStrap\Meta\Api;
5
6
7use ComboStrap\DataType;
8use ComboStrap\ExceptionCompile;
9use ComboStrap\ExceptionNotFound;
10use ComboStrap\ExceptionRuntime;
11use ComboStrap\Meta\Api\Metadata;
12use ComboStrap\Meta\Store\MetadataDokuWikiStore;
13use ComboStrap\MetadataFormDataStore;
14
15/**
16 * Class MetadataBoolean
17 * @package ComboStrap
18 */
19abstract class MetadataBoolean extends Metadata
20{
21
22    /**
23     * @var bool|null
24     */
25    protected $value;
26
27    static public function getDataType(): string
28    {
29        return DataType::BOOLEAN_TYPE_VALUE;
30    }
31
32    /**
33     * @return bool
34     * @throws ExceptionNotFound
35     */
36    public function getValue(): bool
37    {
38        $this->buildCheck();
39        if ($this->value === null) {
40            throw new ExceptionNotFound("No ($this) found");
41        }
42        return $this->value;
43    }
44
45
46    /**
47     * @param null|boolean $value
48     * @return Metadata
49     */
50    public function setValue($value): Metadata
51    {
52        if ($value === null) {
53            $this->value = null;
54            return $this;
55        }
56        if (!is_bool($value)) {
57            throw new ExceptionRuntime("The value is not a boolean: " . var_export($value, true));
58        }
59        $this->value = $value;
60        return $this;
61    }
62
63    public function toStoreDefaultValue()
64    {
65        $store = $this->getWriteStore();
66
67        if ($store instanceof MetadataFormDataStore) {
68            /**
69             * In a boolean form field, the data is returned only when the field is checked.
70             *
71             * By default, this is not checked, therefore, the default value is when this is not the default.
72             * It means that this is the inverse of the default value
73             */
74            try {
75                return !$this->getDefaultValue();
76            } catch (ExceptionNotFound $e) {
77                return null;
78            }
79
80        }
81        return parent::toStoreDefaultValue();
82    }
83
84
85    /**
86     * @return bool|string|null
87     */
88    public function toStoreValue()
89    {
90
91        $store = $this->getWriteStore();
92        try {
93            $value = $this->getValue();
94        } catch (ExceptionNotFound $e) {
95            return null;
96        }
97
98        if ($store instanceof MetadataFormDataStore) {
99            return $value;
100        }
101
102        if ($store instanceof MetadataDokuWikiStore) {
103            // The store modify it
104            return $value;
105        }
106
107        if ($store->isHierarchicalTextBased()) {
108            $value = DataType::toBooleanString($value);
109        }
110
111        return $value;
112
113    }
114
115    /**
116     * @throws ExceptionCompile
117     */
118    public
119    function setFromStoreValue($value): Metadata
120    {
121        $value = $this->toBoolean($value);
122        return $this->setValue($value);
123    }
124
125
126    public
127    function valueIsNotNull(): bool
128    {
129        return $this->value !== null;
130    }
131
132    public
133    function setFromStoreValueWithoutException($value): Metadata
134    {
135        $this->value = $this->toBoolean($value);
136        return $this;
137    }
138
139    private
140    function toBoolean($value): ?bool
141    {
142        /**
143         * TODO: There is no validation
144         * If the value is not a boolean, the return value is false ...
145         */
146        return DataType::toBoolean($value);
147
148    }
149
150
151}
152