xref: /template/strap/ComboStrap/MetadataSingleArrayStore.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Store\MetadataDokuWikiStore;
9use ComboStrap\Meta\Api\MetadataStoreAbs;
10
11/**
12 * Class MetadataArrayStore
13 * @package ComboStrap
14 * A store based on a single array for a single resource
15 */
16abstract class MetadataSingleArrayStore extends MetadataStoreAbs
17{
18
19
20    /**
21     * @var bool
22     */
23    protected bool $hasChanged = false;
24
25    protected array $data = [];
26
27    /**
28     * MetadataSingleArrayStore constructor.
29     * @param ResourceCombo $page
30     * @param $data
31     */
32    public function __construct(ResourceCombo $page, $data = null)
33    {
34        if ($data !== null) {
35            foreach ($data as $key => $value) {
36                $key = $this->toNormalizedKey($key);
37                $this->data[$key] = $value;
38            }
39        }
40        parent::__construct($page);
41    }
42
43
44    public function set(Metadata $metadata)
45    {
46        $this->checkResource($metadata->getResource());
47        $this->setFromPersistentName($metadata::getPersistentName(), $metadata->toStoreValue());
48    }
49
50    public function get(Metadata $metadata, $default = null)
51    {
52        $this->checkResource($metadata->getResource());
53        $value = $this->data[$metadata::getPersistentName()];
54        if ($value !== null) {
55            return $value;
56        }
57        foreach ($metadata::getOldPersistentNames() as $name) {
58            $value = $this->data[$name];
59            if ($value !== null) {
60                $this->data[$metadata::getPersistentName()] = $value;
61                unset($this->data[$name]);
62                return $value;
63            }
64        }
65        return $default;
66    }
67
68    public function persist()
69    {
70        if (PluginUtility::isDevOrTest()) {
71            throw new ExceptionRuntime("Not yet implemented, use sendToStore");
72        }
73    }
74
75    public function isHierarchicalTextBased(): bool
76    {
77        return true;
78    }
79
80
81    public function getData(): array
82    {
83        return $this->data;
84    }
85
86
87    public function reset()
88    {
89        $this->data = [];
90    }
91
92    public function getFromName(string $name, $default = null)
93    {
94
95        $value = $this->data[$name];
96        if ($value !== null) {
97            return $value;
98        }
99        return $default;
100    }
101
102    public function setFromPersistentName(string $name, $value, $default = null)
103    {
104        $actualValue = $this->data[$name];
105        if ($actualValue !== $value) {
106            $this->hasChanged = true;
107        }
108
109        $name = $this->toNormalizedKey($name);
110        if ($value === null || $value === "") {
111            // remove
112            unset($this->data[$name]);
113            return;
114        }
115        $this->data[$name] = $value;
116
117    }
118
119
120    public function hasProperty(string $name): bool
121    {
122        return isset($this->data[$name]);
123    }
124
125    public function remove(Metadata $metadata): MetadataSingleArrayStore
126    {
127        $this->checkResource($metadata->getResource());
128        $this->setFromPersistentName($metadata->getName(), null);
129        return $this;
130    }
131
132    private function toNormalizedKey(string $key): string
133    {
134        return trim($key);
135    }
136
137    /**
138     * Used to update the data from an other external process
139     * (ie
140     *    {@link MetadataDokuWikiStore::renderAndPersist() metadata renderer}
141     *    or {@link \action_plugin_combo_metamanager::handleViewerPost() metadata manager
142     * )
143     * @param $data
144     */
145    public function setData($data)
146    {
147        if ($data !== $this->data) {
148            $this->hasChanged = true;
149        }
150        $this->data = $data;
151    }
152
153    /**
154     * @return bool - true if the data has changed
155     */
156    public function hasStateChanged(): bool
157    {
158        return $this->hasChanged;
159    }
160
161
162}
163