Last active
May 1, 2022 14:18
-
-
Save alexsoyes/31890d29b55e884f29cd346df034e26c to your computer and use it in GitHub Desktop.
A simple example of a "team" service in PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use App\Entity\Team; | |
use App\Repository\TeamRepository; | |
class TeamService | |
{ | |
const TEAM_CATEGORIES = [ | |
'World Tour' => ['WTT'], | |
'Women World Tour' => ['WTW'], | |
'Pro Teams' => ['PRO', 'PRT'], | |
'ProContinental Teams' => ['PCT'], | |
'Continental Men' => ['CTM'], | |
'Continental Women' => ['CTW'], | |
]; | |
private TeamRepository $teamRepository; | |
public function __construct(TeamRepository $teamRepository) | |
{ | |
$this->teamRepository = $teamRepository; | |
} | |
/** | |
* @return array<string, array<Team>> | |
*/ | |
public function findTeamsByCategories(int $year): array | |
{ | |
$teams = []; | |
foreach (self::TEAM_CATEGORIES as $teamCategoryName => $teamCategory) { | |
$teams[$teamCategoryName] = []; | |
foreach ($teamCategory as $category) { | |
$teams[$teamCategoryName] = array_merge( | |
$teams[$teamCategoryName], | |
$this->teamRepository->findBy(['year' => $year, 'category' => $category]) | |
); | |
} | |
} | |
return $teams; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment