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

ExportData::getValue()   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
 * ExportData.php
5
 *
6
 * Export metadata for Jaxon classes.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2025 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\Data;
16
17
use Jaxon\Exception\SetupException;
18
19
use function addslashes;
20
use function is_string;
21
use function json_encode;
22
23
class ExportData extends AbstractData
24
{
25
    /**
26
     * @var array<string, array<string>>
27
     */
28
    private array $aMethods = [];
29
30
    /**
31
     * @return string
32
     */
33
    public function getName(): string
34
    {
35
        return 'export';
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function getValue(): mixed
42
    {
43
        return $this->aMethods;
44
    }
45
46
    /**
47
     * @param array $aMethods
48
     *
49
     * @return void
50
     */
51
    public function setMethods(array $aMethods): void
52
    {
53
        foreach(['base', 'only', 'except'] as $sKey)
54
        {
55
            foreach($aMethods[$sKey] ?? [] as $sMethod)
56
            {
57
                if(!is_string($sMethod) || !$this->validateMethod($sMethod))
58
                {
59
                    throw new SetupException("'$sMethod' is not a valid method name.");
60
                }
61
            }
62
        }
63
        $this->aMethods = $aMethods;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function encode(string $sVarName): array
70
    {
71
        $sMethods = addslashes(json_encode($this->aMethods));
72
        return ["{$sVarName}->setMethods(json_decode(\"$sMethods\", true));"];
73
    }
74
}
75