Passed
Push — main ( c6b105...31af8b )
by Thierry
07:02
created

Metadata   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 70
c 2
b 0
f 0
dl 0
loc 236
rs 10
wmc 28

11 Methods

Rating   Name   Duplication   Size   Complexity  
A callback() 0 7 2
A before() 0 7 2
A after() 0 7 2
A databag() 0 7 2
A container() 0 7 2
A upload() 0 7 2
A exclude() 0 7 2
A isExcluded() 0 3 2
A encode() 0 33 5
A getProtectedMethods() 0 5 2
A getProperties() 0 33 5
1
<?php
2
3
/**
4
 * Metadata.php
5
 *
6
 * Callable class 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 implements MetadataInterface
23
{
24
    /**
25
     * @var array<Data\ExcludeData>
26
     */
27
    private array $aExcludes = [];
28
29
    /**
30
     * @var array<Data\ContainerData>
31
     */
32
    private array $aContainers = [];
33
34
    /**
35
     * @var array<Data\DatabagData>
36
     */
37
    private array $aDatabags = [];
38
39
    /**
40
     * @var array<Data\CallbackData>
41
     */
42
    private array $aCallbacks = [];
43
44
    /**
45
     * @var array<Data\BeforeData>
46
     */
47
    private array $aBefores = [];
48
49
    /**
50
     * @var array<Data\AfterData>
51
     */
52
    private array $aAfters = [];
53
54
    /**
55
     * @var array<Data\UploadData>
56
     */
57
    private array $aUploads = [];
58
59
    // /**
60
    //  * @param bool $bIsExcluded
61
    //  * @param array $aProperties
62
    //  * @param array $aProtectedMethods
63
    //  */
64
    // public function __construct(private bool $bIsExcluded,
65
    //     private array $aProperties, private array $aProtectedMethods)
66
    // {}
67
68
    /**
69
     * @param string $sMethod
70
     *
71
     * @return Data\ExcludeData
72
     */
73
    public function exclude(string $sMethod = '*'): Data\ExcludeData
74
    {
75
        if(!isset($this->aExcludes[$sMethod]))
76
        {
77
            $this->aExcludes[$sMethod] = new Data\ExcludeData();
78
        }
79
        return $this->aExcludes[$sMethod];
80
    }
81
82
    /**
83
     * @param string $sMethod
84
     *
85
     * @return Data\ContainerData
86
     */
87
    public function container(string $sMethod = '*'): Data\ContainerData
88
    {
89
        if(!isset($this->aContainers[$sMethod]))
90
        {
91
            $this->aContainers[$sMethod] = new Data\ContainerData();
92
        }
93
        return $this->aContainers[$sMethod];
94
    }
95
96
    /**
97
     * @param string $sMethod
98
     *
99
     * @return Data\DatabagData
100
     */
101
    public function databag(string $sMethod = '*'): Data\DatabagData
102
    {
103
        if(!isset($this->aDatabags[$sMethod]))
104
        {
105
            $this->aDatabags[$sMethod] = new Data\DatabagData();
106
        }
107
        return $this->aDatabags[$sMethod];
108
    }
109
110
    /**
111
     * @param string $sMethod
112
     *
113
     * @return Data\CallbackData
114
     */
115
    public function callback(string $sMethod = '*'): Data\CallbackData
116
    {
117
        if(!isset($this->aCallbacks[$sMethod]))
118
        {
119
            $this->aCallbacks[$sMethod] = new Data\CallbackData();
120
        }
121
        return $this->aCallbacks[$sMethod];
122
    }
123
124
    /**
125
     * @param string $sMethod
126
     *
127
     * @return Data\BeforeData
128
     */
129
    public function before(string $sMethod = '*'): Data\BeforeData
130
    {
131
        if(!isset($this->aBefores[$sMethod]))
132
        {
133
            $this->aBefores[$sMethod] = new Data\BeforeData();
134
        }
135
        return $this->aBefores[$sMethod];
136
    }
137
138
    /**
139
     * @param string $sMethod
140
     *
141
     * @return Data\AfterData
142
     */
143
    public function after(string $sMethod = '*'): Data\AfterData
144
    {
145
        if(!isset($this->aAfters[$sMethod]))
146
        {
147
            $this->aAfters[$sMethod] = new Data\AfterData();
148
        }
149
        return $this->aAfters[$sMethod];
150
    }
151
152
    /**
153
     * @param string $sMethod
154
     *
155
     * @return Data\UploadData
156
     */
157
    public function upload(string $sMethod = '*'): Data\UploadData
158
    {
159
        if(!isset($this->aUploads[$sMethod]))
160
        {
161
            $this->aUploads[$sMethod] = new Data\UploadData();
162
        }
163
        return $this->aUploads[$sMethod];
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function isExcluded(): bool
170
    {
171
        return isset($this->aExcludes['*']) && $this->aExcludes['*']->getValue() === true;
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function getProperties(): array
178
    {
179
        $aAttributes = [
180
            // $this->aExcludes,
181
            $this->aContainers,
182
            $this->aDatabags,
183
            $this->aCallbacks,
184
            $this->aBefores,
185
            $this->aAfters,
186
            $this->aUploads,
187
        ];
188
        $aProperties = [];
189
        $aClassProperties = [];
190
191
        foreach($aAttributes as $aValues)
192
        {
193
            foreach($aValues as $sMethod => $xData)
194
            {
195
                if($sMethod === '*')
196
                {
197
                    $aClassProperties[$xData->getName()] = $xData->getValue();
198
                    continue;
199
                }
200
                $aProperties[$sMethod][$xData->getName()] = $xData->getValue();
201
            }
202
        }
203
204
        if(count($aClassProperties) > 0)
205
        {
206
            $aProperties['*'] = $aClassProperties;
207
        }
208
209
        return $aProperties;
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    public function getProtectedMethods(): array
216
    {
217
        $aMethods = array_keys($this->aExcludes);
218
        return array_values(array_filter($aMethods, fn(string $sName) =>
219
            $sName !== '*' && $this->aExcludes[$sName]->getValue() === true));
220
    }
221
222
    /**
223
     * @return array
224
     */
225
    public function encode(): array
226
    {
227
        $aAttributes = [
228
            'exclude' => $this->aExcludes,
229
            'container' => $this->aContainers,
230
            'databag' => $this->aDatabags,
231
            'callback' => $this->aCallbacks,
232
            'before' => $this->aBefores,
233
            'after' => $this->aAfters,
234
            'upload' => $this->aUploads,
235
        ];
236
237
        $sVar = '$'; // The dollar car.
238
        $aCalls = [
239
            "{$sVar}xMetadata = new " . Metadata::class . '();'
240
        ];
241
        foreach($aAttributes as $sAttr => $aValues)
242
        {
243
            if(count($aValues) === 0)
244
            {
245
                continue;
246
            }
247
            foreach($aValues as $sMethod => $xData)
248
            {
249
                $aCalls[] = "{$sVar}xData = {$sVar}xMetadata->{$sAttr}('$sMethod');";
250
                foreach($xData->encode("{$sVar}xData") as $sCall)
251
                {
252
                    $aCalls[] = $sCall;
253
                }
254
            }
255
        }
256
        $aCalls[] = "return {$sVar}xMetadata;";
257
        return $aCalls;
258
    }
259
}
260