/lib/zend/Zend/Validate/Db/Abstract.php

https://github.com/nigeldaley/moodle · PHP · 298 lines · 126 code · 32 blank · 140 comment · 18 complexity · ec17920dfa114f372fcbb3aeb8c66742 MD5 · raw file

  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$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Class for Database record validation
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @uses Zend_Validate_Abstract
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract
  35. {
  36. /**
  37. * Error constants
  38. */
  39. const ERROR_NO_RECORD_FOUND = 'noRecordFound';
  40. const ERROR_RECORD_FOUND = 'recordFound';
  41. /**
  42. * @var array Message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found',
  46. self::ERROR_RECORD_FOUND => 'A record matching %value% was found',
  47. );
  48. /**
  49. * @var string
  50. */
  51. protected $_schema = null;
  52. /**
  53. * @var string
  54. */
  55. protected $_table = '';
  56. /**
  57. * @var string
  58. */
  59. protected $_field = '';
  60. /**
  61. * @var mixed
  62. */
  63. protected $_exclude = null;
  64. /**
  65. * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead
  66. *
  67. * @var unknown_type
  68. */
  69. protected $_adapter = null;
  70. /**
  71. * Provides basic configuration for use with Zend_Validate_Db Validators
  72. * Setting $exclude allows a single record to be excluded from matching.
  73. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  74. * to define the where clause added to the sql.
  75. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  76. *
  77. * The following option keys are supported:
  78. * 'table' => The database table to validate against
  79. * 'schema' => The schema keys
  80. * 'field' => The field to check for a match
  81. * 'exclude' => An optional where clause or field/value pair to exclude from the query
  82. * 'adapter' => An optional database adapter to use
  83. *
  84. * @param array|Zend_Config $options Options to use for this validator
  85. */
  86. public function __construct($options)
  87. {
  88. if ($options instanceof Zend_Config) {
  89. $options = $options->toArray();
  90. } else if (func_num_args() > 1) {
  91. $options = func_get_args();
  92. $temp['table'] = array_shift($options);
  93. $temp['field'] = array_shift($options);
  94. if (!empty($options)) {
  95. $temp['exclude'] = array_shift($options);
  96. }
  97. if (!empty($options)) {
  98. $temp['adapter'] = array_shift($options);
  99. }
  100. $options = $temp;
  101. }
  102. if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
  103. require_once 'Zend/Validate/Exception.php';
  104. throw new Zend_Validate_Exception('Table or Schema option missing!');
  105. }
  106. if (!array_key_exists('field', $options)) {
  107. require_once 'Zend/Validate/Exception.php';
  108. throw new Zend_Validate_Exception('Field option missing!');
  109. }
  110. if (array_key_exists('adapter', $options)) {
  111. $this->setAdapter($options['adapter']);
  112. }
  113. if (array_key_exists('exclude', $options)) {
  114. $this->setExclude($options['exclude']);
  115. }
  116. $this->setField($options['field']);
  117. if (array_key_exists('table', $options)) {
  118. $this->setTable($options['table']);
  119. }
  120. if (array_key_exists('schema', $options)) {
  121. $this->setSchema($options['schema']);
  122. }
  123. }
  124. /**
  125. * Returns the set adapter
  126. *
  127. * @return Zend_Db_Adapter
  128. */
  129. public function getAdapter()
  130. {
  131. return $this->_adapter;
  132. }
  133. /**
  134. * Sets a new database adapter
  135. *
  136. * @param Zend_Db_Adapter_Abstract $adapter
  137. * @return Zend_Validate_Db_Abstract
  138. */
  139. public function setAdapter($adapter)
  140. {
  141. if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
  142. require_once 'Zend/Validate/Exception.php';
  143. throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
  144. }
  145. $this->_adapter = $adapter;
  146. return $this;
  147. }
  148. /**
  149. * Returns the set exclude clause
  150. *
  151. * @return string|array
  152. */
  153. public function getExclude()
  154. {
  155. return $this->_exclude;
  156. }
  157. /**
  158. * Sets a new exclude clause
  159. *
  160. * @param string|array $exclude
  161. * @return Zend_Validate_Db_Abstract
  162. */
  163. public function setExclude($exclude)
  164. {
  165. $this->_exclude = $exclude;
  166. return $this;
  167. }
  168. /**
  169. * Returns the set field
  170. *
  171. * @return string|array
  172. */
  173. public function getField()
  174. {
  175. return $this->_field;
  176. }
  177. /**
  178. * Sets a new field
  179. *
  180. * @param string $field
  181. * @return Zend_Validate_Db_Abstract
  182. */
  183. public function setField($field)
  184. {
  185. $this->_field = (string) $field;
  186. return $this;
  187. }
  188. /**
  189. * Returns the set table
  190. *
  191. * @return string
  192. */
  193. public function getTable()
  194. {
  195. return $this->_table;
  196. }
  197. /**
  198. * Sets a new table
  199. *
  200. * @param string $table
  201. * @return Zend_Validate_Db_Abstract
  202. */
  203. public function setTable($table)
  204. {
  205. $this->_table = (string) $table;
  206. return $this;
  207. }
  208. /**
  209. * Returns the set schema
  210. *
  211. * @return string
  212. */
  213. public function getSchema()
  214. {
  215. return $this->_schema;
  216. }
  217. /**
  218. * Sets a new schema
  219. *
  220. * @param string $schema
  221. * @return Zend_Validate_Db_Abstract
  222. */
  223. public function setSchema($schema)
  224. {
  225. $this->_schema = $schema;
  226. return $this;
  227. }
  228. /**
  229. * Run query and returns matches, or null if no matches are found.
  230. *
  231. * @param String $value
  232. * @return Array when matches are found.
  233. */
  234. protected function _query($value)
  235. {
  236. /**
  237. * Check for an adapter being defined. if not, fetch the default adapter.
  238. */
  239. if ($this->_adapter === null) {
  240. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  241. if (null === $this->_adapter) {
  242. require_once 'Zend/Validate/Exception.php';
  243. throw new Zend_Validate_Exception('No database adapter present');
  244. }
  245. }
  246. /**
  247. * Build select object
  248. */
  249. $select = new Zend_Db_Select($this->_adapter);
  250. $select->from($this->_table, array($this->_field), $this->_schema)
  251. ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value);
  252. if ($this->_exclude !== null) {
  253. if (is_array($this->_exclude)) {
  254. $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']);
  255. } else {
  256. $select->where($this->_exclude);
  257. }
  258. }
  259. $select->limit(1);
  260. /**
  261. * Run query
  262. */
  263. $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);
  264. return $result;
  265. }
  266. }