1<?php
2
3
4namespace ComboStrap;
5
6/**
7 * Implement all default method for a text metadata (ie small string without any paragraph such as name, title, ...)
8 * Class MetadataText
9 * @package ComboStrap
10 */
11abstract class MetadataText extends Metadata
12{
13
14    /**
15     * @var string|null
16     */
17    protected $value;
18
19
20    public function getDataType(): string
21    {
22        return DataType::TEXT_TYPE_VALUE;
23    }
24
25    public function getValue(): ?string
26    {
27        $this->buildCheck();
28        return $this->value;
29    }
30
31    public function valueIsNotNull(): bool
32    {
33        return $this->value !== null;
34    }
35
36
37    /**
38     * @param null|string $value
39     * @return $this
40     * @throws ExceptionCombo
41     */
42    public function setValue($value): Metadata
43    {
44        if ($value !== null && !is_string($value)) {
45            throw new ExceptionCombo("The value of the metadata ($this) is not a string", $this->getCanonical());
46        }
47        $value = trim($value);
48        if ($value === "") {
49            /**
50             * TODO: move this into the function {@link MetadataText::buildFromStoreValue()} ??
51             *   form don't return null only empty string
52             *   equivalent to null
53             */
54            return $this;
55        }
56        $possibleValues = $this->getPossibleValues();
57        if ($possibleValues !== null && $value !== null && $value !== "") {
58            if (!in_array($value, $possibleValues)) {
59                throw new ExceptionCombo("The value ($value) for the metadata ({$this->getName()}) is not one of the possible following values: " . implode(", ", $possibleValues) . ".");
60            }
61        }
62        $this->value = $value;
63        return $this;
64
65    }
66
67    /**
68     * @throws ExceptionCombo
69     */
70    public function setFromStoreValue($value): Metadata
71    {
72        return $this->setValue($value);
73    }
74
75    public function buildFromStoreValue($value): Metadata
76    {
77        if ($value === null || $value === "") {
78            $this->value = null;
79            return $this;
80        }
81        if (!is_string($value)) {
82            LogUtility::msg("This value of a text metadata is not a string. " . var_export($value, true));
83            return $this;
84        }
85        $this->value = $value;
86        return $this;
87    }
88
89    public function getDefaultValue()
90    {
91        return null;
92    }
93
94
95
96
97}
98