Completed
Push — master ( 75c398...3c71f0 )
by Thierry
01:47
created

JsCall::useSingleQuotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * JsCall.php - A javascript function call, with its parameters.
5
 *
6
 * @package jaxon-core
7
 * @author Jared White
8
 * @author J. Max Wilson
9
 * @author Joseph Woolley
10
 * @author Steffen Konerow
11
 * @author Thierry Feuzeu
12
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
13
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
14
 * @copyright 2016 Thierry Feuzeu <[email protected]>
15
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
16
 * @link https://github.com/jaxon-php/jaxon-core
17
 */
18
19
namespace Jaxon\Request\Factory;
20
21
use JsonSerializable;
22
use Jaxon\Jaxon;
23
24
class JsCall implements JsonSerializable
25
{
26
    /**
27
     * The name of the javascript function
28
     *
29
     * @var string
30
     */
31
    private $sFunction;
32
33
    /**
34
     * A string containing either a single or a double quote character that will be used
35
     * during the generation of the javascript for this function.
36
     * This can be set prior to calling <Request->getScript>
37
     *
38
     * @var string
39
     */
40
    public $sQuoteCharacter;
41
42
    /**
43
     * An array of parameters that will be used to populate the argument list for this function
44
     * when the javascript is output in <Request->getScript>
45
     *
46
     * @var array
47
     */
48
    protected $aParameters;
49
50
    /**
51
     * The constructor.
52
     *
53
     * @param string        $sFunction            The javascript function
54
     */
55
    public function __construct($sFunction)
56
    {
57
        $this->aParameters = [];
58
        $this->sQuoteCharacter = '"';
59
        $this->sFunction = $sFunction;
60
    }
61
62
    /**
63
     * Instruct the request to use single quotes when generating the javascript
64
     *
65
     * @return void
66
     */
67
    public function useSingleQuote()
68
    {
69
        $this->sQuoteCharacter = "'";
70
        return $this;
71
    }
72
73
    /**
74
     * Instruct the request to use single quotes when generating the javascript
75
     *
76
     * @return void
77
     */
78
    public function useSingleQuotes()
79
    {
80
        $this->sQuoteCharacter = "'";
81
        return $this;
82
    }
83
84
    /**
85
     * Instruct the request to use double quotes while generating the javascript
86
     *
87
     * @return void
88
     */
89
    public function useDoubleQuote()
90
    {
91
        $this->sQuoteCharacter = '"';
92
    }
93
94
    /**
95
     * Instruct the request to use double quotes while generating the javascript
96
     *
97
     * @return void
98
     */
99
    public function useDoubleQuotes()
100
    {
101
        $this->sQuoteCharacter = '"';
102
    }
103
104
    /**
105
     * Clear the parameter list associated with this request
106
     *
107
     * @return void
108
     */
109
    public function clearParameters()
110
    {
111
        $this->aParameters = [];
112
    }
113
114
    /**
115
     * Set the value of the parameter at the given position
116
     *
117
     * @param Interfaces\Parameter      $xParameter             The value to be used
118
     *
119
     * @return void
120
     */
121
    public function pushParameter(Interfaces\Parameter $xParameter)
122
    {
123
        $this->aParameters[] = $xParameter;
124
    }
125
126
    /**
127
     * Add a parameter value to the parameter list for this request
128
     *
129
     * @param string            $sType              The type of the value to be used
130
     * @param string            $sValue             The value to be used
131
     *
132
     * Types should be one of the following <Jaxon::FORM_VALUES>, <Jaxon::QUOTED_VALUE>, <Jaxon::NUMERIC_VALUE>,
133
     * <Jaxon::JS_VALUE>, <Jaxon::INPUT_VALUE>, <Jaxon::CHECKED_VALUE>, <Jaxon::PAGE_NUMBER>.
134
     * The value should be as follows:
135
     * - <Jaxon::FORM_VALUES> - Use the ID of the form you want to process.
136
     * - <Jaxon::QUOTED_VALUE> - The string data to be passed.
137
     * - <Jaxon::JS_VALUE> - A string containing valid javascript
138
     *   (either a javascript variable name that will be in scope at the time of the call or
139
     *   a javascript function call whose return value will become the parameter).
140
     *
141
     * @return void
142
     */
143
    public function addParameter($sType, $sValue)
144
    {
145
        $this->pushParameter(new Parameter($sType, $sValue));
146
    }
147
148
    /**
149
     * Add a set of parameters to this request
150
     *
151
     * @param array             $aParameters             The parameters
152
     *
153
     * @return void
154
     */
155
    public function addParameters(array $aParameters)
156
    {
157
        foreach($aParameters as $xParameter)
158
        {
159
            if($xParameter instanceof JsCall)
160
            {
161
                $this->addParameter(Jaxon::JS_VALUE, 'function(){' . $xParameter->getScript() . ';}');
162
            }
163
            else
164
            {
165
                $this->pushParameter(Parameter::make($xParameter));
166
            }
167
        }
168
    }
169
170
    /**
171
     * Returns a string representation of the script output (javascript) from this request object
172
     *
173
     * @return string
174
     */
175
    public function getScript()
176
    {
177
        return $this->sFunction . '(' . implode(', ', $this->aParameters) . ')';
178
    }
179
180
    /**
181
     * Convert this request object to string
182
     *
183
     * @return string
184
     */
185
    public function __toString()
186
    {
187
        return $this->getScript();
188
    }
189
190
    /**
191
     * Convert this request object to string, when converting the response into json.
192
     *
193
     * This is a method of the JsonSerializable interface.
194
     *
195
     * @return string
196
     */
197
    public function jsonSerialize()
198
    {
199
        return $this->getScript();
200
    }
201
}
202