/vendor/zend/Zend/Db/AbstractDb.php

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