DatabasePresenceVerifier.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Illuminate\Validation;
  3. use Closure;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Database\ConnectionResolverInterface;
  6. class DatabasePresenceVerifier implements PresenceVerifierInterface
  7. {
  8. /**
  9. * The database connection instance.
  10. *
  11. * @var \Illuminate\Database\ConnectionResolverInterface
  12. */
  13. protected $db;
  14. /**
  15. * The database connection to use.
  16. *
  17. * @var string
  18. */
  19. protected $connection;
  20. /**
  21. * Create a new database presence verifier.
  22. *
  23. * @param \Illuminate\Database\ConnectionResolverInterface $db
  24. * @return void
  25. */
  26. public function __construct(ConnectionResolverInterface $db)
  27. {
  28. $this->db = $db;
  29. }
  30. /**
  31. * Count the number of objects in a collection having the given value.
  32. *
  33. * @param string $collection
  34. * @param string $column
  35. * @param string $value
  36. * @param int|null $excludeId
  37. * @param string|null $idColumn
  38. * @param array $extra
  39. * @return int
  40. */
  41. public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
  42. {
  43. $query = $this->table($collection)->where($column, '=', $value);
  44. if (! is_null($excludeId) && $excludeId !== 'NULL') {
  45. $query->where($idColumn ?: 'id', '<>', $excludeId);
  46. }
  47. return $this->addConditions($query, $extra)->count();
  48. }
  49. /**
  50. * Count the number of objects in a collection with the given values.
  51. *
  52. * @param string $collection
  53. * @param string $column
  54. * @param array $values
  55. * @param array $extra
  56. * @return int
  57. */
  58. public function getMultiCount($collection, $column, array $values, array $extra = [])
  59. {
  60. $query = $this->table($collection)->whereIn($column, $values);
  61. return $this->addConditions($query, $extra)->count();
  62. }
  63. /**
  64. * Add the given conditions to the query.
  65. *
  66. * @param \Illuminate\Database\Query\Builder $query
  67. * @param array $conditions
  68. * @return \Illuminate\Database\Query\Builder
  69. */
  70. protected function addConditions($query, $conditions)
  71. {
  72. foreach ($conditions as $key => $value) {
  73. if ($value instanceof Closure) {
  74. $query->where(function ($query) use ($value) {
  75. $value($query);
  76. });
  77. } else {
  78. $this->addWhere($query, $key, $value);
  79. }
  80. }
  81. return $query;
  82. }
  83. /**
  84. * Add a "where" clause to the given query.
  85. *
  86. * @param \Illuminate\Database\Query\Builder $query
  87. * @param string $key
  88. * @param string $extraValue
  89. * @return void
  90. */
  91. protected function addWhere($query, $key, $extraValue)
  92. {
  93. if ($extraValue === 'NULL') {
  94. $query->whereNull($key);
  95. } elseif ($extraValue === 'NOT_NULL') {
  96. $query->whereNotNull($key);
  97. } elseif (Str::startsWith($extraValue, '!')) {
  98. $query->where($key, '!=', mb_substr($extraValue, 1));
  99. } else {
  100. $query->where($key, $extraValue);
  101. }
  102. }
  103. /**
  104. * Get a query builder for the given table.
  105. *
  106. * @param string $table
  107. * @return \Illuminate\Database\Query\Builder
  108. */
  109. protected function table($table)
  110. {
  111. return $this->db->connection($this->connection)->table($table)->useWritePdo();
  112. }
  113. /**
  114. * Set the connection to be used.
  115. *
  116. * @param string $connection
  117. * @return void
  118. */
  119. public function setConnection($connection)
  120. {
  121. $this->connection = $connection;
  122. }
  123. }