Completed
Push — master ( c61dd9...791f1f )
by Thierry
01:36
created

Element::getScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Element.php - A jQuery selector
5
 *
6
 * This class is used to create client side requests to the Jaxon functions and callable objects.
7
 *
8
 * When inserted into a Jaxon response, an Element object must be converted to the corresponding jQuery code.
9
 * Therefore, the Element class implements the JsonSerializable interface.
10
 *
11
 * When used as a parameter of a Jaxon call, the Element must be converted to Jaxon request parameter.
12
 * Therefore, the Element class also implements the Jaxon\Request\Interfaces\Parameter interface.
13
 *
14
 * @package jaxon-jquery
15
 * @author Thierry Feuzeu <[email protected]>
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-jquery
19
 */
20
21
namespace Jaxon\Response\Plugin\JQuery\Dom;
22
23
use JsonSerializable;
24
use Jaxon\Jaxon;
25
use Jaxon\Request\JsCall;
26
use Jaxon\Request\Interfaces\Parameter;
27
28
use Jaxon\Response\Plugin\Call\Method;
29
use Jaxon\Response\Plugin\Call\AttrSet;
30
use Jaxon\Response\Plugin\Call\AttrGet;
31
32
class Element implements JsonSerializable, Parameter
33
{
34
    /**
35
     * The jQuery selector
36
     *
37
     * @var string
38
     */
39
    protected $sSelector;
40
41
    /**
42
     * The actions to be applied on the selected element
43
     *
44
     * @var array
45
     */
46
    protected $aCalls;
47
48
    /**
49
     * The constructor.
50
     *
51
     * @param string        $sSelector            The jQuery selector
52
     * @param string        $sContext             A context associated to the selector
53
     */
54
    public function __construct($sSelector = '', $sContext = '')
55
    {
56
        $sSelector = trim($sSelector, " \t");
57
        $sContext = trim($sContext, " \t");
58
        $this->aCalls = [];
59
60
        $jQueryNs = jaxon()->getOption('core.jquery.no_conflict', false) ? 'jQuery' : '$';
61
        if(!$sSelector)
62
        {
63
            $this->sSelector = "$jQueryNs(this)"; // If an empty selector is given, use javascript "this" instead
64
        }
65
        elseif(($sContext))
66
        {
67
            $this->sSelector = "$jQueryNs('" . $sSelector . "', $jQueryNs('" . $sContext . "'))";
68
        }
69
        else
70
        {
71
            $this->sSelector = "$jQueryNs('" . $sSelector . "')";
72
        }
73
    }
74
75
    /**
76
     * Add a call to a jQuery method on the selected elements
77
     *
78
     * @return Element
79
     */
80
    public function __call($sMethod, $aArguments)
81
    {
82
        // Push the action into the array
83
        $this->aCalls[] = new Method($sMethod, $aArguments);
84
        // Return $this so the calls can be chained
85
        return $this;
86
    }
87
88
    /**
89
     * Get the value of an attribute on the first selected element
90
     *
91
     * @return Element
92
     */
93
    public function __get($sAttribute)
94
    {
95
        // Push the action into the array
96
        $this->aCalls[] = new AttrGet($sAttribute);
97
        // Return $this so the calls can be chained
98
        return $this;
99
    }
100
101
    /**
102
     * Set the value of an attribute on the first selected element
103
     *
104
     * @return Element
105
     */
106
    public function __set($sAttribute, $xValue)
107
    {
108
        // Push the action into the array
109
        $this->aCalls[] = new AttrSet($sAttribute, $xValue);
110
        // Return null because no other call is allowed after a set
111
        return null;
112
    }
113
114
    /**
115
     * Generate the jQuery call.
116
     *
117
     * @return string
118
     */
119
    public function getScript()
120
    {
121
        if(count($this->aCalls) == 0)
122
        {
123
            return $this->sSelector;
124
        }
125
        return $this->sSelector . '.' . implode('.', $this->aCalls);
126
    }
127
128
    /**
129
     * Magic function to generate the jQuery call.
130
     *
131
     * @return string
132
     */
133
    public function __toString()
134
    {
135
        return $this->getScript();
136
    }
137
138
    /**
139
     * Generate the jQuery call, when converting the response into json.
140
     *
141
     * This is a method of the JsonSerializable interface.
142
     *
143
     * @return string
144
     */
145
    public function jsonSerialize()
146
    {
147
        return $this->getScript();
148
    }
149
}
150