1<?php
2
3
4namespace ComboStrap;
5
6use ComboStrap\Meta\Api\Metadata;
7use ComboStrap\Meta\Api\MetadataStore;
8
9/**
10 * Class MetadataFormStore
11 * @package ComboStrap
12 * Represents a data array of a post from an HTML form
13 * ie formData
14 */
15class MetadataFormDataStore extends MetadataSingleArrayStore
16{
17
18
19    public static function getOrCreateFromResource(ResourceCombo $resourceCombo, array $formData = []): MetadataStore
20    {
21        return new MetadataFormDataStore($resourceCombo, $formData);
22    }
23
24
25    public function get(Metadata $metadata, $default = null)
26    {
27        $this->checkResource($metadata->getResource());
28
29        $type = $metadata->getDataType();
30        switch ($type) {
31            case DataType::TABULAR_TYPE_VALUE:
32                /**
33                 * In a tabular, the children name are
34                 */
35                $value = null;
36                foreach ($metadata->getChildrenObject() as $childrenObject) {
37                    $childrenValue = $this->data[$childrenObject::getName()];
38                    if ($childrenValue !== null) {
39                        $value[$childrenObject::getPersistentName()] = $childrenValue;
40                    }
41                }
42                if ($value !== null) {
43                    return $value;
44                }
45                break;
46            default:
47                /**
48                 * In a form, the name is send, not the {@link Metadata::getPersistentName()}
49                 * but with the name
50                 */
51                $value = $this->data[$metadata::getName()] ?? null;
52                if ($value !== null) {
53                    return $value;
54                }
55        }
56
57        return $default;
58    }
59
60    public function isHierarchicalTextBased(): bool
61    {
62        /**
63         * It's a list of field that is not hiearchical
64         */
65        return false;
66    }
67
68}
69