2
0

Abstract.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  90. // trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE);
  91. $options = func_get_args();
  92. $temp['table'] = array_shift($options);
  93. $temp['field'] = array_shift($options);
  94. if (!empty($options)) {
  95. $options['exclude'] = array_shift($options);
  96. }
  97. if (!empty($options)) {
  98. $options['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. if (!($options['adapter'] instanceof Zend_Db_Adapter_Abstract)) {
  112. require_once 'Zend/Validate/Exception.php';
  113. throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
  114. }
  115. $this->_adapter = $options['adapter'];
  116. }
  117. if (array_key_exists('exclude', $options)) {
  118. $this->_exclude = $options['exclude'];
  119. }
  120. $this->_field = (string) $options['field'];
  121. if (array_key_exists('table', $options)) {
  122. $this->_table = (string) $options['table'];
  123. }
  124. if (array_key_exists('schema', $options)) {
  125. $this->_schema = $options['schema'];
  126. }
  127. }
  128. /**
  129. * Run query and returns matches, or null if no matches are found.
  130. *
  131. * @param String $value
  132. * @return Array when matches are found.
  133. */
  134. protected function _query($value)
  135. {
  136. /**
  137. * Check for an adapter being defined. if not, fetch the default adapter.
  138. */
  139. if ($this->_adapter === null) {
  140. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  141. if (null === $this->_adapter) {
  142. require_once 'Zend/Validate/Exception.php';
  143. throw new Zend_Validate_Exception('No database adapter present');
  144. }
  145. }
  146. /**
  147. * Build select object
  148. */
  149. $select = new Zend_Db_Select($this->_adapter);
  150. $select->from($this->_table, array($this->_field), $this->_schema)
  151. ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value);
  152. if ($this->_exclude !== null) {
  153. if (is_array($this->_exclude)) {
  154. $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' != ?', $this->_exclude['value']);
  155. } else {
  156. $select->where($this->_exclude);
  157. }
  158. }
  159. $select->limit(1);
  160. /**
  161. * Run query
  162. */
  163. $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);
  164. return $result;
  165. }
  166. }