DatabasePresenceVerifier.php 3.6 KB

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