Skip to content

Instantly share code, notes, and snippets.

@alexsoyes
Last active May 1, 2022 14:18
Show Gist options
  • Save alexsoyes/31890d29b55e884f29cd346df034e26c to your computer and use it in GitHub Desktop.
Save alexsoyes/31890d29b55e884f29cd346df034e26c to your computer and use it in GitHub Desktop.
A simple example of a "team" service in PHP.
<?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