1 |
<?php
|
2 |
/*
|
3 |
* $Id: Hook.php 2702 2007-10-03 21:43:22Z Jonathan.Wage $
|
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.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Hook
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage Hook
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 2702 $
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
class Doctrine_Hook
|
34 |
{
|
35 |
/**
|
36 |
* @var Doctrine_Query $query the base query
|
37 |
*/
|
38 |
protected $query;
|
39 |
/**
|
40 |
* @var array $joins the optional joins of the base query
|
41 |
*/
|
42 |
protected $joins;
|
43 |
/**
|
44 |
* @var array $hooks hooks array
|
45 |
*/
|
46 |
protected $hooks = array(
|
47 |
'where',
|
48 |
'orderby',
|
49 |
'limit',
|
50 |
'offset'
|
51 |
);
|
52 |
/**
|
53 |
* @var array $fieldParsers custom field parsers array
|
54 |
* keys as field names in the format componentAlias.FieldName
|
55 |
* values as parser names / objects
|
56 |
*/
|
57 |
protected $fieldParsers = array();
|
58 |
|
59 |
/**
|
60 |
* @var array $typeParsers type parsers array
|
61 |
* keys as type names and values as parser names / objects
|
62 |
*/
|
63 |
protected $typeParsers = array(
|
64 |
'char' => 'Doctrine_Hook_WordLike',
|
65 |
'string' => 'Doctrine_Hook_WordLike',
|
66 |
'varchar' => 'Doctrine_Hook_WordLike',
|
67 |
'integer' => 'Doctrine_Hook_Integer',
|
68 |
'enum' => 'Doctrine_Hook_Integer',
|
69 |
'time' => 'Doctrine_Hook_Time',
|
70 |
'date' => 'Doctrine_Hook_Date',
|
71 |
);
|
72 |
|
73 |
/**
|
74 |
* @param Doctrine_Query $query the base query
|
75 |
*/
|
76 |
public function __construct($query)
|
77 |
{
|
78 |
if (is_string($query)) {
|
79 |
$this->query = new Doctrine_Query();
|
80 |
$this->query->parseQuery($query);
|
81 |
} elseif ($query instanceof Doctrine_Query) {
|
82 |
$this->query = $query;
|
83 |
} else {
|
84 |
throw new Doctrine_Exception('Constructor argument should be either Doctrine_Query object or valid DQL query');
|
85 |
}
|
86 |
|
87 |
$this->query->getQuery();
|
88 |
}
|
89 |
/**
|
90 |
* getQuery
|
91 |
*
|
92 |
* @return Doctrine_Query returns the query object associated with this hook
|
93 |
*/
|
94 |
public function getQuery()
|
95 |
{
|
96 |
return $this->query;
|
97 |
}
|
98 |
/**
|
99 |
* setTypeParser
|
100 |
*
|
101 |
* @param string $type type name
|
102 |
* @param string|object $parser parser name or custom parser object
|
103 |
*/
|
104 |
public function setTypeParser($type, $parser)
|
105 |
{
|
106 |
$this->typeParsers[$type] = $parser;
|
107 |
}
|
108 |
/**
|
109 |
* setFieldParser
|
110 |
*
|
111 |
* @param string $field field name
|
112 |
* @param string|object $parser parser name or custom parser object
|
113 |
*/
|
114 |
public function setFieldParser($field, $parser)
|
115 |
{
|
116 |
$this->fieldParsers[$field] = $parser;
|
117 |
}
|
118 |
/**
|
119 |
* hookWhere
|
120 |
* builds DQL query where part from given parameter array
|
121 |
*
|
122 |
* @param array $params an associative array containing field
|
123 |
* names and their values
|
124 |
* @return boolean whether or not the hooking was
|
125 |
*/
|
126 |
public function hookWhere($params)
|
127 |
{
|
128 |
if ( ! is_array($params)) {
|
129 |
return false;
|
130 |
}
|
131 |
foreach ($params as $name => $value) {
|
132 |
if ($value === '' || $value === '-') {
|
133 |
continue;
|
134 |
}
|
135 |
$e = explode('.', $name);
|
136 |
|
137 |
if (count($e) == 2) {
|
138 |
list($alias, $column) = $e;
|
139 |
|
140 |
$map = $this->query->getAliasDeclaration($alias);
|
141 |
$table = $map['table'];
|
142 |
|
143 |
if ( ! $table) {
|
144 |
throw new Doctrine_Exception('Unknown alias ' . $alias);
|
145 |
}
|
146 |
|
147 |
if ($def = $table->getDefinitionOf($column)) {
|
148 |
|
149 |
$def[0] = gettype($value);
|
150 |
if (isset($this->typeParsers[$def[0]])) {
|
151 |
$name = $this->typeParsers[$def[0]];
|
152 |
$parser = new $name;
|
153 |
}
|
154 |
|
155 |
$parser->parse($alias, $column, $value);
|
156 |
|
157 |
$this->query->addWhere($parser->getCondition(), $parser->getParams());
|
158 |
}
|
159 |
}
|
160 |
}
|
161 |
|
162 |
return true;
|
163 |
}
|
164 |
/**
|
165 |
* hookOrderBy
|
166 |
* builds DQL query orderby part from given parameter array
|
167 |
*
|
168 |
* @param array $params an array containing all fields which the built query
|
169 |
* should be ordered by
|
170 |
* @return boolean whether or not the hooking was successful
|
171 |
*/
|
172 |
public function hookOrderby($params)
|
173 |
{
|
174 |
if ( ! is_array($params)) {
|
175 |
return false;
|
176 |
}
|
177 |
foreach ($params as $name) {
|
178 |
$e = explode(' ', $name);
|
179 |
|
180 |
$order = 'ASC';
|
181 |
|
182 |
if (count($e) > 1) {
|
183 |
$order = ($e[1] == 'DESC') ? 'DESC' : 'ASC';
|
184 |
}
|
185 |
|
186 |
$e = explode('.', $e[0]);
|
187 |
|
188 |
if (count($e) == 2) {
|
189 |
list($alias, $column) = $e;
|
190 |
|
191 |
$map = $this->query->getAliasDeclaration($alias);
|
192 |
$table = $map['table'];
|
193 |
|
194 |
if ($def = $table->getDefinitionOf($column)) {
|
195 |
$this->query->addOrderBy($alias . '.' . $column . ' ' . $order);
|
196 |
}
|
197 |
}
|
198 |
}
|
199 |
return true;
|
200 |
}
|
201 |
/**
|
202 |
* @param integer $limit
|
203 |
*/
|
204 |
public function hookLimit($limit)
|
205 |
{
|
206 |
$this->query->limit((int) $limit);
|
207 |
}
|
208 |
/**
|
209 |
* @param integer $offset
|
210 |
*/
|
211 |
public function hookOffset($offset)
|
212 |
{
|
213 |
$this->query->offset((int) $offset);
|
214 |
}
|
215 |
} |