Abstract.php 7.8 KB

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