|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Validate |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Abstract.php 23356 2010-11-18 15:59:10Z ralph $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Validate_Abstract |
|
24 */ |
|
25 require_once 'Zend/Validate/Abstract.php'; |
|
26 |
|
27 /** |
|
28 * Class for Database record validation |
|
29 * |
|
30 * @category Zend |
|
31 * @package Zend_Validate |
|
32 * @uses Zend_Validate_Abstract |
|
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 */ |
|
36 abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract |
|
37 { |
|
38 /** |
|
39 * Error constants |
|
40 */ |
|
41 const ERROR_NO_RECORD_FOUND = 'noRecordFound'; |
|
42 const ERROR_RECORD_FOUND = 'recordFound'; |
|
43 |
|
44 /** |
|
45 * @var array Message templates |
|
46 */ |
|
47 protected $_messageTemplates = array( |
|
48 self::ERROR_NO_RECORD_FOUND => "No record matching '%value%' was found", |
|
49 self::ERROR_RECORD_FOUND => "A record matching '%value%' was found", |
|
50 ); |
|
51 |
|
52 /** |
|
53 * @var string |
|
54 */ |
|
55 protected $_schema = null; |
|
56 |
|
57 /** |
|
58 * @var string |
|
59 */ |
|
60 protected $_table = ''; |
|
61 |
|
62 /** |
|
63 * @var string |
|
64 */ |
|
65 protected $_field = ''; |
|
66 |
|
67 /** |
|
68 * @var mixed |
|
69 */ |
|
70 protected $_exclude = null; |
|
71 |
|
72 /** |
|
73 * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead |
|
74 * |
|
75 * @var unknown_type |
|
76 */ |
|
77 protected $_adapter = null; |
|
78 |
|
79 /** |
|
80 * Select object to use. can be set, or will be auto-generated |
|
81 * @var Zend_Db_Select |
|
82 */ |
|
83 protected $_select; |
|
84 |
|
85 /** |
|
86 * Provides basic configuration for use with Zend_Validate_Db Validators |
|
87 * Setting $exclude allows a single record to be excluded from matching. |
|
88 * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys |
|
89 * to define the where clause added to the sql. |
|
90 * A database adapter may optionally be supplied to avoid using the registered default adapter. |
|
91 * |
|
92 * The following option keys are supported: |
|
93 * 'table' => The database table to validate against |
|
94 * 'schema' => The schema keys |
|
95 * 'field' => The field to check for a match |
|
96 * 'exclude' => An optional where clause or field/value pair to exclude from the query |
|
97 * 'adapter' => An optional database adapter to use |
|
98 * |
|
99 * @param array|Zend_Config $options Options to use for this validator |
|
100 */ |
|
101 public function __construct($options) |
|
102 { |
|
103 if ($options instanceof Zend_Db_Select) { |
|
104 $this->setSelect($options); |
|
105 return; |
|
106 } |
|
107 if ($options instanceof Zend_Config) { |
|
108 $options = $options->toArray(); |
|
109 } else if (func_num_args() > 1) { |
|
110 $options = func_get_args(); |
|
111 $temp['table'] = array_shift($options); |
|
112 $temp['field'] = array_shift($options); |
|
113 if (!empty($options)) { |
|
114 $temp['exclude'] = array_shift($options); |
|
115 } |
|
116 |
|
117 if (!empty($options)) { |
|
118 $temp['adapter'] = array_shift($options); |
|
119 } |
|
120 |
|
121 $options = $temp; |
|
122 } |
|
123 |
|
124 if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) { |
|
125 require_once 'Zend/Validate/Exception.php'; |
|
126 throw new Zend_Validate_Exception('Table or Schema option missing!'); |
|
127 } |
|
128 |
|
129 if (!array_key_exists('field', $options)) { |
|
130 require_once 'Zend/Validate/Exception.php'; |
|
131 throw new Zend_Validate_Exception('Field option missing!'); |
|
132 } |
|
133 |
|
134 if (array_key_exists('adapter', $options)) { |
|
135 $this->setAdapter($options['adapter']); |
|
136 } |
|
137 |
|
138 if (array_key_exists('exclude', $options)) { |
|
139 $this->setExclude($options['exclude']); |
|
140 } |
|
141 |
|
142 $this->setField($options['field']); |
|
143 if (array_key_exists('table', $options)) { |
|
144 $this->setTable($options['table']); |
|
145 } |
|
146 |
|
147 if (array_key_exists('schema', $options)) { |
|
148 $this->setSchema($options['schema']); |
|
149 } |
|
150 } |
|
151 |
|
152 /** |
|
153 * Returns the set adapter |
|
154 * |
|
155 * @return Zend_Db_Adapter |
|
156 */ |
|
157 public function getAdapter() |
|
158 { |
|
159 /** |
|
160 * Check for an adapter being defined. if not, fetch the default adapter. |
|
161 */ |
|
162 if ($this->_adapter === null) { |
|
163 $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter(); |
|
164 if (null === $this->_adapter) { |
|
165 require_once 'Zend/Validate/Exception.php'; |
|
166 throw new Zend_Validate_Exception('No database adapter present'); |
|
167 } |
|
168 } |
|
169 return $this->_adapter; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Sets a new database adapter |
|
174 * |
|
175 * @param Zend_Db_Adapter_Abstract $adapter |
|
176 * @return Zend_Validate_Db_Abstract |
|
177 */ |
|
178 public function setAdapter($adapter) |
|
179 { |
|
180 if (!($adapter instanceof Zend_Db_Adapter_Abstract)) { |
|
181 require_once 'Zend/Validate/Exception.php'; |
|
182 throw new Zend_Validate_Exception('Adapter option must be a database adapter!'); |
|
183 } |
|
184 |
|
185 $this->_adapter = $adapter; |
|
186 return $this; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Returns the set exclude clause |
|
191 * |
|
192 * @return string|array |
|
193 */ |
|
194 public function getExclude() |
|
195 { |
|
196 return $this->_exclude; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Sets a new exclude clause |
|
201 * |
|
202 * @param string|array $exclude |
|
203 * @return Zend_Validate_Db_Abstract |
|
204 */ |
|
205 public function setExclude($exclude) |
|
206 { |
|
207 $this->_exclude = $exclude; |
|
208 return $this; |
|
209 } |
|
210 |
|
211 /** |
|
212 * Returns the set field |
|
213 * |
|
214 * @return string|array |
|
215 */ |
|
216 public function getField() |
|
217 { |
|
218 return $this->_field; |
|
219 } |
|
220 |
|
221 /** |
|
222 * Sets a new field |
|
223 * |
|
224 * @param string $field |
|
225 * @return Zend_Validate_Db_Abstract |
|
226 */ |
|
227 public function setField($field) |
|
228 { |
|
229 $this->_field = (string) $field; |
|
230 return $this; |
|
231 } |
|
232 |
|
233 /** |
|
234 * Returns the set table |
|
235 * |
|
236 * @return string |
|
237 */ |
|
238 public function getTable() |
|
239 { |
|
240 return $this->_table; |
|
241 } |
|
242 |
|
243 /** |
|
244 * Sets a new table |
|
245 * |
|
246 * @param string $table |
|
247 * @return Zend_Validate_Db_Abstract |
|
248 */ |
|
249 public function setTable($table) |
|
250 { |
|
251 $this->_table = (string) $table; |
|
252 return $this; |
|
253 } |
|
254 |
|
255 /** |
|
256 * Returns the set schema |
|
257 * |
|
258 * @return string |
|
259 */ |
|
260 public function getSchema() |
|
261 { |
|
262 return $this->_schema; |
|
263 } |
|
264 |
|
265 /** |
|
266 * Sets a new schema |
|
267 * |
|
268 * @param string $schema |
|
269 * @return Zend_Validate_Db_Abstract |
|
270 */ |
|
271 public function setSchema($schema) |
|
272 { |
|
273 $this->_schema = $schema; |
|
274 return $this; |
|
275 } |
|
276 |
|
277 /** |
|
278 * Sets the select object to be used by the validator |
|
279 * |
|
280 * @param Zend_Db_Select $select |
|
281 * @return Zend_Validate_Db_Abstract |
|
282 */ |
|
283 public function setSelect($select) |
|
284 { |
|
285 if (!$select instanceof Zend_Db_Select) { |
|
286 throw new Zend_Validate_Exception('Select option must be a valid ' . |
|
287 'Zend_Db_Select object'); |
|
288 } |
|
289 $this->_select = $select; |
|
290 return $this; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Gets the select object to be used by the validator. |
|
295 * If no select object was supplied to the constructor, |
|
296 * then it will auto-generate one from the given table, |
|
297 * schema, field, and adapter options. |
|
298 * |
|
299 * @return Zend_Db_Select The Select object which will be used |
|
300 */ |
|
301 public function getSelect() |
|
302 { |
|
303 if (null === $this->_select) { |
|
304 $db = $this->getAdapter(); |
|
305 /** |
|
306 * Build select object |
|
307 */ |
|
308 $select = new Zend_Db_Select($db); |
|
309 $select->from($this->_table, array($this->_field), $this->_schema); |
|
310 if ($db->supportsParameters('named')) { |
|
311 $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named |
|
312 } else { |
|
313 $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional |
|
314 } |
|
315 if ($this->_exclude !== null) { |
|
316 if (is_array($this->_exclude)) { |
|
317 $select->where( |
|
318 $db->quoteIdentifier($this->_exclude['field'], true) . |
|
319 ' != ?', $this->_exclude['value'] |
|
320 ); |
|
321 } else { |
|
322 $select->where($this->_exclude); |
|
323 } |
|
324 } |
|
325 $select->limit(1); |
|
326 $this->_select = $select; |
|
327 } |
|
328 return $this->_select; |
|
329 } |
|
330 |
|
331 /** |
|
332 * Run query and returns matches, or null if no matches are found. |
|
333 * |
|
334 * @param String $value |
|
335 * @return Array when matches are found. |
|
336 */ |
|
337 protected function _query($value) |
|
338 { |
|
339 $select = $this->getSelect(); |
|
340 /** |
|
341 * Run query |
|
342 */ |
|
343 $result = $select->getAdapter()->fetchRow( |
|
344 $select, |
|
345 array('value' => $value), // this should work whether db supports positional or named params |
|
346 Zend_Db::FETCH_ASSOC |
|
347 ); |
|
348 |
|
349 return $result; |
|
350 } |
|
351 } |