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