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

DatabagData::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
 * DatabagData.php
5
 *
6
 * Databag 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 array_map;
20
use function preg_match;
21
22
class DatabagData extends AbstractData
23
{
24
    /**
25
     * The databag names
26
     *
27
     * @var string
28
     */
29
    protected $aNames = [];
30
31
    /**
32
     * @return string
33
     */
34
    public function getName(): string
35
    {
36
        return 'bags';
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function getValue(): mixed
43
    {
44
        return $this->aNames;
45
    }
46
47
    /**
48
     * @param string $sName
49
     *
50
     * @return void
51
     */
52
    protected function validateName(string $sName): void
53
    {
54
        if(preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sName) > 0)
55
        {
56
            return;
57
        }
58
        throw new SetupException("$sName is not a valid \"name\" value for databag");
59
    }
60
61
    /**
62
     * @param string $sName
63
     *
64
     * @return void
65
     */
66
    public function addValue(string $sName): void
67
    {
68
        $this->validateName($sName);
69
70
        $this->aNames[] = $sName;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function encode(string $sVarName): array
77
    {
78
        return array_map(fn($sName) => "{$sVarName}->addValue('$sName');", $this->aNames);
0 ignored issues
show
Bug introduced by
$this->aNames of type string is incompatible with the type array expected by parameter $array of array_map(). ( Ignorable by Annotation )

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

78
        return array_map(fn($sName) => "{$sVarName}->addValue('$sName');", /** @scrutinizer ignore-type */ $this->aNames);
Loading history...
79
    }
80
}
81