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