PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Validator/Db/AbstractDb.php

https://bitbucket.org/juan_sanchez/aiyellow
PHP | 327 lines | 154 code | 41 blank | 132 comment | 17 complexity | d695457f6aacd7f852b8f2d7e4bc879b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator\Db;
  10. use Traversable;
  11. use Zend\Db\Adapter\Adapter as DbAdapter;
  12. use Zend\Db\Adapter\Driver\DriverInterface as DbDriverInterface;
  13. use Zend\Db\Sql\Sql;
  14. use Zend\Db\Sql\Select;
  15. use Zend\Db\Sql\TableIdentifier;
  16. use Zend\Stdlib\ArrayUtils;
  17. use Zend\Validator\AbstractValidator;
  18. use Zend\Validator\Exception;
  19. /**
  20. * Class for Database record validation
  21. */
  22. abstract class AbstractDb extends AbstractValidator
  23. {
  24. /**
  25. * Error constants
  26. */
  27. const ERROR_NO_RECORD_FOUND = 'noRecordFound';
  28. const ERROR_RECORD_FOUND = 'recordFound';
  29. /**
  30. * @var array Message templates
  31. */
  32. protected $messageTemplates = array(
  33. self::ERROR_NO_RECORD_FOUND => "No record matching the input was found",
  34. self::ERROR_RECORD_FOUND => "A record matching the input was found",
  35. );
  36. /**
  37. * Select object to use. can be set, or will be auto-generated
  38. *
  39. * @var Select
  40. */
  41. protected $select;
  42. /**
  43. * @var string
  44. */
  45. protected $schema = null;
  46. /**
  47. * @var string
  48. */
  49. protected $table = '';
  50. /**
  51. * @var string
  52. */
  53. protected $field = '';
  54. /**
  55. * @var mixed
  56. */
  57. protected $exclude = null;
  58. /**
  59. * Database adapter to use. If null isValid() will throw an exception
  60. *
  61. * @var \Zend\Db\Adapter\Adapter
  62. */
  63. protected $adapter = null;
  64. /**
  65. * Provides basic configuration for use with Zend\Validator\Db Validators
  66. * Setting $exclude allows a single record to be excluded from matching.
  67. * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
  68. * to define the where clause added to the sql.
  69. * A database adapter may optionally be supplied to avoid using the registered default adapter.
  70. *
  71. * The following option keys are supported:
  72. * 'table' => The database table to validate against
  73. * 'schema' => The schema keys
  74. * 'field' => The field to check for a match
  75. * 'exclude' => An optional where clause or field/value pair to exclude from the query
  76. * 'adapter' => An optional database adapter to use
  77. *
  78. * @param array|Traversable|Select $options Options to use for this validator
  79. * @throws \Zend\Validator\Exception\InvalidArgumentException
  80. */
  81. public function __construct($options = null)
  82. {
  83. parent::__construct($options);
  84. if ($options instanceof Select) {
  85. $this->setSelect($options);
  86. return;
  87. }
  88. if ($options instanceof Traversable) {
  89. $options = ArrayUtils::iteratorToArray($options);
  90. } elseif (func_num_args() > 1) {
  91. $options = func_get_args();
  92. $firstArgument = array_shift($options);
  93. if (is_array($firstArgument)) {
  94. $temp = ArrayUtils::iteratorToArray($firstArgument);
  95. } else {
  96. $temp['table'] = $firstArgument;
  97. }
  98. $temp['field'] = array_shift($options);
  99. if (!empty($options)) {
  100. $temp['exclude'] = array_shift($options);
  101. }
  102. if (!empty($options)) {
  103. $temp['adapter'] = array_shift($options);
  104. }
  105. $options = $temp;
  106. }
  107. if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
  108. throw new Exception\InvalidArgumentException('Table or Schema option missing!');
  109. }
  110. if (!array_key_exists('field', $options)) {
  111. throw new Exception\InvalidArgumentException('Field option missing!');
  112. }
  113. if (array_key_exists('adapter', $options)) {
  114. $this->setAdapter($options['adapter']);
  115. }
  116. if (array_key_exists('exclude', $options)) {
  117. $this->setExclude($options['exclude']);
  118. }
  119. $this->setField($options['field']);
  120. if (array_key_exists('table', $options)) {
  121. $this->setTable($options['table']);
  122. }
  123. if (array_key_exists('schema', $options)) {
  124. $this->setSchema($options['schema']);
  125. }
  126. }
  127. /**
  128. * Returns the set adapter
  129. *
  130. * @throws \Zend\Validator\Exception\RuntimeException When no database adapter is defined
  131. * @return DbAdapter
  132. */
  133. public function getAdapter()
  134. {
  135. return $this->adapter;
  136. }
  137. /**
  138. * Sets a new database adapter
  139. *
  140. * @param DbAdapter $adapter
  141. * @return self Provides a fluent interface
  142. */
  143. public function setAdapter(DbAdapter $adapter)
  144. {
  145. $this->adapter = $adapter;
  146. return $this;
  147. }
  148. /**
  149. * Returns the set exclude clause
  150. *
  151. * @return string|array
  152. */
  153. public function getExclude()
  154. {
  155. return $this->exclude;
  156. }
  157. /**
  158. * Sets a new exclude clause
  159. *
  160. * @param string|array $exclude
  161. * @return self Provides a fluent interface
  162. */
  163. public function setExclude($exclude)
  164. {
  165. $this->exclude = $exclude;
  166. return $this;
  167. }
  168. /**
  169. * Returns the set field
  170. *
  171. * @return string|array
  172. */
  173. public function getField()
  174. {
  175. return $this->field;
  176. }
  177. /**
  178. * Sets a new field
  179. *
  180. * @param string $field
  181. * @return AbstractDb
  182. */
  183. public function setField($field)
  184. {
  185. $this->field = (string) $field;
  186. return $this;
  187. }
  188. /**
  189. * Returns the set table
  190. *
  191. * @return string
  192. */
  193. public function getTable()
  194. {
  195. return $this->table;
  196. }
  197. /**
  198. * Sets a new table
  199. *
  200. * @param string $table
  201. * @return self Provides a fluent interface
  202. */
  203. public function setTable($table)
  204. {
  205. $this->table = (string) $table;
  206. return $this;
  207. }
  208. /**
  209. * Returns the set schema
  210. *
  211. * @return string
  212. */
  213. public function getSchema()
  214. {
  215. return $this->schema;
  216. }
  217. /**
  218. * Sets a new schema
  219. *
  220. * @param string $schema
  221. * @return self Provides a fluent interface
  222. */
  223. public function setSchema($schema)
  224. {
  225. $this->schema = $schema;
  226. return $this;
  227. }
  228. /**
  229. * Sets the select object to be used by the validator
  230. *
  231. * @param Select $select
  232. * @return self Provides a fluent interface
  233. */
  234. public function setSelect(Select $select)
  235. {
  236. $this->select = $select;
  237. return $this;
  238. }
  239. /**
  240. * Gets the select object to be used by the validator.
  241. * If no select object was supplied to the constructor,
  242. * then it will auto-generate one from the given table,
  243. * schema, field, and adapter options.
  244. *
  245. * @return Select The Select object which will be used
  246. */
  247. public function getSelect()
  248. {
  249. if ($this->select instanceof Select) {
  250. return $this->select;
  251. }
  252. // Build select object
  253. $select = new Select();
  254. $tableIdentifier = new TableIdentifier($this->table, $this->schema);
  255. $select->from($tableIdentifier)->columns(array($this->field));
  256. $select->where->equalTo($this->field, null);
  257. if ($this->exclude !== null) {
  258. if (is_array($this->exclude)) {
  259. $select->where->notEqualTo(
  260. $this->exclude['field'],
  261. $this->exclude['value']
  262. );
  263. } else {
  264. $select->where($this->exclude);
  265. }
  266. }
  267. $this->select = $select;
  268. return $this->select;
  269. }
  270. /**
  271. * Run query and returns matches, or null if no matches are found.
  272. *
  273. * @param string $value
  274. * @return array when matches are found.
  275. */
  276. protected function query($value)
  277. {
  278. $sql = new Sql($this->getAdapter());
  279. $select = $this->getSelect();
  280. $statement = $sql->prepareStatementForSqlObject($select);
  281. $parameters = $statement->getParameterContainer();
  282. $parameters['where1'] = $value;
  283. $result = $statement->execute();
  284. return $result->current();
  285. }
  286. }