ComponentResponse::prepend()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * ComponentResponse.php
5
 *
6
 * This class is a special response class form Jaxon components.
7
 *
8
 * @package jaxon-core
9
 * @author Jared White
10
 * @author J. Max Wilson
11
 * @author Joseph Woolley
12
 * @author Steffen Konerow
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
20
21
namespace Jaxon\Response;
22
23
use Jaxon\Plugin\Manager\PluginManager;
24
use Jaxon\Script\JxnCall;
25
use JsonSerializable;
26
27
class ComponentResponse extends AjaxResponse
28
{
29
    /**
30
     * @var array
31
     */
32
    private $aComponent = [];
33
34
    /**
35
     * The constructor
36
     *
37
     * @param ResponseManager $xManager
38
     * @param PluginManager $xPluginManager
39
     * @param JxnCall $xJxnCall
40
     */
41
    public function __construct(ResponseManager $xManager,
42
        PluginManager $xPluginManager, JxnCall $xJxnCall)
43
    {
44
        parent::__construct($xManager, $xPluginManager);
45
        // The js class name is also the component name.
46
        $this->aComponent['name'] = $this->str($xJxnCall->_class());
47
    }
48
49
    /**
50
     * Set the component item
51
     *
52
     * @param string $sItem
53
     *
54
     * @return self
55
     */
56
    public function item(string $sItem = 'main'): self
57
    {
58
        $this->aComponent['item'] = $this->str($sItem);
59
        return $this;
60
    }
61
62
    /**
63
     * Add a response command to the array of commands
64
     *
65
     * @param string $sName    The command name
66
     * @param array|JsonSerializable $aArgs    The command arguments
67
     * @param bool $bRemoveEmpty
68
     *
69
     * @return Command
70
     */
71
    public function addCommand(string $sName, array|JsonSerializable $aArgs = [],
72
        bool $bRemoveEmpty = false): Command
73
    {
74
        return $this->xManager
75
            ->addCommand($sName, $aArgs, $bRemoveEmpty)
76
            ->setComponent($this->aComponent);
77
    }
78
79
    /**
80
     * Add a command to assign the specified value to the given element's attribute
81
     *
82
     * @param string $sAttribute    The attribute to be assigned
83
     * @param string $sValue    The value to be assigned to the attribute
84
     *
85
     * @return self
86
     */
87
    public function assign(string $sAttribute, string $sValue): self
88
    {
89
        $this->addCommand('node.assign', [
90
            'attr' => $this->str($sAttribute),
91
            'value' => $this->str($sValue),
92
        ]);
93
        return $this;
94
    }
95
96
    /**
97
     * Add a command to assign the specified HTML content to the given element
98
     *
99
     * This is a shortcut for assign() on the innerHTML attribute.
100
     *
101
     * @param string $sValue    The value to be assigned to the attribute
102
     *
103
     * @return self
104
     */
105
    public function html(string $sValue): self
106
    {
107
        return $this->assign('innerHTML', $sValue);
108
    }
109
110
    /**
111
     * Add a command to append the specified data to the given element's attribute
112
     *
113
     * @param string $sAttribute    The name of the attribute to be appended to
114
     * @param string $sValue    The data to be appended to the attribute
115
     *
116
     * @return self
117
     */
118
    public function append(string $sAttribute, string $sValue): self
119
    {
120
        $this->addCommand('node.append', [
121
            'attr' => $this->str($sAttribute),
122
            'value' => $this->str($sValue),
123
        ]);
124
        return $this;
125
    }
126
127
    /**
128
     * Add a command to prepend the specified data to the given element's attribute
129
     *
130
     * @param string $sAttribute    The name of the attribute to be prepended to
131
     * @param string $sValue    The value to be prepended to the attribute
132
     *
133
     * @return self
134
     */
135
    public function prepend(string $sAttribute, string $sValue): self
136
    {
137
        $this->addCommand('node.prepend', [
138
            'attr' => $this->str($sAttribute),
139
            'value' => $this->str($sValue),
140
        ]);
141
        return $this;
142
    }
143
144
    /**
145
     * Add a command to replace a specified value with another value within the given element's attribute
146
     *
147
     * @param string $sAttribute    The attribute to be updated
148
     * @param string $sSearch    The needle to search for
149
     * @param string $sReplace    The data to use in place of the needle
150
     *
151
     * @return self
152
     */
153
    public function replace(string $sAttribute, string $sSearch, string $sReplace): self
154
    {
155
        $this->addCommand('node.replace', [
156
            'attr' => $this->str($sAttribute),
157
            'search' => $this->str($sSearch),
158
            'replace' => $this->str($sReplace),
159
        ]);
160
        return $this;
161
    }
162
163
    /**
164
     * Add a command to clear the specified attribute of the given element
165
     *
166
     * @param string $sAttribute    The attribute to be cleared
167
     *
168
     * @return self
169
     */
170
    public function clear(string $sAttribute = 'innerHTML'): self
171
    {
172
        $this->addCommand('node.clear', [
173
            'attr' => $this->str($sAttribute),
174
        ]);
175
        return $this;
176
    }
177
178
    /**
179
     * Add a command to remove an element from the document
180
     *
181
     * @return self
182
     */
183
    public function remove(): self
184
    {
185
        $this->addCommand('node.remove', []);
186
        return $this;
187
    }
188
}
189