DatabaseRule.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Closure;
  4. trait DatabaseRule
  5. {
  6. /**
  7. * The table to run the query against.
  8. *
  9. * @var string
  10. */
  11. protected $table;
  12. /**
  13. * The column to check on.
  14. *
  15. * @var string
  16. */
  17. protected $column;
  18. /**
  19. * There extra where clauses for the query.
  20. *
  21. * @var array
  22. */
  23. protected $wheres = [];
  24. /**
  25. * The array of custom query callbacks.
  26. *
  27. * @var array
  28. */
  29. protected $using = [];
  30. /**
  31. * Create a new rule instance.
  32. *
  33. * @param string $table
  34. * @param string $column
  35. * @return void
  36. */
  37. public function __construct($table, $column = 'NULL')
  38. {
  39. $this->table = $table;
  40. $this->column = $column;
  41. }
  42. /**
  43. * Set a "where" constraint on the query.
  44. *
  45. * @param string|\Closure $column
  46. * @param array|string|null $value
  47. * @return $this
  48. */
  49. public function where($column, $value = null)
  50. {
  51. if (is_array($value)) {
  52. return $this->whereIn($column, $value);
  53. }
  54. if ($column instanceof Closure) {
  55. return $this->using($column);
  56. }
  57. $this->wheres[] = compact('column', 'value');
  58. return $this;
  59. }
  60. /**
  61. * Set a "where not" constraint on the query.
  62. *
  63. * @param string $column
  64. * @param array|string $value
  65. * @return $this
  66. */
  67. public function whereNot($column, $value)
  68. {
  69. if (is_array($value)) {
  70. return $this->whereNotIn($column, $value);
  71. }
  72. return $this->where($column, '!'.$value);
  73. }
  74. /**
  75. * Set a "where null" constraint on the query.
  76. *
  77. * @param string $column
  78. * @return $this
  79. */
  80. public function whereNull($column)
  81. {
  82. return $this->where($column, 'NULL');
  83. }
  84. /**
  85. * Set a "where not null" constraint on the query.
  86. *
  87. * @param string $column
  88. * @return $this
  89. */
  90. public function whereNotNull($column)
  91. {
  92. return $this->where($column, 'NOT_NULL');
  93. }
  94. /**
  95. * Set a "where in" constraint on the query.
  96. *
  97. * @param string $column
  98. * @param array $values
  99. * @return $this
  100. */
  101. public function whereIn($column, array $values)
  102. {
  103. return $this->where(function ($query) use ($column, $values) {
  104. $query->whereIn($column, $values);
  105. });
  106. }
  107. /**
  108. * Set a "where not in" constraint on the query.
  109. *
  110. * @param string $column
  111. * @param array $values
  112. * @return $this
  113. */
  114. public function whereNotIn($column, array $values)
  115. {
  116. return $this->where(function ($query) use ($column, $values) {
  117. $query->whereNotIn($column, $values);
  118. });
  119. }
  120. /**
  121. * Register a custom query callback.
  122. *
  123. * @param \Closure $callback
  124. * @return $this
  125. */
  126. public function using(Closure $callback)
  127. {
  128. $this->using[] = $callback;
  129. return $this;
  130. }
  131. /**
  132. * Get the custom query callbacks for the rule.
  133. *
  134. * @return array
  135. */
  136. public function queryCallbacks()
  137. {
  138. return $this->using;
  139. }
  140. /**
  141. * Format the where clauses.
  142. *
  143. * @return string
  144. */
  145. protected function formatWheres()
  146. {
  147. return collect($this->wheres)->map(function ($where) {
  148. return $where['column'].','.$where['value'];
  149. })->implode(',');
  150. }
  151. }