Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. <?php
  2. /**
  3. Really expensive algorithm
  4. */
  5.  
  6. /**
  7. * Get all project vacancies
  8. *
  9. * @return Collection of project vacancies
  10. */
  11. public function projectVacancies()
  12. {
  13. $vacancies = ProjectVacancy::available()->with('role', 'project')->get();
  14.  
  15. return $vacancies->map(function ($item) {
  16. return "{$item->project_id},{$item->project_role_id}";
  17. })->unique()->values()->map(function ($item) use ($vacancies) {
  18. $ids = explode(',', $item);
  19. $project_id = (int) $ids[0];
  20. $project_role_id = (int) $ids[1];
  21. return $vacancies->first(function ($vacancy) use ($project_id, $project_role_id) {
  22. return $vacancy->project_id === $project_id && $vacancy->project_role_id === $project_role_id;
  23. });
  24. });
  25. }
  26.  
  27.  
  28. /**
  29. Hash table implementation
  30. */
  31. /**
  32. * Process vacancies (group by project&role, Caertification)
  33. *
  34. * @param array $vacancies
  35. * @return array
  36. */
  37. public function processVacancies($vacancies, $projects = true)
  38. {
  39. return $vacancies->reduce(function ($formatedVacancies, $vacancy) use ($projects){
  40. $formatedVacancies = $formatedVacancies ?: [];
  41.  
  42. $groupId = $projects ? "{$vacancy->project_id}-{$vacancy->project_role_id}" : "{$vacancy->certification_id}";
  43. $formatedVacancy = $vacancy->toArray();
  44. if($projects) {
  45. unset($formatedVacancy['project']);
  46. unset($formatedVacancy['role']);
  47. }else {
  48. unset($formatedVacancy['certification']);
  49. }
  50.  
  51. if (array_key_exists($groupId, $formatedVacancies)) {
  52. $key = $projects ? 'vacancies' : 'certification';
  53. array_push($formatedVacancies[$groupId][$key], $formatedVacancy);
  54. } else if($projects){
  55. $formatedVacancies[$groupId]['project'] = $vacancy->project->toArray();
  56. $formatedVacancies[$groupId]['role'] = $vacancy->role->toArray();
  57. $formatedVacancies[$groupId]['vacancies'] = [$formatedVacancy];
  58. $formatedVacancies[$groupId]['available_slots'] = 0;
  59. } else {
  60. $formatedVacancies[$groupId]['certification'] = $vacancy->certification->toArray();
  61. $formatedVacancies[$groupId]['available_slots'] = 0;
  62. }
  63. if (! $vacancy->fellow_id) {
  64. $formatedVacancies[$groupId]['available_slots']++;
  65. }
  66. return $formatedVacancies;
  67. });
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement