PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Validate/Db/Abstract.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 351 lines | 160 code | 33 blank | 158 comment | 23 complexity | f3a7a94a8eeced5835b02312a47f952c 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. * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * Select object to use. can be set, or will be auto-generated
  72. * @var Zend_Db_Select
  73. */
  74. protected $_select;
  75. /**
  76. * Provides basic configuration for use with Zend_Validate_Db Validators
  77. * Setting $exclude allows a single record to be excluded from matching.
  78. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  79. * to define the where clause added to the sql.
  80. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  81. *
  82. * The following option keys are supported:
  83. * 'table' => The database table to validate against
  84. * 'schema' => The schema keys
  85. * 'field' => The field to check for a match
  86. * 'exclude' => An optional where clause or field/value pair to exclude from the query
  87. * 'adapter' => An optional database adapter to use
  88. *
  89. * @param array|Zend_Config $options Options to use for this validator
  90. */
  91. public function __construct($options)
  92. {
  93. if ($options instanceof Zend_Db_Select) {
  94. $this->setSelect($options);
  95. return;
  96. }
  97. if ($options instanceof Zend_Config) {
  98. $options = $options->toArray();
  99. } else if (func_num_args() > 1) {
  100. $options = func_get_args();
  101. $temp['table'] = array_shift($options);
  102. $temp['field'] = array_shift($options);
  103. if (!empty($options)) {
  104. $temp['exclude'] = array_shift($options);
  105. }
  106. if (!empty($options)) {
  107. $temp['adapter'] = array_shift($options);
  108. }
  109. $options = $temp;
  110. }
  111. if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
  112. require_once 'Zend/Validate/Exception.php';
  113. throw new Zend_Validate_Exception('Table or Schema option missing!');
  114. }
  115. if (!array_key_exists('field', $options)) {
  116. require_once 'Zend/Validate/Exception.php';
  117. throw new Zend_Validate_Exception('Field option missing!');
  118. }
  119. if (array_key_exists('adapter', $options)) {
  120. $this->setAdapter($options['adapter']);
  121. }
  122. if (array_key_exists('exclude', $options)) {
  123. $this->setExclude($options['exclude']);
  124. }
  125. $this->setField($options['field']);
  126. if (array_key_exists('table', $options)) {
  127. $this->setTable($options['table']);
  128. }
  129. if (array_key_exists('schema', $options)) {
  130. $this->setSchema($options['schema']);
  131. }
  132. }
  133. /**
  134. * Returns the set adapter
  135. *
  136. * @return Zend_Db_Adapter
  137. */
  138. public function getAdapter()
  139. {
  140. /**
  141. * Check for an adapter being defined. if not, fetch the default adapter.
  142. */
  143. if ($this->_adapter === null) {
  144. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  145. if (null === $this->_adapter) {
  146. require_once 'Zend/Validate/Exception.php';
  147. throw new Zend_Validate_Exception('No database adapter present');
  148. }
  149. }
  150. return $this->_adapter;
  151. }
  152. /**
  153. * Sets a new database adapter
  154. *
  155. * @param Zend_Db_Adapter_Abstract $adapter
  156. * @return Zend_Validate_Db_Abstract
  157. */
  158. public function setAdapter($adapter)
  159. {
  160. if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
  161. require_once 'Zend/Validate/Exception.php';
  162. throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
  163. }
  164. $this->_adapter = $adapter;
  165. return $this;
  166. }
  167. /**
  168. * Returns the set exclude clause
  169. *
  170. * @return string|array
  171. */
  172. public function getExclude()
  173. {
  174. return $this->_exclude;
  175. }
  176. /**
  177. * Sets a new exclude clause
  178. *
  179. * @param string|array $exclude
  180. * @return Zend_Validate_Db_Abstract
  181. */
  182. public function setExclude($exclude)
  183. {
  184. $this->_exclude = $exclude;
  185. return $this;
  186. }
  187. /**
  188. * Returns the set field
  189. *
  190. * @return string|array
  191. */
  192. public function getField()
  193. {
  194. return $this->_field;
  195. }
  196. /**
  197. * Sets a new field
  198. *
  199. * @param string $field
  200. * @return Zend_Validate_Db_Abstract
  201. */
  202. public function setField($field)
  203. {
  204. $this->_field = (string) $field;
  205. return $this;
  206. }
  207. /**
  208. * Returns the set table
  209. *
  210. * @return string
  211. */
  212. public function getTable()
  213. {
  214. return $this->_table;
  215. }
  216. /**
  217. * Sets a new table
  218. *
  219. * @param string $table
  220. * @return Zend_Validate_Db_Abstract
  221. */
  222. public function setTable($table)
  223. {
  224. $this->_table = (string) $table;
  225. return $this;
  226. }
  227. /**
  228. * Returns the set schema
  229. *
  230. * @return string
  231. */
  232. public function getSchema()
  233. {
  234. return $this->_schema;
  235. }
  236. /**
  237. * Sets a new schema
  238. *
  239. * @param string $schema
  240. * @return Zend_Validate_Db_Abstract
  241. */
  242. public function setSchema($schema)
  243. {
  244. $this->_schema = $schema;
  245. return $this;
  246. }
  247. /**
  248. * Sets the select object to be used by the validator
  249. *
  250. * @param Zend_Db_Select $select
  251. * @return Zend_Validate_Db_Abstract
  252. */
  253. public function setSelect($select)
  254. {
  255. if (!$select instanceof Zend_Db_Select) {
  256. throw new Zend_Validate_Exception('Select option must be a valid ' .
  257. 'Zend_Db_Select object');
  258. }
  259. $this->_select = $select;
  260. return $this;
  261. }
  262. /**
  263. * Gets the select object to be used by the validator.
  264. * If no select object was supplied to the constructor,
  265. * then it will auto-generate one from the given table,
  266. * schema, field, and adapter options.
  267. *
  268. * @return Zend_Db_Select The Select object which will be used
  269. */
  270. public function getSelect()
  271. {
  272. if (null === $this->_select) {
  273. $db = $this->getAdapter();
  274. /**
  275. * Build select object
  276. */
  277. $select = new Zend_Db_Select($db);
  278. $select->from($this->_table, array($this->_field), $this->_schema);
  279. if ($db->supportsParameters('named')) {
  280. $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named
  281. } else {
  282. $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional
  283. }
  284. if ($this->_exclude !== null) {
  285. if (is_array($this->_exclude)) {
  286. $select->where(
  287. $db->quoteIdentifier($this->_exclude['field'], true) .
  288. ' != ?', $this->_exclude['value']
  289. );
  290. } else {
  291. $select->where($this->_exclude);
  292. }
  293. }
  294. $select->limit(1);
  295. $this->_select = $select;
  296. }
  297. return $this->_select;
  298. }
  299. /**
  300. * Run query and returns matches, or null if no matches are found.
  301. *
  302. * @param String $value
  303. * @return Array when matches are found.
  304. */
  305. protected function _query($value)
  306. {
  307. $select = $this->getSelect();
  308. /**
  309. * Run query
  310. */
  311. $result = $select->getAdapter()->fetchRow(
  312. $select,
  313. array('value' => $value), // this should work whether db supports positional or named params
  314. Zend_Db::FETCH_ASSOC
  315. );
  316. return $result;
  317. }
  318. }