Hook.php 6.89 KB
Newer Older
zYne's avatar
zYne committed
1 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
<?php
/*
 *  $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_Hook
 *
 * @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_Hook
{
zYne's avatar
zYne committed
35 36 37
    /**
     * @var Doctrine_Query $query           the base query
     */
38
    protected $query;
zYne's avatar
zYne committed
39 40 41
    /**
     * @var array $joins                    the optional joins of the base query
     */
42 43 44 45 46 47 48 49 50 51 52
    protected $joins;
    /**
     * @var array $hooks                    hooks array
     */
    protected $hooks        = array(
                             'where',
                             'orderby',
                             'limit',
                             'offset'
                              );
    /**
zYne's avatar
zYne committed
53 54 55
     * @var array $fieldParsers             custom field parsers array
     *                                      keys as field names in the format componentAlias.FieldName
     *                                      values as parser names / objects
56 57 58 59
     */
    protected $fieldParsers = array();

    /**
zYne's avatar
zYne committed
60 61
     * @var array $typeParsers              type parsers array
     *                                      keys as type names and values as parser names / objects
62 63 64 65
     */
    protected $typeParsers  = array(
                              'char'      => 'Doctrine_Hook_WordLike',
                              'string'    => 'Doctrine_Hook_WordLike',
66
                              'varchar'   => 'Doctrine_Hook_WordLike',
67
                              'integer'   => 'Doctrine_Hook_Integer',
zYne's avatar
zYne committed
68 69
                              'time'      => 'Doctrine_Hook_Time',
                              'date'      => 'Doctrine_Hook_Date',
70
                              );
zYne's avatar
zYne committed
71 72 73 74

    /**
     * @param Doctrine_Query $query         the base query
     */
lsmith's avatar
lsmith committed
75 76
    public function __construct($query)
    {
lsmith's avatar
lsmith committed
77
        if (is_string($query)) {
zYne's avatar
zYne committed
78 79
            $this->query = new Doctrine_Query();
            $this->query->parseQuery($query);
lsmith's avatar
lsmith committed
80
        } elseif ($query instanceof Doctrine_Query) {
zYne's avatar
zYne committed
81
            $this->query = $query;
zYne's avatar
zYne committed
82 83
        } else {
            throw new Doctrine_Exception('Constructor argument should be either Doctrine_Query object or valid DQL query');      	
zYne's avatar
zYne committed
84
        }
zYne's avatar
zYne committed
85 86
        
        $this->query->getQuery();
zYne's avatar
zYne committed
87
    }
88 89 90 91 92
    /**
     * getQuery
     *
     * @return Doctrine_Query       returns the query object associated with this hook
     */
lsmith's avatar
lsmith committed
93 94
    public function getQuery()
    {
95 96
        return $this->query;
    }
zYne's avatar
zYne committed
97 98 99 100 101 102 103
    /**
     * setTypeParser
     *
     * @param string $type              type name
     * @param string|object $parser     parser name or custom parser object
     */
    public function setTypeParser($type, $parser) 
lsmith's avatar
lsmith committed
104
    {
zYne's avatar
zYne committed
105
        $this->typeParsers[$type] = $parser;
zYne's avatar
zYne committed
106
    }
zYne's avatar
zYne committed
107 108 109 110 111 112 113
    /**
     * setFieldParser
     *
     * @param string $field             field name
     * @param string|object $parser     parser name or custom parser object
     */
    public function setFieldParser($field, $parser)
lsmith's avatar
lsmith committed
114
    {
zYne's avatar
zYne committed
115
        $this->fieldParsers[$field] = $parser;
zYne's avatar
zYne committed
116
    }
117 118
    /**
     * hookWhere
zYne's avatar
zYne committed
119
     * builds DQL query where part from given parameter array
120
     *
lsmith's avatar
lsmith committed
121
     * @param array $params         an associative array containing field
zYne's avatar
zYne committed
122 123
     *                              names and their values
     * @return boolean              whether or not the hooking was
124
     */
lsmith's avatar
lsmith committed
125 126
    public function hookWhere($params)
    {
lsmith's avatar
lsmith committed
127
        if ( ! is_array($params)) {
zYne's avatar
zYne committed
128
            return false;
lsmith's avatar
lsmith committed
129 130
        }
        foreach ($params as $name => $value) {
131
            $e = explode('.', $name);
zYne's avatar
zYne committed
132

lsmith's avatar
lsmith committed
133
            if (count($e) == 2) {
134
                list($alias, $column) = $e;
zYne's avatar
zYne committed
135

zYne's avatar
zYne committed
136 137
                $map   = $this->query->getAliasDeclaration($alias);
                $table = $map['table'];
zYne's avatar
zYne committed
138

zYne's avatar
zYne committed
139
                if ( ! $table) {
zYne's avatar
zYne committed
140
                    throw new Doctrine_Exception('Unknown alias ' . $alias);
zYne's avatar
zYne committed
141 142
                }

lsmith's avatar
lsmith committed
143
                if ($def = $table->getDefinitionOf($column)) {
zYne's avatar
zYne committed
144

zYne's avatar
zYne committed
145
                $def[0] = gettype($value);
lsmith's avatar
lsmith committed
146
                    if (isset($this->typeParsers[$def[0]])) {
147 148 149 150 151 152
                        $name   = $this->typeParsers[$def[0]];
                        $parser = new $name;
                    }

                    $parser->parse($alias, $column, $value);

153
                    $this->query->addWhere($parser->getCondition(), $parser->getParams());
zYne's avatar
zYne committed
154
                }
155 156
            }
        }
lsmith's avatar
lsmith committed
157

zYne's avatar
zYne committed
158
        return true;
zYne's avatar
zYne committed
159
    }
160
    /**
zYne's avatar
zYne committed
161 162 163 164 165
     * hookOrderBy
     * builds DQL query orderby part from given parameter array
     *
     * @param array $params         an array containing all fields which the built query
     *                              should be ordered by
166
     * @return boolean              whether or not the hooking was successful
167
     */
lsmith's avatar
lsmith committed
168 169
    public function hookOrderby($params)
    {
lsmith's avatar
lsmith committed
170
        if ( ! is_array($params)) {
zYne's avatar
zYne committed
171
            return false;
lsmith's avatar
lsmith committed
172 173
        }
        foreach ($params as $name) {
zYne's avatar
zYne committed
174 175 176 177
            $e = explode(' ', $name);

            $order = 'ASC';

lsmith's avatar
lsmith committed
178
            if (count($e) > 1) {
zYne's avatar
zYne committed
179 180 181 182 183
                $order = ($e[1] == 'DESC') ? 'DESC' : 'ASC';
            }

            $e = explode('.', $e[0]);

lsmith's avatar
lsmith committed
184
            if (count($e) == 2) {
zYne's avatar
zYne committed
185
                list($alias, $column) = $e;
zYne's avatar
zYne committed
186

zYne's avatar
zYne committed
187 188
                $map   = $this->query->getAliasDeclaration($alias);
                $table = $map['table'];
zYne's avatar
zYne committed
189

zYne's avatar
zYne committed
190
                if ($def = $table->getDefinitionOf($column)) {   
zYne's avatar
zYne committed
191
                    $this->query->addOrderBy($alias . '.' . $column . ' ' . $order);
zYne's avatar
zYne committed
192
                }
zYne's avatar
zYne committed
193 194
            }
        }
195
        return true;
zYne's avatar
zYne committed
196 197 198 199
    }
    /**
     * @param integer $limit
     */
lsmith's avatar
lsmith committed
200 201
    public function hookLimit($limit)
    {
zYne's avatar
zYne committed
202
        $this->query->limit((int) $limit);
zYne's avatar
zYne committed
203 204 205 206
    }
    /**
     * @param integer $offset
     */
lsmith's avatar
lsmith committed
207 208
    public function hookOffset($offset)
    {
zYne's avatar
zYne committed
209
        $this->query->offset((int) $offset);
zYne's avatar
zYne committed
210 211
    }
}