Completed
Push — 1.2 ( bc7404...476269 )
by David
15s
created

AggregateFunction::getDirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\MoufManager;
37
use Mouf\MoufInstanceDescriptor;
38
use SQLParser\Node\Traverser\NodeTraverser;
39
use SQLParser\Node\Traverser\VisitorInterface;
40
41
/**
42
 * This class represents an aggregation expression (like COUNT, SUM...) that is an SQL expression.
43
 *
44
 * @author David Négrier <[email protected]>
45
 */
46
class AggregateFunction implements NodeInterface
47
{
48
    private $functionName;
49
50
    /**
51
     * Returns the base expression (the string that generated this expression).
52
     *
53
     * @return string
54
     */
55
    public function getFunctionName()
56
    {
57
        return $this->functionName;
58
    }
59
60
    /**
61
     * Sets the base expression (the string that generated this expression).
62
     *
63
     * @Important
64
     *
65
     * @param string $functionName
66
     */
67
    public function setFunctionName($functionName)
68
    {
69
        $this->functionName = $functionName;
70
    }
71
72
    private $subTree;
73
74
    public function getSubTree()
75
    {
76
        return $this->subTree;
77
    }
78
79
    /**
80
     * Sets the subtree.
81
     *
82
     * @Important
83
     *
84
     * @param array<NodeInterface> $subTree
85
     */
86
    public function setSubTree($subTree)
87
    {
88
        $this->subTree = $subTree;
89
    }
90
91
    private $alias;
92
93
    public function getAlias()
94
    {
95
        return $this->alias;
96
    }
97
98
    /**
99
     * Sets the alias.
100
     *
101
     * @param string $alias
102
     */
103
    public function setAlias($alias)
104
    {
105
        $this->alias = $alias;
106
    }
107
108
    private $direction;
109
110
    public function getDirection()
111
    {
112
        return $this->direction;
113
    }
114
115
    /**
116
     * Sets the direction.
117
     *
118
     * @Important
119
     *
120
     * @param string $direction
121
     */
122
    public function setDirection($direction)
123
    {
124
        $this->direction = $direction;
125
    }
126
127
    /**
128
     * Returns a Mouf instance descriptor describing this object.
129
     *
130
     * @param MoufManager $moufManager
131
     *
132
     * @return MoufInstanceDescriptor
133
     */
134
    public function toInstanceDescriptor(MoufManager $moufManager)
135
    {
136
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
137
        $instanceDescriptor->getProperty('functionName')->setValue($this->functionName, $moufManager);
138
        $instanceDescriptor->getProperty('subTree')->setValue(NodeFactory::nodeToInstanceDescriptor($this->subTree, $moufManager));
139
        $instanceDescriptor->getProperty('alias')->setValue($this->alias, $moufManager);
140
141
        return $instanceDescriptor;
142
    }
143
144
    /**
145
     * Renders the object as a SQL string.
146
     *
147
     * @param Connection $dbConnection
148
     * @param array      $parameters
149
     * @param number     $indent
150
     * @param int        $conditionsMode
151
     *
152
     * @return string
153
     */
154
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
155
    {
156
        $subTreeSql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
157
        if ($subTreeSql !== null) {
158
            $sql = $this->functionName.'(';
159
            $sql .= $subTreeSql;
160
            $sql .= ')';
161
            if ($this->alias) {
162
                // defensive fix:
163
                $alias = is_array($this->alias) ? $this->alias['name'] : $this->alias;
164
165
                $sql .= ' AS '.$alias;
166
            }
167
            if ($this->direction) {
168
                $sql .= ' '.$this->direction;
169
            }
170
        } else {
171
            $sql = null;
172
        }
173
174
        return $sql;
175
    }
176
177
    /**
178
     * Walks the tree of nodes, calling the visitor passed in parameter.
179
     *
180
     * @param VisitorInterface $visitor
181
     */
182 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...
183
    {
184
        $node = $this;
185
        $result = $visitor->enterNode($node);
186
        if ($result instanceof NodeInterface) {
187
            $node = $result;
188
        }
189
        if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
190
            foreach ($this->subTree as $key => $operand) {
191
                $result2 = $operand->walk($visitor);
192
                if ($result2 === NodeTraverser::REMOVE_NODE) {
193
                    unset($this->subTree[$key]);
194
                } elseif ($result2 instanceof NodeInterface) {
195
                    $this->subTree[$key] = $result2;
196
                }
197
            }
198
        }
199
200
        return $visitor->leaveNode($node);
201
    }
202
}
203