# Coverage for Doctrine_Query_Having

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Having.php 3212 2007-11-24 18:58:33Z romanb $
4 | *
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 | *
17 | * This software consists of voluntary contributions made by many individuals
18 | * and is licensed under the LGPL. For more information, see
19 | * <http://www.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Query_Condition');
22 | /**
23 | * Doctrine_Query_Having
24 | *
25 | * @package Doctrine
26 | * @subpackage Query
27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28 | * @link www.phpdoctrine.org
29 | * @since 1.0
30 | * @version $Revision: 3212 $
31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32 | */
33 | class Doctrine_Query_Having extends Doctrine_Query_Condition
34 | {
35 | /**
36 | * DQL Aggregate Function parser
37 | *
38 | * @param string $func
39 | * @return mixed
40 | */
41 | private function parseAggregateFunction($func)
42 | {
43 | $pos = strpos($func, '(');
44 |
45 | if ($pos !== false) {
46 | $funcs = array();
47 |
48 | $name = substr($func, 0, $pos);
49 | $func = substr($func, ($pos + 1), -1);
50 | $params = $this->_tokenizer->bracketExplode($func, ',', '(', ')');
51 |
52 | foreach ($params as $k => $param) {
53 | $params[$k] = $this->parseAggregateFunction($param);
54 | }
55 |
56 | $funcs = $name . '(' . implode(', ', $params) . ')';
57 |
58 | return $funcs;
59 |
60 | } else {
61 | if ( ! is_numeric($func)) {
62 | $a = explode('.', $func);
63 |
64 | if (count($a) > 1) {
65 | $field = array_pop($a);
66 | $reference = implode('.', $a);
67 | $map = $this->query->load($reference, false);
68 | $field = $map['table']->getColumnName($field);
69 | $func = $this->query->getTableAlias($reference) . '.' . $field;
70 | } else {
71 | $field = end($a);
72 | $func = $this->query->getAggregateAlias($field);
73 | }
74 | return $func;
75 | } else {
76 | return $func;
77 | }
78 | }
79 | }
80 |
81 |
82 | /**
83 | * load
84 | * returns the parsed query part
85 | *
86 | * @param string $having
87 | * @return string
88 | */
89 | final public function load($having)
90 | {
91 | $tokens = $this->_tokenizer->bracketExplode($having, ' ', '(', ')');
92 | $part = $this->parseAggregateFunction(array_shift($tokens));
93 | $operator = array_shift($tokens);
94 | $value = implode(' ', $tokens);
95 | $part .= ' ' . $operator . ' ' . $value;
96 | return $part;
97 | }
98 | }