Passed
Push — main ( 87fed9...27211a )
by Thierry
04:04
created

HookData::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * HookData.php
5
 *
6
 * Hook 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 json_encode;
21
22
abstract class HookData extends AbstractData
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $aCalls = [];
28
29
    /**
30
     * @return string
31
     */
32
    abstract protected function getType(): string;
33
34
    /**
35
     * @return string
36
     */
37
    public function getName(): string
38
    {
39
        return '__' . $this->getType();
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function getValue(): mixed
46
    {
47
        return $this->aCalls;
48
    }
49
50
    /**
51
     * @param string $sMethod
52
     * @param array $aParams
53
     *
54
     * @return void
55
     */
56
    public function addCall(string $sMethod, array $aParams): void
57
    {
58
        if(!$this->validateMethod($sMethod))
59
        {
60
            $sType = $this->getType();
61
            throw new SetupException("'$sMethod' is not a valid \"call\" value for $sType.");
62
        }
63
        $this->aCalls[$sMethod] = $aParams;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function encode(string $sVarName): array
70
    {
71
        $aCalls = [];
72
        foreach($this->aCalls as $sMethod => $aParams)
73
        {
74
            $sParams = addslashes(json_encode($aParams));
75
            $aCalls[] = "{$sVarName}->addCall('$sMethod', json_decode(\"$sParams\", true));";
76
        }
77
        return $aCalls;
78
    }
79
}
80