Command   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 13
dl 0
loc 91
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 3 1
A offsetGet() 0 3 1
A offsetExists() 0 3 1
A offsetSet() 0 2 1
A __construct() 0 3 1
A setComponent() 0 4 1
A offsetUnset() 0 2 1
A setOption() 0 9 2
A str() 0 3 1
1
<?php
2
3
/**
4
 * Command.php
5
 *
6
 * This class represents a command in a Jaxon response.
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\Response;
16
17
use ArrayAccess;
18
use JsonSerializable;
19
20
use function trim;
21
22
class Command implements ArrayAccess, JsonSerializable
23
{
24
    /**
25
     * @var array
26
     */
27
    private $aCommand;
28
29
    public function __construct(array $aCommand)
30
    {
31
        $this->aCommand = $aCommand;
32
    }
33
34
    /**
35
     * Set a component on the command
36
     *
37
     * @param array $aComponent
38
     *
39
     * @return Command
40
     */
41
    public function setComponent(array $aComponent): Command
42
    {
43
        $this->aCommand['component'] = $aComponent;
44
        return $this;
45
    }
46
47
    /**
48
     * Convert to string
49
     *
50
     * @param mixed $xData
51
     *
52
     * @return string
53
     */
54
    private function str($xData): string
55
    {
56
        return trim((string)$xData, " \t\n");
57
    }
58
59
    /**
60
     * Add an option to the command
61
     *
62
     * @param string $sName    The option name
63
     * @param string|array|JsonSerializable $xValue    The option value
64
     *
65
     * @return Command
66
     */
67
    public function setOption(string $sName, string|array|JsonSerializable $xValue): Command
68
    {
69
        if(isset($this->aCommand['options']))
70
        {
71
            $this->aCommand['options'][$this->str($sName)] = $xValue;
72
            return $this;
73
        }
74
        $this->aCommand['options'] = [$this->str($sName) => $xValue];
75
        return $this;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function jsonSerialize(): array
82
    {
83
        return $this->aCommand;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function offsetExists($offset): bool
90
    {
91
        return isset($this->aCommand[$offset]);
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function offsetGet($offset): mixed
98
    {
99
        return $this->aCommand[$offset] ?? null;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function offsetSet($offset, $value): void
106
    {} // Not implemented
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function offsetUnset($offset): void
112
    {} // Not implemented
113
}
114