PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 355 lines | 155 code | 33 blank | 167 comment | 23 complexity | f32f786acb59d6936c87698c0ac868ad 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-2015 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-2015 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. * @throws Zend_Validate_Exception
  91. */
  92. public function __construct($options)
  93. {
  94. if ($options instanceof Zend_Db_Select) {
  95. $this->setSelect($options);
  96. return;
  97. }
  98. if ($options instanceof Zend_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. #require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception('Table or Schema option missing!');
  115. }
  116. if (!array_key_exists('field', $options)) {
  117. #require_once 'Zend/Validate/Exception.php';
  118. throw new Zend_Validate_Exception('Field option missing!');
  119. }
  120. if (array_key_exists('adapter', $options)) {
  121. $this->setAdapter($options['adapter']);
  122. }
  123. if (array_key_exists('exclude', $options)) {
  124. $this->setExclude($options['exclude']);
  125. }
  126. $this->setField($options['field']);
  127. if (array_key_exists('table', $options)) {
  128. $this->setTable($options['table']);
  129. }
  130. if (array_key_exists('schema', $options)) {
  131. $this->setSchema($options['schema']);
  132. }
  133. }
  134. /**
  135. * Returns the set adapter
  136. *
  137. * @throws Zend_Validate_Exception
  138. * @return Zend_Db_Adapter
  139. */
  140. public function getAdapter()
  141. {
  142. /**
  143. * Check for an adapter being defined. if not, fetch the default adapter.
  144. */
  145. if ($this->_adapter === null) {
  146. $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
  147. if (null === $this->_adapter) {
  148. #require_once 'Zend/Validate/Exception.php';
  149. throw new Zend_Validate_Exception('No database adapter present');
  150. }
  151. }
  152. return $this->_adapter;
  153. }
  154. /**
  155. * Sets a new database adapter
  156. *
  157. * @param Zend_Db_Adapter_Abstract $adapter
  158. * @throws Zend_Validate_Exception
  159. * @return Zend_Validate_Db_Abstract
  160. */
  161. public function setAdapter($adapter)
  162. {
  163. if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
  164. #require_once 'Zend/Validate/Exception.php';
  165. throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
  166. }
  167. $this->_adapter = $adapter;
  168. return $this;
  169. }
  170. /**
  171. * Returns the set exclude clause
  172. *
  173. * @return string|array
  174. */
  175. public function getExclude()
  176. {
  177. return $this->_exclude;
  178. }
  179. /**
  180. * Sets a new exclude clause
  181. *
  182. * @param string|array $exclude
  183. * @return Zend_Validate_Db_Abstract
  184. */
  185. public function setExclude($exclude)
  186. {
  187. $this->_exclude = $exclude;
  188. return $this;
  189. }
  190. /**
  191. * Returns the set field
  192. *
  193. * @return string|array
  194. */
  195. public function getField()
  196. {
  197. return $this->_field;
  198. }
  199. /**
  200. * Sets a new field
  201. *
  202. * @param string $field
  203. * @return Zend_Validate_Db_Abstract
  204. */
  205. public function setField($field)
  206. {
  207. $this->_field = (string) $field;
  208. return $this;
  209. }
  210. /**
  211. * Returns the set table
  212. *
  213. * @return string
  214. */
  215. public function getTable()
  216. {
  217. return $this->_table;
  218. }
  219. /**
  220. * Sets a new table
  221. *
  222. * @param string $table
  223. * @return Zend_Validate_Db_Abstract
  224. */
  225. public function setTable($table)
  226. {
  227. $this->_table = (string) $table;
  228. return $this;
  229. }
  230. /**
  231. * Returns the set schema
  232. *
  233. * @return string
  234. */
  235. public function getSchema()
  236. {
  237. return $this->_schema;
  238. }
  239. /**
  240. * Sets a new schema
  241. *
  242. * @param string $schema
  243. * @return Zend_Validate_Db_Abstract
  244. */
  245. public function setSchema($schema)
  246. {
  247. $this->_schema = $schema;
  248. return $this;
  249. }
  250. /**
  251. * Sets the select object to be used by the validator
  252. *
  253. * @param Zend_Db_Select $select
  254. * @throws Zend_Validate_Exception
  255. * @return Zend_Validate_Db_Abstract
  256. */
  257. public function setSelect($select)
  258. {
  259. if (!$select instanceof Zend_Db_Select) {
  260. throw new Zend_Validate_Exception('Select option must be a valid ' .
  261. 'Zend_Db_Select object');
  262. }
  263. $this->_select = $select;
  264. return $this;
  265. }
  266. /**
  267. * Gets the select object to be used by the validator.
  268. * If no select object was supplied to the constructor,
  269. * then it will auto-generate one from the given table,
  270. * schema, field, and adapter options.
  271. *
  272. * @return Zend_Db_Select The Select object which will be used
  273. */
  274. public function getSelect()
  275. {
  276. if (null === $this->_select) {
  277. $db = $this->getAdapter();
  278. /**
  279. * Build select object
  280. */
  281. $select = new Zend_Db_Select($db);
  282. $select->from($this->_table, array($this->_field), $this->_schema);
  283. if ($db->supportsParameters('named')) {
  284. $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named
  285. } else {
  286. $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional
  287. }
  288. if ($this->_exclude !== null) {
  289. if (is_array($this->_exclude)) {
  290. $select->where(
  291. $db->quoteIdentifier($this->_exclude['field'], true) .
  292. ' != ?', $this->_exclude['value']
  293. );
  294. } else {
  295. $select->where($this->_exclude);
  296. }
  297. }
  298. $select->limit(1);
  299. $this->_select = $select;
  300. }
  301. return $this->_select;
  302. }
  303. /**
  304. * Run query and returns matches, or null if no matches are found.
  305. *
  306. * @param String $value
  307. * @return Array when matches are found.
  308. */
  309. protected function _query($value)
  310. {
  311. $select = $this->getSelect();
  312. /**
  313. * Run query
  314. */
  315. $result = $select->getAdapter()->fetchRow(
  316. $select,
  317. array('value' => $value), // this should work whether db supports positional or named params
  318. Zend_Db::FETCH_ASSOC
  319. );
  320. return $result;
  321. }
  322. }