Completed
Push — master ( 9c85df...39246a )
by Thierry
03:07
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
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\Request as JaxonRequest;
20
use Jaxon\Request\Support\CallableObject;
21
22
// Extends Parameter for compatibility with older versions (see function rq())
23
class Request extends Parameter
24
{
25
    use \Jaxon\Utils\Traits\Config;
26
27
    /**
28
     * The prefix to prepend on each call
29
     *
30
     * @var string
31
     */
32
    protected $sPrefix;
33
34
    /**
35
     * The callable dir plugin
36
     *
37
     * @var Jaxon\Request\Plugin\CallableDir;
38
     */
39
    protected $xCallableDirPlugin;
40
41
    /**
42
     * The callable class plugin
43
     *
44
     * @var Jaxon\Request\Plugin\CallableClass;
45
     */
46
    protected $xCallableClassPlugin;
47
48
    /**
49
     * The class constructor
50
     */
51
    public function __construct()
52
    {
53
        $xPluginManager = jaxon()->getPluginManager();
54
        $this->xCallableDirPlugin = $xPluginManager->getRequestPlugin(Jaxon::CALLABLE_DIR);
55
        $this->xCallableClassPlugin = $xPluginManager->getRequestPlugin(Jaxon::CALLABLE_CLASS);
56
    }
57
58
    /**
59
     * Set the name of the class to call
60
     *
61
     * @param string|null            $sClass              The callable class
62
     *
63
     * @return Factory
64
     */
65
    public function setClassName($sClass)
66
    {
67
        $this->sPrefix = $this->getOption('core.prefix.function');
68
69
        $sClass = trim($sClass, '.\\ ');
70
        if(!$sClass)
71
        {
72
            return $this;
73
        }
74
75
        $xCallable = $this->xCallableClassPlugin->getCallableObject($sClass);
76
        if(!$xCallable)
77
        {
78
            $xCallable = $this->xCallableDirPlugin->getCallableObject($sClass);
79
        }
80
        if(!$xCallable)
81
        {
82
            // Todo: decide which of these values to return
83
            // return null;
84
            return $this;
85
        }
86
87
        $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
88
        return $this;
89
    }
90
91
    /**
92
     * Set the callable object to call
93
     *
94
     * @param CallableObject          $xCallable              The callable object
95
     *
96
     * @return Factory
97
     */
98
    public function setCallable(CallableObject $xCallable)
99
    {
100
        $this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.';
101
102
        return $this;
103
    }
104
105
    /**
106
     * Return the javascript call to a Jaxon function or object method
107
     *
108
     * @param string            $sFunction          The function or method (without class) name
109
     * @param ...               $xParams            The parameters of the function or method
110
     *
111
     * @return \Jaxon\Request\Request
112
     */
113
    public function call($sFunction)
114
    {
115
        $aArguments = func_get_args();
116
        $sFunction = (string)$sFunction;
117
        // Remove the function name from the arguments array.
118
        array_shift($aArguments);
119
120
        // Makes legacy code works
121
        if(strpos($sFunction, '.') !== false)
122
        {
123
            // If there is a dot in the name, then it is a call to a class
124
            $this->sPrefix = $this->getOption('core.prefix.class');
125
        }
126
127
        // Make the request
128
        $xRequest = new JaxonRequest($this->sPrefix . $sFunction);
129
        $xRequest->useSingleQuote();
130
        $xRequest->addParameters($aArguments);
131
        return $xRequest;
132
    }
133
134
    /**
135
     * Return the javascript call to a generic function
136
     *
137
     * @param string            $sFunction          The function or method (with class) name
138
     * @param ...               $xParams            The parameters of the function or method
139
     *
140
     * @return \Jaxon\Request\Request
141
     */
142
    public function func($sFunction)
143
    {
144
        $aArguments = func_get_args();
145
        $sFunction = (string)$sFunction;
146
        // Remove the function name from the arguments array.
147
        array_shift($aArguments);
148
        // Make the request
149
        $xRequest = new JaxonRequest($sFunction);
150
        $xRequest->useSingleQuote();
151
        $xRequest->addParameters($aArguments);
152
        return $xRequest;
153
    }
154
155
    /**
156
     * Make the pagination links for a registered Jaxon class method
157
     *
158
     * @param integer       $nItemsTotal            The total number of items
159
     * @param integer       $nItemsPerPage          The number of items per page page
160
     * @param integer       $nCurrentPage           The current page
161
     * @param string        $sMethod                The name of function or a method prepended with its class name
162
     * @param ...           $xParams                The parameters of the function or method
163
     *
164
     * @return string the pagination links
165
     */
166
    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...
167
    {
168
        // Get the args list starting from the $sMethod
169
        $aArgs = array_slice(func_get_args(), 3);
170
        // Make the request
171
        $request = call_user_func_array('self::call', $aArgs);
172
        $paginator = jaxon()->paginator($nItemsTotal, $nItemsPerPage, $nCurrentPage, $request);
173
        return $paginator->toHtml();
174
    }
175
}
176