AbstractDb.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ qZ9w5XDgHGbMtZMNSWIUOyMLxCOGtq2wqigA+5UFVBA=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.5
  11. */
  12. namespace Cube\Validate\Db;
  13. use Cube\Validate\AbstractValidate,
  14. Cube\Db\Table\AbstractTable,
  15. Cube\Db\Select;
  16. abstract class AbstractDb extends AbstractValidate
  17. {
  18. /**
  19. *
  20. * table object
  21. *
  22. * @var \Cube\Db\Table\AbstractTable
  23. */
  24. protected $_table;
  25. /**
  26. *
  27. * table field
  28. *
  29. * @var string
  30. */
  31. protected $_field;
  32. /**
  33. *
  34. * data to be excluded by the query
  35. *
  36. * @var mixed
  37. */
  38. protected $_exclude;
  39. /**
  40. *
  41. * select object
  42. *
  43. * @var \Cube\Db\Select
  44. */
  45. protected $_select;
  46. /**
  47. *
  48. * class constructor
  49. *
  50. * add the table and the field to compare against
  51. *
  52. * @param array $data
  53. * Supported keys:
  54. * 'table' -> table - fully qualified namespace;
  55. * 'field' -> table field
  56. * 'exclude' -> where clause or field/value pair to exclude from the query
  57. * @throws \InvalidArgumentException
  58. */
  59. public function __construct(array $data = null)
  60. {
  61. if (array_key_exists('table', $data)) {
  62. $this->setTable($data['table']);
  63. }
  64. else {
  65. throw new \InvalidArgumentException("'table' option missing from the validator.");
  66. }
  67. if (array_key_exists('field', $data)) {
  68. $this->setField($data['field']);
  69. }
  70. else {
  71. throw new \InvalidArgumentException("'field' option missing from the validator.");
  72. }
  73. if (array_key_exists('exclude', $data)) {
  74. $this->setExclude($data['exclude']);
  75. }
  76. }
  77. /**
  78. *
  79. * get table object
  80. *
  81. * @return \Cube\Db\Table\AbstractTable
  82. */
  83. public function getTable()
  84. {
  85. return $this->_table;
  86. }
  87. /**
  88. *
  89. * set table object
  90. *
  91. * @param string|\Cube\Db\Table\AbstractTable $table table object or fully qualified name
  92. * @throws \InvalidArgumentException
  93. * @return \Cube\Validate\Db\AbstractDb
  94. */
  95. public function setTable($table)
  96. {
  97. if ($table instanceof AbstractTable) {
  98. $this->_table = $table;
  99. }
  100. else if (class_exists((string) $table)) {
  101. $this->_table = new $table();
  102. }
  103. else {
  104. throw new \InvalidArgumentException("The table variable must be
  105. an instance of \Cube\Db\Table\AbstractTable,
  106. or a string representing the fully qualified namespace of the table.");
  107. }
  108. return $this;
  109. }
  110. /**
  111. *
  112. * get table field
  113. *
  114. * @return string
  115. */
  116. public function getField()
  117. {
  118. return $this->_field;
  119. }
  120. /**
  121. *
  122. * set table field
  123. *
  124. * @param string $field
  125. * @return \Cube\Validate\Db\AbstractDb
  126. */
  127. public function setField($field)
  128. {
  129. $this->_field = (string) $field;
  130. return $this;
  131. }
  132. /**
  133. *
  134. * get data to exclude
  135. *
  136. * @return array
  137. */
  138. public function getExclude()
  139. {
  140. return $this->_exclude;
  141. }
  142. /**
  143. *
  144. * set data to exclude
  145. *
  146. * @param array $exclude
  147. * @return \Cube\Validate\Db\AbstractDb
  148. */
  149. public function setExclude($exclude)
  150. {
  151. $this->_exclude = $exclude;
  152. return $this;
  153. }
  154. /**
  155. *
  156. * set select object
  157. *
  158. * @param \Cube\Db\Select $select
  159. * @return \Cube\Validate\Db\AbstractDb
  160. */
  161. public function setSelect(Select $select)
  162. {
  163. $this->_select = $select;
  164. return $this;
  165. }
  166. /**
  167. *
  168. * get select object, construct if it wasn't set yet
  169. *
  170. * @return \Cube\Db\Select
  171. */
  172. public function getSelect()
  173. {
  174. if ($this->_select === null) {
  175. $adapter = $this->_table->getAdapter();
  176. $select = $this->_table->select()
  177. ->where($adapter->quoteIdentifier($this->_field) . ' = ?', strval($this->_value));
  178. if ($this->_exclude !== null) {
  179. if (is_array($this->_exclude)) {
  180. $select->where(
  181. $adapter->quoteIdentifier($this->_exclude['field'], true) .
  182. ' != ?', strval($this->_exclude['value'])
  183. );
  184. }
  185. else {
  186. $select->where($this->_exclude);
  187. }
  188. }
  189. $select->limit(1);
  190. $this->_select = $select;
  191. }
  192. return $this->_select;
  193. }
  194. }