Completed
Push — master ( 7b173b...f911b9 )
by Thierry
01:48
created

Factory::setClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
rs 9.7666
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\Request;
17
18
use Jaxon\Jaxon;
19
20
use Jaxon\Request\Support\CallableObject;
21
22
class Factory
23
{
24
    use \Jaxon\Utils\Traits\Config;
25
26
    /**
27
     * The prefix to prepend on each call
28
     *
29
     * @var string
30
     */
31
    protected $sPrefix;
32
33
    /**
34
     * Set the name of the class to call
35
     *
36
     * @param string|null            $sClass              The callable class
37
     *
38
     * @return Factory
39
     */
40
    public function setClassName($sClass)
41
    {
42
        $sClass = trim($sClass, '.\\ ');
43
        if(($sClass))
44
        {
45
            $xCallable = jaxon()->di()->get($sClass);
46
            $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
47
        }
48
        else
49
        {
50
            $this->sPrefix = $this->getOption('core.prefix.function');
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * Set the callable object to call
58
     *
59
     * @param CallableObject          $xCallable              The callable object
60
     *
61
     * @return Factory
62
     */
63
    public function setCallable(CallableObject $xCallable)
64
    {
65
        $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
66
67
        return $this;
68
    }
69
70
    /**
71
     * Return the javascript call to a Jaxon function or object method
72
     *
73
     * @param string            $sFunction          The function or method (without class) name
74
     * @param ...               $xParams            The parameters of the function or method
75
     *
76
     * @return \Jaxon\Request\Request
77
     */
78
    public function call($sFunction)
79
    {
80
        $aArguments = func_get_args();
81
        $sFunction = (string)$sFunction;
82
        // Remove the function name from the arguments array.
83
        array_shift($aArguments);
84
85
        // Makes legacy code works
86
        if(strpos($sFunction, '.') !== false)
87
        {
88
            // If there is a dot in the name, then it is a call to a class
89
            $this->sPrefix = $this->getOption('core.prefix.class');
90
        }
91
92
        // Make the request
93
        $xRequest = new Request($this->sPrefix . $sFunction);
94
        $xRequest->useSingleQuote();
95
        $xRequest->addParameters($aArguments);
96
        return $xRequest;
97
    }
98
99
    /**
100
     * Return the javascript call to a generic function
101
     *
102
     * @param string            $sFunction          The function or method (with class) name
103
     * @param ...               $xParams            The parameters of the function or method
104
     *
105
     * @return \Jaxon\Request\Request
106
     */
107
    public function func($sFunction)
108
    {
109
        $aArguments = func_get_args();
110
        $sFunction = (string)$sFunction;
111
        // Remove the function name from the arguments array.
112
        array_shift($aArguments);
113
        // Make the request
114
        $xRequest = new Request($sFunction);
115
        $xRequest->useSingleQuote();
116
        $xRequest->addParameters($aArguments);
117
        return $xRequest;
118
    }
119
120
    /**
121
     * Make the pagination links for a registered Jaxon class method
122
     *
123
     * @param integer       $nItemsTotal            The total number of items
124
     * @param integer       $nItemsPerPage          The number of items per page page
125
     * @param integer       $nCurrentPage           The current page
126
     * @param string        $sMethod                The name of function or a method prepended with its class name
127
     * @param ...           $xParams                The parameters of the function or method
128
     *
129
     * @return string the pagination links
130
     */
131
    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...
132
    {
133
        // Get the args list starting from the $sMethod
134
        $aArgs = array_slice(func_get_args(), 3);
135
        // Make the request
136
        $request = call_user_func_array('self::call', $aArgs);
137
        $paginator = jaxon()->paginator($nItemsTotal, $nItemsPerPage, $nCurrentPage, $request);
138
        return $paginator->toHtml();
139
    }
140
141
    /**
142
     * Make a parameter of type Jaxon::FORM_VALUES
143
     *
144
     * @param string        $sFormId                The id of the HTML form
145
     *
146
     * @return Parameter
147
     */
148
    public function form($sFormId)
149
    {
150
        return new Parameter(Jaxon::FORM_VALUES, $sFormId);
151
    }
152
153
    /**
154
     * Make a parameter of type Jaxon::INPUT_VALUE
155
     *
156
     * @param string $sInputId the id of the HTML input element
157
     *
158
     * @return Parameter
159
     */
160
    public function input($sInputId)
161
    {
162
        return new Parameter(Jaxon::INPUT_VALUE, $sInputId);
163
    }
164
165
    /**
166
     * Make a parameter of type Jaxon::CHECKED_VALUE
167
     *
168
     * @param string $sInputId the name of the HTML form element
169
     *
170
     * @return Parameter
171
     */
172
    public function checked($sInputId)
173
    {
174
        return new Parameter(Jaxon::CHECKED_VALUE, $sInputId);
175
    }
176
177
    /**
178
     * Make a parameter of type Jaxon::CHECKED_VALUE
179
     *
180
     * @param string $sInputId the name of the HTML form element
181
     *
182
     * @return Parameter
183
     */
184
    public function select($sInputId)
185
    {
186
        return self::input($sInputId);
187
    }
188
189
    /**
190
     * Make a parameter of type Jaxon::ELEMENT_INNERHTML
191
     *
192
     * @param string $sElementId the id of the HTML element
193
     *
194
     * @return Parameter
195
     */
196
    public function html($sElementId)
197
    {
198
        return new Parameter(Jaxon::ELEMENT_INNERHTML, $sElementId);
199
    }
200
201
    /**
202
     * Make a parameter of type Jaxon::QUOTED_VALUE
203
     *
204
     * @param string $sValue the value of the parameter
205
     *
206
     * @return Parameter
207
     */
208
    public function string($sValue)
209
    {
210
        return new Parameter(Jaxon::QUOTED_VALUE, $sValue);
211
    }
212
213
    /**
214
     * Make a parameter of type Jaxon::NUMERIC_VALUE
215
     *
216
     * @param numeric $nValue the value of the parameter
217
     *
218
     * @return Parameter
219
     */
220
    public function numeric($nValue)
221
    {
222
        return new Parameter(Jaxon::NUMERIC_VALUE, intval($nValue));
223
    }
224
225
    /**
226
     * Make a parameter of type Jaxon::NUMERIC_VALUE
227
     *
228
     * @param numeric $nValue the value of the parameter
229
     *
230
     * @return Parameter
231
     */
232
    public function int($nValue)
233
    {
234
        return self::numeric($nValue);
235
    }
236
237
    /**
238
     * Make a parameter of type Jaxon::JS_VALUE
239
     *
240
     * @param string $sValue the javascript code of the parameter
241
     *
242
     * @return Parameter
243
     */
244
    public function javascript($sValue)
245
    {
246
        return new Parameter(Jaxon::JS_VALUE, $sValue);
247
    }
248
249
    /**
250
     * Make a parameter of type Jaxon::JS_VALUE
251
     *
252
     * @param string $sValue the javascript code of the parameter
253
     *
254
     * @return Parameter
255
     */
256
    public function js($sValue)
257
    {
258
        return self::javascript($sValue);
259
    }
260
261
    /**
262
     * Make a parameter of type Jaxon::PAGE_NUMBER
263
     *
264
     * @return Parameter
265
     */
266
    public function page()
267
    {
268
        // By default, the value of a parameter of type Jaxon::PAGE_NUMBER is 0.
269
        return new Parameter(Jaxon::PAGE_NUMBER, 0);
270
    }
271
}
272