Passed
Push — main ( 27211a...7f530d )
by Thierry
03:53
created

Metadata::getExportOnlyMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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_merge;
20
use function array_values;
21
use function count;
22
23
class Metadata
24
{
25
    /**
26
     * @var array<string, array<string, Data\AbstractData>>
27
     */
28
    private array $aAttributes = [
29
        'exclude' => [],
30
        'export' => [],
31
        'container' => [],
32
        'databag' => [],
33
        'callback' => [],
34
        'before' => [],
35
        'after' => [],
36
        'upload' => [],
37
    ];
38
39
    /**
40
     * @return array<string, array<string, Data\AbstractData>>
41
     */
42
    public function getAttributes(): array
43
    {
44
        return $this->aAttributes;
45
    }
46
47
    /**
48
     * @param string $sMethod
49
     *
50
     * @return Data\ExcludeData
51
     */
52
    public function exclude(string $sMethod = '*'): Data\ExcludeData
53
    {
54
        return $this->aAttributes['exclude'][$sMethod] ??
55
            $this->aAttributes['exclude'][$sMethod] = new Data\ExcludeData();
56
    }
57
58
    /**
59
     * @param string $sMethod
60
     *
61
     * @return Data\ExportData
62
     */
63
    public function export(string $sMethod = '*'): Data\ExportData
0 ignored issues
show
Unused Code introduced by
The parameter $sMethod is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function export(/** @scrutinizer ignore-unused */ string $sMethod = '*'): Data\ExportData

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        $sMethod = '*'; // On classes only
66
        return $this->aAttributes['export'][$sMethod] ??
67
            $this->aAttributes['export'][$sMethod] = new Data\ExportData();
68
    }
69
70
    /**
71
     * @param string $sMethod
72
     *
73
     * @return Data\ContainerData
74
     */
75
    public function container(string $sMethod = '*'): Data\ContainerData
76
    {
77
        return $this->aAttributes['container'][$sMethod] ??
78
            $this->aAttributes['container'][$sMethod] = new Data\ContainerData();
79
    }
80
81
    /**
82
     * @param string $sMethod
83
     *
84
     * @return Data\DatabagData
85
     */
86
    public function databag(string $sMethod = '*'): Data\DatabagData
87
    {
88
        return $this->aAttributes['databag'][$sMethod] ??
89
            $this->aAttributes['databag'][$sMethod] = new Data\DatabagData();
90
    }
91
92
    /**
93
     * @param string $sMethod
94
     *
95
     * @return Data\CallbackData
96
     */
97
    public function callback(string $sMethod = '*'): Data\CallbackData
98
    {
99
        return $this->aAttributes['callback'][$sMethod] ??
100
            $this->aAttributes['callback'][$sMethod] = new Data\CallbackData();
101
    }
102
103
    /**
104
     * @param string $sMethod
105
     *
106
     * @return Data\BeforeData
107
     */
108
    public function before(string $sMethod = '*'): Data\BeforeData
109
    {
110
        return $this->aAttributes['before'][$sMethod] ??
111
            $this->aAttributes['before'][$sMethod] = new Data\BeforeData();
112
    }
113
114
    /**
115
     * @param string $sMethod
116
     *
117
     * @return Data\AfterData
118
     */
119
    public function after(string $sMethod = '*'): Data\AfterData
120
    {
121
        return $this->aAttributes['after'][$sMethod] ??
122
            $this->aAttributes['after'][$sMethod] = new Data\AfterData();
123
    }
124
125
    /**
126
     * @param string $sMethod
127
     *
128
     * @return Data\UploadData
129
     */
130
    public function upload(string $sMethod = '*'): Data\UploadData
131
    {
132
        return $this->aAttributes['upload'][$sMethod] ??
133
            $this->aAttributes['upload'][$sMethod] = new Data\UploadData();
134
    }
135
136
    /**
137
     * True if the class is excluded
138
     *
139
     * @return bool
140
     */
141
    public function isExcluded(): bool
142
    {
143
        $xData = $this->aAttributes['exclude']['*'] ?? null;
144
        return $xData !== null && $xData->getValue() === true;
145
    }
146
147
    /**
148
     * Get the properties of the class methods
149
     *
150
     * @return array
151
     */
152
    public function getProperties(): array
153
    {
154
        $aProperties = [];
155
        $aClassProperties = [];
156
        foreach($this->aAttributes as $sType => $aValues)
157
        {
158
            if($sType === 'exclude')
159
            {
160
                continue;
161
            }
162
163
            foreach($aValues as $sMethod => $xData)
164
            {
165
                if($sMethod === '*')
166
                {
167
                    $aClassProperties[$xData->getName()] = $xData->getValue();
168
                    continue;
169
                }
170
                $aProperties[$sMethod][$xData->getName()] = $xData->getValue();
171
            }
172
        }
173
174
        if(count($aClassProperties) > 0)
175
        {
176
            $aProperties['*'] = $aClassProperties;
177
        }
178
179
        return $aProperties;
180
    }
181
182
    /**
183
     * Get the methods in the export attributes
184
     *
185
     * @return array
186
     */
187
    public function getExportMethods(): array
188
    {
189
        /** @var array<Data\ExcludeData> */
190
        $aAttributes = $this->aAttributes['exclude'];
191
        $aExcludeMethods = array_keys($aAttributes);
192
        $aExcludeMethods = array_values(array_filter($aExcludeMethods,
193
            fn(string $sName) => $sName !== '*' &&
194
                $aAttributes[$sName]->getValue() === true));
195
196
        /** @var Data\ExportData */
197
        $xExportData = $this->aAttributes['export']['*'] ?? null;
198
        $aExportMethods = $xExportData?->getValue() ?? [];
199
200
        $aExceptMethods = $aExportMethods['except'] ?? [];
201
        $aExportMethods['except'] = array_merge($aExcludeMethods, $aExceptMethods);
202
        return $aExportMethods;
203
    }
204
205
    /**
206
     * Get the exluded methods
207
     *
208
     * @return array
209
     */
210
    public function getExceptMethods(): array
211
    {
212
        return $this->getExportMethods()['except'];
213
    }
214
215
    /**
216
     * Get the export base methods
217
     *
218
     * @return array
219
     */
220
    public function getExportBaseMethods(): array
221
    {
222
        return $this->getExportMethods()['base'] ?? [];
223
    }
224
225
    /**
226
     * Get the export only methods
227
     *
228
     * @return array
229
     */
230
    public function getExportOnlyMethods(): array
231
    {
232
        return $this->getExportMethods()['only'] ?? [];
233
    }
234
}
235