Passed
Push — main ( 31af8b...652a21 )
by Thierry
04:07
created

Metadata::encode()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 33
rs 9.2888
1
<?php
2
3
/**
4
 * Metadata.php
5
 *
6
 * Jaxon component metadata.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2024 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\App\Metadata;
16
17
use function array_filter;
18
use function array_keys;
19
use function array_values;
20
use function count;
21
22
class Metadata
23
{
24
    /**
25
     * @var array<string, array<string, Data\AbstractData>>
26
     */
27
    private array $aAttributes = [
28
        'exclude' => [],
29
        'container' => [],
30
        'databag' => [],
31
        'callback' => [],
32
        'before' => [],
33
        'after' => [],
34
        'upload' => [],
35
    ];
36
37
    /**
38
     * @return array<string, array<string, Data\AbstractData>>
39
     */
40
    public function getAttributes(): array
41
    {
42
        return $this->aAttributes;
43
    }
44
45
    /**
46
     * @param string $sMethod
47
     *
48
     * @return Data\ExcludeData
49
     */
50
    public function exclude(string $sMethod = '*'): Data\ExcludeData
51
    {
52
        return $this->aAttributes['exclude'][$sMethod] ??
53
            $this->aAttributes['exclude'][$sMethod] = new Data\ExcludeData();
54
    }
55
56
    /**
57
     * @param string $sMethod
58
     *
59
     * @return Data\ContainerData
60
     */
61
    public function container(string $sMethod = '*'): Data\ContainerData
62
    {
63
        return $this->aAttributes['container'][$sMethod] ??
64
            $this->aAttributes['container'][$sMethod] = new Data\ContainerData();
65
    }
66
67
    /**
68
     * @param string $sMethod
69
     *
70
     * @return Data\DatabagData
71
     */
72
    public function databag(string $sMethod = '*'): Data\DatabagData
73
    {
74
        return $this->aAttributes['databag'][$sMethod] ??
75
            $this->aAttributes['databag'][$sMethod] = new Data\DatabagData();
76
    }
77
78
    /**
79
     * @param string $sMethod
80
     *
81
     * @return Data\CallbackData
82
     */
83
    public function callback(string $sMethod = '*'): Data\CallbackData
84
    {
85
        return $this->aAttributes['callback'][$sMethod] ??
86
            $this->aAttributes['callback'][$sMethod] = new Data\CallbackData();
87
    }
88
89
    /**
90
     * @param string $sMethod
91
     *
92
     * @return Data\BeforeData
93
     */
94
    public function before(string $sMethod = '*'): Data\BeforeData
95
    {
96
        return $this->aAttributes['before'][$sMethod] ??
97
            $this->aAttributes['before'][$sMethod] = new Data\BeforeData();
98
    }
99
100
    /**
101
     * @param string $sMethod
102
     *
103
     * @return Data\AfterData
104
     */
105
    public function after(string $sMethod = '*'): Data\AfterData
106
    {
107
        return $this->aAttributes['after'][$sMethod] ??
108
            $this->aAttributes['after'][$sMethod] = new Data\AfterData();
109
    }
110
111
    /**
112
     * @param string $sMethod
113
     *
114
     * @return Data\UploadData
115
     */
116
    public function upload(string $sMethod = '*'): Data\UploadData
117
    {
118
        return $this->aAttributes['upload'][$sMethod] ??
119
            $this->aAttributes['upload'][$sMethod] = new Data\UploadData();
120
    }
121
122
    /**
123
     * True if the class is excluded
124
     *
125
     * @return bool
126
     */
127
    public function isExcluded(): bool
128
    {
129
        $xData = $this->aAttributes['exclude']['*'] ?? null;
130
        return $xData !== null && $xData->getValue() === true;
131
    }
132
133
    /**
134
     * Get the properties of the class methods
135
     *
136
     * @return array
137
     */
138
    public function getProperties(): array
139
    {
140
        $aProperties = [];
141
        $aClassProperties = [];
142
        foreach($this->aAttributes as $sType => $aValues)
143
        {
144
            if($sType === 'exclude')
145
            {
146
                continue;
147
            }
148
149
            foreach($aValues as $sMethod => $xData)
150
            {
151
                if($sMethod === '*')
152
                {
153
                    $aClassProperties[$xData->getName()] = $xData->getValue();
154
                    continue;
155
                }
156
                $aProperties[$sMethod][$xData->getName()] = $xData->getValue();
157
            }
158
        }
159
160
        if(count($aClassProperties) > 0)
161
        {
162
            $aProperties['*'] = $aClassProperties;
163
        }
164
165
        return $aProperties;
166
    }
167
168
    /**
169
     * Get the protected methods
170
     *
171
     * @return array
172
     */
173
    public function getProtectedMethods(): array
174
    {
175
        /** @var array<Data\ExcludeData> */
176
        $aAttributes = $this->aAttributes['exclude'];
177
        $aMethods = array_keys($aAttributes);
178
        return array_values(array_filter($aMethods, fn(string $sName) =>
179
            $sName !== '*' && $aAttributes[$sName]->getValue() === true));
180
    }
181
}
182