Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123 řádky
3.5KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\Employee;
  5. use Core\Repository;
  6. class EmployeeRepository extends Repository
  7. {
  8. protected string $table = 'employees';
  9. protected string $primaryKey = 'id';
  10. public function create(Employee $employee): bool
  11. {
  12. return $this->database->execute(
  13. 'INSERT INTO employees (first_name, last_name, email, department, job_title, start_date)
  14. VALUES (:first_name, :last_name, :email, :department, :job_title, :start_date)',
  15. [
  16. 'first_name' => $employee->firstName,
  17. 'last_name' => $employee->lastName,
  18. 'email' => $employee->email,
  19. 'department' => $employee->department,
  20. 'job_title' => $employee->jobTitle,
  21. 'start_date' => $employee->startDate,
  22. ]
  23. );
  24. }
  25. public function findByEmail(string $email): ?array
  26. {
  27. return $this->database->first(
  28. 'SELECT * FROM employees WHERE email = :email',
  29. ['email' => $email]
  30. );
  31. }
  32. /**
  33. * @return list<array<string, mixed>>
  34. */
  35. public function latest(int $limit = 8): array
  36. {
  37. $limit = max(1, $limit);
  38. return $this->database->query(
  39. "SELECT * FROM employees ORDER BY created_at DESC, id DESC LIMIT {$limit}"
  40. );
  41. }
  42. /**
  43. * @return list<array<string, mixed>>
  44. */
  45. public function search(string $search = ''): array
  46. {
  47. [$whereClause, $parameters] = $this->buildSearchClause($search);
  48. return $this->database->query(
  49. 'SELECT id, first_name, last_name, email, department, job_title, start_date, created_at
  50. FROM employees' . $whereClause . '
  51. ORDER BY created_at DESC, id DESC',
  52. $parameters
  53. );
  54. }
  55. public function countMatching(string $search = ''): int
  56. {
  57. [$whereClause, $parameters] = $this->buildSearchClause($search);
  58. $row = $this->database->first(
  59. 'SELECT COUNT(*) AS total FROM employees' . $whereClause,
  60. $parameters
  61. );
  62. return (int) ($row['total'] ?? 0);
  63. }
  64. public function countDepartments(string $search = ''): int
  65. {
  66. [$whereClause, $parameters] = $this->buildSearchClause($search);
  67. $row = $this->database->first(
  68. 'SELECT COUNT(DISTINCT department) AS total FROM employees' . $whereClause,
  69. $parameters
  70. );
  71. return (int) ($row['total'] ?? 0);
  72. }
  73. public function newestMatching(string $search = ''): ?array
  74. {
  75. [$whereClause, $parameters] = $this->buildSearchClause($search);
  76. return $this->database->first(
  77. 'SELECT id, first_name, last_name, department, job_title, start_date
  78. FROM employees' . $whereClause . '
  79. ORDER BY created_at DESC, id DESC
  80. LIMIT 1',
  81. $parameters
  82. );
  83. }
  84. /**
  85. * @return array{0:string,1:array<string,string>}
  86. */
  87. private function buildSearchClause(string $search): array
  88. {
  89. $search = trim($search);
  90. if ($search === '') {
  91. return ['', []];
  92. }
  93. return [
  94. ' WHERE first_name LIKE :search
  95. OR last_name LIKE :search
  96. OR email LIKE :search
  97. OR department LIKE :search
  98. OR job_title LIKE :search
  99. OR start_date LIKE :search',
  100. ['search' => '%' . $search . '%'],
  101. ];
  102. }
  103. }

Powered by TurnKey Linux.