Having.php 3.33 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Query_Condition');
/**
 * Doctrine_Query_Having
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
33 34
class Doctrine_Query_Having extends Doctrine_Query_Condition
{
doctrine's avatar
doctrine committed
35 36 37 38 39 40
    /**
     * DQL Aggregate Function parser
     *
     * @param string $func
     * @return mixed
     */
lsmith's avatar
lsmith committed
41 42
    private function parseAggregateFunction($func)
    {
43
        $pos = strpos($func, '(');
doctrine's avatar
doctrine committed
44

lsmith's avatar
lsmith committed
45
        if ($pos !== false) {
doctrine's avatar
doctrine committed
46 47 48 49
            $funcs  = array();

            $name   = substr($func, 0, $pos);
            $func   = substr($func, ($pos + 1), -1);
50
            $params = Doctrine_Query::bracketExplode($func, ',', '(', ')');
doctrine's avatar
doctrine committed
51

lsmith's avatar
lsmith committed
52
            foreach ($params as $k => $param) {
doctrine's avatar
doctrine committed
53 54 55
                $params[$k] = $this->parseAggregateFunction($param);
            }

56
            $funcs = $name . '(' . implode(', ', $params) . ')';
doctrine's avatar
doctrine committed
57 58 59 60

            return $funcs;

        } else {
lsmith's avatar
lsmith committed
61
            if ( ! is_numeric($func)) {
62
                $a = explode('.', $func);
doctrine's avatar
doctrine committed
63
                $field     = array_pop($a);
64
                $reference = implode('.', $a);
doctrine's avatar
doctrine committed
65
                $table     = $this->query->load($reference, false);
66 67
                $field     = $table->getColumnName($field);
                $func      = $this->query->getTableAlias($reference) . '.' . $field;
doctrine's avatar
doctrine committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81

                return $func;
            } else {
                return $func;
            }
        }
    }
    /**
     * load
     * returns the parsed query part
     *
     * @param string $having
     * @return string
     */
lsmith's avatar
lsmith committed
82 83
    final public function load($having)
    {
zYne's avatar
zYne committed
84
        $e = Doctrine_Query::bracketExplode($having, ' ', '(', ')');
doctrine's avatar
doctrine committed
85 86

        $r = array_shift($e);
zYne's avatar
zYne committed
87
        $t = explode('(', $r);
doctrine's avatar
doctrine committed
88 89 90 91

        $count = count($t);
        $r = $this->parseAggregateFunction($r);
        $operator  = array_shift($e);
zYne's avatar
zYne committed
92 93
        $value     = implode(' ', $e);
        $r .= ' ' . $operator . ' ' . $value;
doctrine's avatar
doctrine committed
94 95 96 97 98 99 100 101

        return $r;
    }
    /**
     * __toString
     *
     * @return string
     */
lsmith's avatar
lsmith committed
102 103
    public function __toString()
    {
zYne's avatar
zYne committed
104
        return ( ! empty($this->parts))?implode(' AND ', $this->parts):'';
doctrine's avatar
doctrine committed
105 106
    }
}