Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

CommandTrait::str()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Response\Traits;
4
5
use Jaxon\Plugin\ResponsePlugin;
6
use Jaxon\Response\ResponseInterface;
7
use JsonSerializable;
8
9
use function array_filter;
10
use function array_merge;
11
use function count;
12
13
trait CommandTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait CommandTrait
Loading history...
14
{
15
    /**
16
     * The commands that will be sent to the browser in the response
17
     *
18
     * @var array
19
     */
20
    protected $aCommands = [];
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
21
22
    /**
23
     * Convert to string
24
     *
25
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
26
     *
27
     * @return string
28
     */
29
    abstract protected function str($xData): string;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
30
31
    /**
32
     * Get the commands in the response
33
     *
34
     * @return array
35
     */
36
    public function getCommands(): array
37
    {
38
        return $this->aCommands;
39
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
40
41
    /**
42
     * Get the number of commands in the response
43
     *
44
     * @return int
45
     */
46
    public function getCommandCount(): int
47
    {
48
        return count($this->aCommands);
49
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
50
51
    /**
52
     * Clear all the commands already added to the response
53
     *
54
     * @return void
55
     */
56
    public function clearCommands()
57
    {
58
        $this->aCommands = [];
59
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
60
61
    /**
62
     * Merge the commands with those in this <Response> object
63
     *
64
     * @param array $aCommands    The commands to merge
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
65
     * @param bool $bBefore    Add the new commands to the beginning of the list
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
66
     *
67
     * @return void
68
     */
69
    public function appendCommands(array $aCommands, bool $bBefore = false)
70
    {
71
        $this->aCommands = ($bBefore) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
72
            array_merge($aCommands, $this->aCommands) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
73
            array_merge($this->aCommands, $aCommands);
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * Merge the response commands with those in this <Response> object
78
     *
79
     * @param ResponseInterface $xResponse    The <Response> object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
80
     * @param bool $bBefore    Add the new commands to the beginning of the list
0 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
81
     *
82
     * @return void
83
     */
84
    public function appendResponse(ResponseInterface $xResponse, bool $bBefore = false)
85
    {
86
        $this->appendCommands($xResponse->getCommands(), $bBefore);
87
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
88
89
    /**
90
     * Add a response command to the array of commands that will be sent to the browser
91
     *
92
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
93
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
94
     *
95
     * @return ResponseInterface
96
     */
97
    public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface
98
    {
99
        $this->aCommands[] = [
100
            'cmd' => $this->str($sName),
101
            'options' => $aOptions,
102
        ];
103
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\CommandTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Add a response command that is generated by a plugin
108
     *
109
     * @param ResponsePlugin $xPlugin    The plugin object
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
110
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
111
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
112
     *
113
     * @return ResponseInterface
114
     */
115
    public function addPluginCommand(ResponsePlugin $xPlugin, string $sName,
116
        array|JsonSerializable $aOptions): ResponseInterface
117
    {
118
        $this->aCommands[] = [
119
            'cmd' => $this->str($sName),
120
            'plg' => $xPlugin->getName(),
121
            'options' => $aOptions,
122
        ];
123
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\CommandTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Add a response command to the array of commands that will be sent to the browser
128
     *
129
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
130
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
131
     * @param bool $bRemoveEmpty    If true, remove empty options
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
132
     *
133
     * @return ResponseInterface
134
     */
135
    protected function _addCommand(string $sName, array|JsonSerializable $aOptions,
136
        bool $bRemoveEmpty = false): ResponseInterface
137
    {
138
        if($bRemoveEmpty)
139
        {
140
            $aOptions = array_filter($aOptions, function($xOption) {
141
                return $xOption === '';
142
            });
143
        }
144
        return $this->addCommand($this->str($sName), $aOptions);
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
146
}
147