Completed
Push — master ( 75c398...3c71f0 )
by Thierry
01:47
created

Parameter::getScript()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 45

Duplication

Lines 12
Ratio 26.67 %

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 0
dl 12
loc 45
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Parameter.php - A parameter of a Jaxon request
5
 *
6
 * This class is used to create client side requests to the Jaxon functions and callable objects.
7
 *
8
 * @package jaxon-core
9
 * @copyright 2016 Thierry Feuzeu <[email protected]>
10
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
13
14
namespace Jaxon\Request\Factory;
15
16
use Jaxon\Jaxon;
17
18
class Parameter implements Interfaces\Parameter
19
{
20
    /**
21
     * The parameter type
22
     *
23
     * @var string
24
     */
25
    protected $sType;
26
27
    /**
28
     * The parameter value
29
     *
30
     * @var mixed
31
     */
32
    protected $xValue;
33
34
    /**
35
     * The constructor.
36
     *
37
     * @param string        $sType                  The parameter type
38
     * @param mixed         $xValue                 The parameter value
39
     */
40
    public function __construct($sType, $xValue)
41
    {
42
        $this->sType = $sType;
43
        $this->xValue = $xValue;
44
    }
45
46
    /**
47
     * Get the parameter type
48
     *
49
     * @return string
50
     */
51
    public function getType()
52
    {
53
        return $this->sType;
54
    }
55
56
    /**
57
     * Get the parameter value
58
     *
59
     * @return mixed
60
     */
61
    public function getValue()
62
    {
63
        return $this->xValue;
64
    }
65
66
    /**
67
     * Set the parameter value
68
     *
69
     * @param mixed         $xValue                 The parameter value
70
     *
71
     * @return void
72
     */
73
    public function setValue($xValue)
74
    {
75
        $this->xValue = $xValue;
76
    }
77
78
    /**
79
     * Create a Parameter instance using the given value
80
     *
81
     * @param mixed         $xValue                 The parameter value
82
     *
83
     * @return Parameter
84
     */
85
    public static function make($xValue)
86
    {
87
        if($xValue instanceof Interfaces\Parameter)
88
        {
89
            return $xValue;
90
        }
91
        elseif(is_numeric($xValue))
92
        {
93
            return new Parameter(Jaxon::NUMERIC_VALUE, $xValue);
94
        }
95
        elseif(is_string($xValue))
96
        {
97
            return new Parameter(Jaxon::QUOTED_VALUE, $xValue);
98
        }
99
        elseif(is_bool($xValue))
100
        {
101
            return new Parameter(Jaxon::BOOL_VALUE, $xValue);
102
        }
103
        else // if(is_array($xValue) || is_object($xValue))
104
        {
105
            return new Parameter(Jaxon::JS_VALUE, $xValue);
106
        }
107
    }
108
109
    /**
110
     * Generate the javascript code.
111
     *
112
     * @return string
113
     */
114
    public function getScript()
115
    {
116
        $sJsCode = '';
117
        $sQuoteCharacter = "'";
118
        switch($this->sType)
119
        {
120 View Code Duplication
        case Jaxon::FORM_VALUES:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
            $sJsCode = "jaxon.getFormValues(" . $sQuoteCharacter . $this->xValue . $sQuoteCharacter . ")";
122
            break;
123 View Code Duplication
        case Jaxon::INPUT_VALUE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
            $sJsCode = "jaxon.$("  . $sQuoteCharacter . $this->xValue . $sQuoteCharacter  . ").value";
125
            break;
126 View Code Duplication
        case Jaxon::CHECKED_VALUE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
            $sJsCode = "jaxon.$("  . $sQuoteCharacter . $this->xValue  . $sQuoteCharacter . ").checked";
128
            break;
129 View Code Duplication
        case Jaxon::ELEMENT_INNERHTML:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
            $sJsCode = "jaxon.$(" . $sQuoteCharacter . $this->xValue . $sQuoteCharacter . ").innerHTML";
131
            break;
132
        case Jaxon::QUOTED_VALUE:
133
            $sJsCode = $sQuoteCharacter . addslashes($this->xValue) . $sQuoteCharacter;
134
            break;
135
        case Jaxon::BOOL_VALUE:
136
            $sJsCode = ($this->xValue) ? 'true' : 'false';
137
            break;
138
        case Jaxon::PAGE_NUMBER:
139
            $sJsCode = (string)$this->xValue;
140
            break;
141
        case Jaxon::NUMERIC_VALUE:
142
            $sJsCode = (string)$this->xValue;
143
            break;
144
        case Jaxon::JS_VALUE:
145
            if(is_array($this->xValue) || is_object($this->xValue))
146
            {
147
                // Unable to use double quotes here because they cannot be handled on client side.
148
                // So we are using simple quotes even if the Json standard recommends double quotes.
149
                $sJsCode = str_replace(['"'], ["'"], json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT));
150
            }
151
            else
152
            {
153
                $sJsCode = (string)$this->xValue;
154
            }
155
            break;
156
        }
157
        return $sJsCode;
158
    }
159
160
    /**
161
     * Magic function to generate the jQuery call.
162
     *
163
     * @return string
164
     */
165
    public function __toString()
166
    {
167
        return $this->getScript();
168
    }
169
170
    /**
171
     * Generate the jQuery call, when converting the response into json.
172
     *
173
     * This is a method of the JsonSerializable interface.
174
     *
175
     * @return string
176
     */
177
    public function jsonSerialize()
178
    {
179
        return $this->getScript();
180
    }
181
}
182