Completed
Push — master ( f911b9...fd24d6 )
by Thierry
01:43
created

Request::func()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Factory.php - Jaxon Request Factory
5
 *
6
 * Create Jaxon client side requests, which will generate the client script necessary
7
 * to invoke a jaxon request from the browser to registered objects.
8
 *
9
 * @package jaxon-core
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-core
14
 */
15
16
namespace Jaxon\Factory;
17
18
use Jaxon\Jaxon;
19
use Jaxon\Request\Support\CallableObject;
20
21
class Request
22
{
23
    use \Jaxon\Utils\Traits\Config;
24
25
    /**
26
     * The prefix to prepend on each call
27
     *
28
     * @var string
29
     */
30
    protected $sPrefix;
31
32
    /**
33
     * Set the name of the class to call
34
     *
35
     * @param string|null            $sClass              The callable class
36
     *
37
     * @return Factory
38
     */
39
    public function setClassName($sClass)
40
    {
41
        $sClass = trim($sClass, '.\\ ');
42
        if(($sClass))
43
        {
44
            $xCallable = jaxon()->di()->get($sClass);
45
            $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
46
        }
47
        else
48
        {
49
            $this->sPrefix = $this->getOption('core.prefix.function');
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set the callable object to call
57
     *
58
     * @param CallableObject          $xCallable              The callable object
59
     *
60
     * @return Factory
61
     */
62
    public function setCallable(CallableObject $xCallable)
63
    {
64
        $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
65
66
        return $this;
67
    }
68
69
    /**
70
     * Return the javascript call to a Jaxon function or object method
71
     *
72
     * @param string            $sFunction          The function or method (without class) name
73
     * @param ...               $xParams            The parameters of the function or method
74
     *
75
     * @return \Jaxon\Request\Request
76
     */
77
    public function call($sFunction)
78
    {
79
        $aArguments = func_get_args();
80
        $sFunction = (string)$sFunction;
81
        // Remove the function name from the arguments array.
82
        array_shift($aArguments);
83
84
        // Makes legacy code works
85
        if(strpos($sFunction, '.') !== false)
86
        {
87
            // If there is a dot in the name, then it is a call to a class
88
            $this->sPrefix = $this->getOption('core.prefix.class');
89
        }
90
91
        // Make the request
92
        $xRequest = new Request($this->sPrefix . $sFunction);
0 ignored issues
show
Unused Code introduced by
The call to Request::__construct() has too many arguments starting with $this->sPrefix . $sFunction.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
        $xRequest->useSingleQuote();
0 ignored issues
show
Bug introduced by
The method useSingleQuote() does not seem to exist on object<Jaxon\Factory\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        $xRequest->addParameters($aArguments);
0 ignored issues
show
Bug introduced by
The method addParameters() does not seem to exist on object<Jaxon\Factory\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        return $xRequest;
96
    }
97
98
    /**
99
     * Return the javascript call to a generic function
100
     *
101
     * @param string            $sFunction          The function or method (with class) name
102
     * @param ...               $xParams            The parameters of the function or method
103
     *
104
     * @return \Jaxon\Request\Request
105
     */
106
    public function func($sFunction)
107
    {
108
        $aArguments = func_get_args();
109
        $sFunction = (string)$sFunction;
110
        // Remove the function name from the arguments array.
111
        array_shift($aArguments);
112
        // Make the request
113
        $xRequest = new Request($sFunction);
0 ignored issues
show
Unused Code introduced by
The call to Request::__construct() has too many arguments starting with $sFunction.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
        $xRequest->useSingleQuote();
0 ignored issues
show
Bug introduced by
The method useSingleQuote() does not seem to exist on object<Jaxon\Factory\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        $xRequest->addParameters($aArguments);
0 ignored issues
show
Bug introduced by
The method addParameters() does not seem to exist on object<Jaxon\Factory\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
        return $xRequest;
117
    }
118
119
    /**
120
     * Make the pagination links for a registered Jaxon class method
121
     *
122
     * @param integer       $nItemsTotal            The total number of items
123
     * @param integer       $nItemsPerPage          The number of items per page page
124
     * @param integer       $nCurrentPage           The current page
125
     * @param string        $sMethod                The name of function or a method prepended with its class name
126
     * @param ...           $xParams                The parameters of the function or method
127
     *
128
     * @return string the pagination links
129
     */
130
    public function paginate($nItemsTotal, $nItemsPerPage, $nCurrentPage, $sMethod)
0 ignored issues
show
Unused Code introduced by
The parameter $sMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        // Get the args list starting from the $sMethod
133
        $aArgs = array_slice(func_get_args(), 3);
134
        // Make the request
135
        $request = call_user_func_array('self::call', $aArgs);
136
        $paginator = jaxon()->paginator($nItemsTotal, $nItemsPerPage, $nCurrentPage, $request);
137
        return $paginator->toHtml();
138
    }
139
140
    /**
141
     * Make a parameter of type Jaxon::FORM_VALUES
142
     *
143
     * @param string        $sFormId                The id of the HTML form
144
     *
145
     * @return Parameter
146
     */
147
    public function form($sFormId)
148
    {
149
        return new Parameter(Jaxon::FORM_VALUES, $sFormId);
150
    }
151
152
    /**
153
     * Make a parameter of type Jaxon::INPUT_VALUE
154
     *
155
     * @param string $sInputId the id of the HTML input element
156
     *
157
     * @return Parameter
158
     */
159
    public function input($sInputId)
160
    {
161
        return new Parameter(Jaxon::INPUT_VALUE, $sInputId);
162
    }
163
164
    /**
165
     * Make a parameter of type Jaxon::CHECKED_VALUE
166
     *
167
     * @param string $sInputId the name of the HTML form element
168
     *
169
     * @return Parameter
170
     */
171
    public function checked($sInputId)
172
    {
173
        return new Parameter(Jaxon::CHECKED_VALUE, $sInputId);
174
    }
175
176
    /**
177
     * Make a parameter of type Jaxon::CHECKED_VALUE
178
     *
179
     * @param string $sInputId the name of the HTML form element
180
     *
181
     * @return Parameter
182
     */
183
    public function select($sInputId)
184
    {
185
        return self::input($sInputId);
186
    }
187
188
    /**
189
     * Make a parameter of type Jaxon::ELEMENT_INNERHTML
190
     *
191
     * @param string $sElementId the id of the HTML element
192
     *
193
     * @return Parameter
194
     */
195
    public function html($sElementId)
196
    {
197
        return new Parameter(Jaxon::ELEMENT_INNERHTML, $sElementId);
198
    }
199
200
    /**
201
     * Make a parameter of type Jaxon::QUOTED_VALUE
202
     *
203
     * @param string $sValue the value of the parameter
204
     *
205
     * @return Parameter
206
     */
207
    public function string($sValue)
208
    {
209
        return new Parameter(Jaxon::QUOTED_VALUE, $sValue);
210
    }
211
212
    /**
213
     * Make a parameter of type Jaxon::NUMERIC_VALUE
214
     *
215
     * @param numeric $nValue the value of the parameter
216
     *
217
     * @return Parameter
218
     */
219
    public function numeric($nValue)
220
    {
221
        return new Parameter(Jaxon::NUMERIC_VALUE, intval($nValue));
222
    }
223
224
    /**
225
     * Make a parameter of type Jaxon::NUMERIC_VALUE
226
     *
227
     * @param numeric $nValue the value of the parameter
228
     *
229
     * @return Parameter
230
     */
231
    public function int($nValue)
232
    {
233
        return self::numeric($nValue);
234
    }
235
236
    /**
237
     * Make a parameter of type Jaxon::JS_VALUE
238
     *
239
     * @param string $sValue the javascript code of the parameter
240
     *
241
     * @return Parameter
242
     */
243
    public function javascript($sValue)
244
    {
245
        return new Parameter(Jaxon::JS_VALUE, $sValue);
246
    }
247
248
    /**
249
     * Make a parameter of type Jaxon::JS_VALUE
250
     *
251
     * @param string $sValue the javascript code of the parameter
252
     *
253
     * @return Parameter
254
     */
255
    public function js($sValue)
256
    {
257
        return self::javascript($sValue);
258
    }
259
260
    /**
261
     * Make a parameter of type Jaxon::PAGE_NUMBER
262
     *
263
     * @return Parameter
264
     */
265
    public function page()
266
    {
267
        // By default, the value of a parameter of type Jaxon::PAGE_NUMBER is 0.
268
        return new Parameter(Jaxon::PAGE_NUMBER, 0);
269
    }
270
}
271