領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8KB

  1. <?php
  2. namespace App\Repositories;
  3. use Illuminate\Database\Query\Builder;
  4. abstract class BaseRepository
  5. {
  6. const ORDER_ASC = "asc";
  7. const ORDER_DESC = "desc";
  8. const CONDITION_SORT_TARGET = "sort";
  9. const CONDITION_SORT_ORDER = "order";
  10. const CONDITION_LIMIT = "limit";
  11. const MAX_LIMIT = 500;
  12. protected static function sort(Builder $query, array $condition)
  13. {
  14. $target = data_get($condition, self::CONDITION_SORT_TARGET);
  15. $order = data_get($condition, self::CONDITION_SORT_ORDER);
  16. if ($target === null || $order === null) {
  17. return;
  18. }
  19. if ($order === self::ORDER_ASC) {
  20. $query->orderBy($target);
  21. }
  22. if ($order === self::ORDER_DESC) {
  23. $query->orderByDesc($target);
  24. }
  25. }
  26. protected static function limit(Builder $query, array $condition, int $default = self::MAX_LIMIT)
  27. {
  28. $limit = data_get($condition, self::CONDITION_LIMIT, $default) ?? $default;
  29. $limit = min($limit, self::MAX_LIMIT);
  30. $query->limit($limit);
  31. }
  32. protected static function where(Builder $query, array $condition, string $conditionKey, string|null $columnName = null): bool
  33. {
  34. $ret = data_get($condition, $conditionKey);
  35. if ($ret !== null) {
  36. $query->where($columnName ?? $conditionKey, $ret);
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. protected static function whereIn(Builder $query, array $condition, string $conditionKey, string|null $columnName = null): bool
  43. {
  44. $ret = data_get($condition, $conditionKey);
  45. if ($ret !== null && is_array($ret)) {
  46. $query->whereIn($columnName ?? $conditionKey, $ret);
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. }