Parameter::toInstanceDescriptor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * expression-types.php.
5
 *
6
 *
7
 * Copyright (c) 2010-2013, Justin Swanhart
8
 * with contributions by André Rothe <[email protected], [email protected]>
9
 * and David Négrier <[email protected]>
10
 *
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification,
14
 * are permitted provided that the following conditions are met:
15
 *
16
 *   * Redistributions of source code must retain the above copyright notice,
17
 *     this list of conditions and the following disclaimer.
18
 *   * Redistributions in binary form must reproduce the above copyright notice,
19
 *     this list of conditions and the following disclaimer in the documentation
20
 *     and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31
 * DAMAGE.
32
 */
33
namespace SQLParser\Node;
34
35
use Doctrine\DBAL\Connection;
36
use Mouf\MoufInstanceDescriptor;
37
use Mouf\MoufManager;
38
use SQLParser\Node\Traverser\VisitorInterface;
39
40
/**
41
 * This class represents a parameter (as in parameterized query).
42
 *
43
 * @author David Négrier <[email protected]>
44
 */
45
class Parameter implements NodeInterface
46
{
47
    protected $name;
48
    protected $discardedOnNull = true;
49
50
    /**
51
     * Returns the name.
52
     *
53
     * @return string
54
     */
55
    public function getName()
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * Sets the name.
62
     * If the name ends with !, the parameter will be considered not nullable (regarding magicparameter settings).
63
     *
64
     * @Important
65
     *
66
     * @param string $name
67
     */
68
    public function setName($name)
69
    {
70
        if (strrpos($name, '!') === strlen($name) - 1) {
71
            $this->name = substr($name, 0, strlen($name) - 1);
72
            $this->discardedOnNull = false;
73
        } else {
74
            $this->name = $name;
75
            $this->discardedOnNull = true;
76
        }
77
    }
78
79
    /**
80
     * @var string
81
     */
82
    protected $autoPrepend;
83
84
    /**
85
     * @var string
86
     */
87
    protected $autoAppend;
88
89
    /**
90
     * @return string
91
     */
92
    public function getAutoPrepend()
93
    {
94
        return $this->autoPrepend;
95
    }
96
97
    /**
98
     * Sets a string that will automatically be appended to the parameter, if the parameter is available.
99
     * Very useful to automatically add "%" to a parameter used in a LIKE.
100
     *
101
     * @Important IfSet
102
     *
103
     * @param string $autoPrepend
104
     */
105
    public function setAutoPrepend($autoPrepend)
106
    {
107
        $this->autoPrepend = $autoPrepend;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getAutoAppend()
116
    {
117
        return $this->autoAppend;
118
    }
119
120
    /**
121
     * Sets a string that will automatically be preprended to the parameter, if the parameter is available.
122
     * Very useful to automatically add "%" to a parameter used in a LIKE.
123
     *
124
     * @Important IfSet
125
     *
126
     * @param string $autoAppend
127
     */
128
    public function setAutoAppend($autoAppend)
129
    {
130
        $this->autoAppend = $autoAppend;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Returns a Mouf instance descriptor describing this object.
137
     *
138
     * @param MoufManager $moufManager
139
     *
140
     * @return MoufInstanceDescriptor
141
     */
142
    public function toInstanceDescriptor(MoufManager $moufManager)
143
    {
144
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
145
        $instanceDescriptor->getProperty('name')->setValue($this->name);
146
        $instanceDescriptor->getProperty('autoPrepend')->setValue($this->autoPrepend);
147
        $instanceDescriptor->getProperty('autoAppend')->setValue($this->autoAppend);
148
149
        return $instanceDescriptor;
150
    }
151
152
    /**
153
     * Renders the object as a SQL string.
154
     *
155
     * @param Connection $dbConnection
156
     * @param array      $parameters
157
     * @param number     $indent
158
     * @param int        $conditionsMode
159
     *
160
     * @return string
161
     */
162
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
163
    {
164
        if (isset($parameters[$this->name])) {
165
            if ($dbConnection) {
166
                if (is_array($parameters[$this->name])) {
167
                    return '('.implode(',', array_map(function ($item) use ($dbConnection) {
168
                            return $dbConnection->quote($this->autoPrepend.$item.$this->autoAppend);
169
                        }, $parameters[$this->name])).')';
170
                } else {
171
                    return $dbConnection->quote($this->autoPrepend.$parameters[$this->name].$this->autoAppend);
172
                }
173
            } else {
174
                if ($parameters[$this->name] === null) {
175
                    return 'null';
176
                } else {
177
                    if (is_array($parameters[$this->name])) {
178
                        return '('.implode(',', array_map(function ($item) {
179
                            return "'".addslashes($this->autoPrepend.$item.$this->autoAppend)."'";
180
                        }, $parameters[$this->name])).')';
181
                    } else {
182
                        return "'".addslashes($this->autoPrepend.$parameters[$this->name].$this->autoAppend)."'";
183
                    }
184
                }
185
            }
186
        } elseif (!$this->isDiscardedOnNull()) {
187
            return 'null';
188
        } else {
189
            return ':'.$this->name;
190
        }
191
    }
192
193
    /**
194
     * Walks the tree of nodes, calling the visitor passed in parameter.
195
     *
196
     * @param VisitorInterface $visitor
197
     *
198
     * @return NodeInterface|null|string Can return null if nothing is to be done or a node that should replace this node, or NodeTraverser::REMOVE_NODE to remove the node
199
     */
200 View Code Duplication
    public function walk(VisitorInterface $visitor)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
201
    {
202
        $node = $this;
203
        $result = $visitor->enterNode($node);
204
        if ($result instanceof NodeInterface) {
205
            $node = $result;
206
        }
207
208
        return $visitor->leaveNode($node);
209
    }
210
211
    /**
212
     * Returns whether the parameter can be discarded if provided value is null.
213
     *
214
     * @return bool
215
     */
216
    public function isDiscardedOnNull()
217
    {
218
        return $this->discardedOnNull;
219
    }
220
}
221