123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
-
- namespace Illuminate\Validation\Rules;
-
- use Closure;
-
- trait DatabaseRule
- {
- /**
- * The table to run the query against.
- *
- * @var string
- */
- protected $table;
-
- /**
- * The column to check on.
- *
- * @var string
- */
- protected $column;
-
- /**
- * There extra where clauses for the query.
- *
- * @var array
- */
- protected $wheres = [];
-
- /**
- * The array of custom query callbacks.
- *
- * @var array
- */
- protected $using = [];
-
- /**
- * Create a new rule instance.
- *
- * @param string $table
- * @param string $column
- * @return void
- */
- public function __construct($table, $column = 'NULL')
- {
- $this->table = $table;
- $this->column = $column;
- }
-
- /**
- * Set a "where" constraint on the query.
- *
- * @param string|\Closure $column
- * @param array|string|null $value
- * @return $this
- */
- public function where($column, $value = null)
- {
- if (is_array($value)) {
- return $this->whereIn($column, $value);
- }
-
- if ($column instanceof Closure) {
- return $this->using($column);
- }
-
- $this->wheres[] = compact('column', 'value');
-
- return $this;
- }
-
- /**
- * Set a "where not" constraint on the query.
- *
- * @param string $column
- * @param array|string $value
- * @return $this
- */
- public function whereNot($column, $value)
- {
- if (is_array($value)) {
- return $this->whereNotIn($column, $value);
- }
-
- return $this->where($column, '!'.$value);
- }
-
- /**
- * Set a "where null" constraint on the query.
- *
- * @param string $column
- * @return $this
- */
- public function whereNull($column)
- {
- return $this->where($column, 'NULL');
- }
-
- /**
- * Set a "where not null" constraint on the query.
- *
- * @param string $column
- * @return $this
- */
- public function whereNotNull($column)
- {
- return $this->where($column, 'NOT_NULL');
- }
-
- /**
- * Set a "where in" constraint on the query.
- *
- * @param string $column
- * @param array $values
- * @return $this
- */
- public function whereIn($column, array $values)
- {
- return $this->where(function ($query) use ($column, $values) {
- $query->whereIn($column, $values);
- });
- }
-
- /**
- * Set a "where not in" constraint on the query.
- *
- * @param string $column
- * @param array $values
- * @return $this
- */
- public function whereNotIn($column, array $values)
- {
- return $this->where(function ($query) use ($column, $values) {
- $query->whereNotIn($column, $values);
- });
- }
-
- /**
- * Register a custom query callback.
- *
- * @param \Closure $callback
- * @return $this
- */
- public function using(Closure $callback)
- {
- $this->using[] = $callback;
-
- return $this;
- }
-
- /**
- * Get the custom query callbacks for the rule.
- *
- * @return array
- */
- public function queryCallbacks()
- {
- return $this->using;
- }
-
- /**
- * Format the where clauses.
- *
- * @return string
- */
- protected function formatWheres()
- {
- return collect($this->wheres)->map(function ($where) {
- return $where['column'].','.$where['value'];
- })->implode(',');
- }
- }
|