CallableFunction::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * CallableFunction.php - Jaxon user function
5
 *
6
 * This class stores a reference to a user defined function which can be called from the client via an Jaxon request
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\Plugin\Request\CallableFunction;
22
23
use Jaxon\Di\Container;
24
25
class CallableFunction
26
{
27
    /**
28
     * The DI container
29
     *
30
     * @var Container
31
     */
32
    protected $di;
33
34
    /**
35
     * The name of the function in the ajax call
36
     *
37
     * @var string
38
     */
39
    private $sFunction;
40
41
    /**
42
     * The name of the generated javascript function
43
     *
44
     * @var string
45
     */
46
    private $sJsFunction;
47
48
    /**
49
     * A string or an array which defines the registered PHP function
50
     *
51
     * @var string|array
52
     */
53
    private $xPhpFunction;
54
55
    /**
56
     * The path and file name of the include file where the function is defined
57
     *
58
     * @var string
59
     */
60
    private $sInclude;
61
62
    /**
63
     * An associative array containing call options that will be sent
64
     * to the browser curing client script generation
65
     *
66
     * @var array
67
     */
68
    private $aOptions = [];
69
70
    /**
71
     * The constructor
72
     *
73
     * @param Container $di
74
     * @param string $sFunction
75
     * @param string $sJsFunction
76
     * @param string $sPhpFunction
77
     */
78
    public function __construct(Container $di, string $sFunction, string $sJsFunction, string $sPhpFunction)
79
    {
80
        $this->di = $di;
81
        $this->sFunction = $sFunction;
82
        $this->sJsFunction = $sJsFunction;
83
        $this->xPhpFunction = $sPhpFunction;
84
    }
85
86
    /**
87
     * Get the name of the function being referenced
88
     *
89
     * @return string
90
     */
91
    public function getName(): string
92
    {
93
        return $this->sFunction;
94
    }
95
96
    /**
97
     * Get name of the corresponding javascript function
98
     *
99
     * @return string
100
     */
101
    public function getJsName(): string
102
    {
103
        return $this->sJsFunction;
104
    }
105
106
    /**
107
     * Get the config options of the function being referenced
108
     *
109
     * @return array
110
     */
111
    public function getOptions(): array
112
    {
113
        return $this->aOptions;
114
    }
115
116
    /**
117
     * Set call options for this instance
118
     *
119
     * @param string $sName    The name of the configuration option
120
     * @param string $sValue    The value of the configuration option
121
     *
122
     * @return void
123
     */
124
    public function configure(string $sName, string $sValue)
125
    {
126
        switch($sName)
127
        {
128
        case 'class': // The user function is a method in the given class
129
            $this->xPhpFunction = [$sValue, $this->xPhpFunction];
130
            break;
131
        case 'include':
132
            $this->sInclude = $sValue;
133
            break;
134
        default:
135
            $this->aOptions[$sName] = $sValue;
136
            break;
137
        }
138
    }
139
140
    /**
141
     * Call the registered user function, including an external file if needed
142
     * and passing along the specified arguments
143
     *
144
     * @param array $aArgs    The function arguments
145
     *
146
     * @return mixed
147
     */
148
    public function call(array $aArgs = [])
149
    {
150
        if(($this->sInclude))
151
        {
152
            require_once $this->sInclude;
153
        }
154
        // If the function is an alias for a class method, then instantiate the class
155
        if(is_array($this->xPhpFunction) && is_string($this->xPhpFunction[0]))
156
        {
157
            $sClassName = $this->xPhpFunction[0];
158
            $this->xPhpFunction[0] = $this->di->h($sClassName) ?
159
                $this->di->g($sClassName) : $this->di->make($sClassName);
160
        }
161
        return call_user_func_array($this->xPhpFunction, $aArgs);
162
    }
163
}
164