Hook.php 7.05 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
                              'enum'      => 'Doctrine_Hook_Integer',
zYne's avatar
zYne committed
69 70
                              'time'      => 'Doctrine_Hook_Time',
                              'date'      => 'Doctrine_Hook_Date',
71
                              );
zYne's avatar
zYne committed
72 73 74 75

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

lsmith's avatar
lsmith committed
137
            if (count($e) == 2) {
138
                list($alias, $column) = $e;
zYne's avatar
zYne committed
139

zYne's avatar
zYne committed
140 141
                $map   = $this->query->getAliasDeclaration($alias);
                $table = $map['table'];
zYne's avatar
zYne committed
142

zYne's avatar
zYne committed
143
                if ( ! $table) {
zYne's avatar
zYne committed
144
                    throw new Doctrine_Exception('Unknown alias ' . $alias);
zYne's avatar
zYne committed
145 146
                }

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

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

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

157
                    $this->query->addWhere($parser->getCondition(), $parser->getParams());
zYne's avatar
zYne committed
158
                }
159 160
            }
        }
lsmith's avatar
lsmith committed
161

zYne's avatar
zYne committed
162
        return true;
zYne's avatar
zYne committed
163
    }
164
    /**
zYne's avatar
zYne committed
165 166 167 168 169
     * 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
170
     * @return boolean              whether or not the hooking was successful
171
     */
lsmith's avatar
lsmith committed
172 173
    public function hookOrderby($params)
    {
lsmith's avatar
lsmith committed
174
        if ( ! is_array($params)) {
zYne's avatar
zYne committed
175
            return false;
lsmith's avatar
lsmith committed
176 177
        }
        foreach ($params as $name) {
zYne's avatar
zYne committed
178 179 180 181
            $e = explode(' ', $name);

            $order = 'ASC';

lsmith's avatar
lsmith committed
182
            if (count($e) > 1) {
zYne's avatar
zYne committed
183 184 185 186 187
                $order = ($e[1] == 'DESC') ? 'DESC' : 'ASC';
            }

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

lsmith's avatar
lsmith committed
188
            if (count($e) == 2) {
zYne's avatar
zYne committed
189
                list($alias, $column) = $e;
zYne's avatar
zYne committed
190

zYne's avatar
zYne committed
191 192
                $map   = $this->query->getAliasDeclaration($alias);
                $table = $map['table'];
zYne's avatar
zYne committed
193

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