SchulIT /
icc
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Converter; |
||||
| 4 | |||||
| 5 | use App\Entity\Grade; |
||||
| 6 | use App\Entity\StudyGroup; |
||||
| 7 | use App\Entity\StudyGroupType; |
||||
| 8 | use App\Sorting\Sorter; |
||||
| 9 | use App\Sorting\StudyGroupStrategy; |
||||
| 10 | use Doctrine\Common\Collections\ArrayCollection; |
||||
| 11 | use Doctrine\Common\Collections\Collection; |
||||
| 12 | use Symfony\Contracts\Translation\TranslatorInterface; |
||||
| 13 | |||||
| 14 | class StudyGroupsGradeStringConverter { |
||||
| 15 | |||||
| 16 | public function __construct(private TranslatorInterface $translator, private Sorter $sorter, private GradesCollapsedArrayConverter $gradeConverter) |
||||
| 17 | { |
||||
| 18 | } |
||||
| 19 | |||||
| 20 | 14 | /** |
|||
| 21 | 14 | * @param StudyGroup[]|iterable $studyGroups |
|||
| 22 | 14 | * @param Grade[]|iterable $onlyGrades |
|||
| 23 | 14 | */ |
|||
| 24 | 14 | public function convert(iterable $studyGroups, bool $sort = false, iterable $onlyGrades = [ ]): string { |
|||
| 25 | if($studyGroups instanceof Collection) { |
||||
| 26 | $studyGroups = $studyGroups->toArray(); |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | if($onlyGrades instanceof Collection) { |
||||
| 30 | $onlyGrades = $onlyGrades->toArray(); |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | /** @var int[] $onlyGrades */ |
||||
| 34 | $onlyGrades = array_map(fn(Grade $grade) => $grade->getId(), $onlyGrades); |
||||
| 35 | |||||
| 36 | if($sort === true) { |
||||
| 37 | $this->sorter->sort($studyGroups, StudyGroupStrategy::class); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 38 | } |
||||
| 39 | |||||
| 40 | $output = [ ]; |
||||
|
0 ignored issues
–
show
|
|||||
| 41 | |||||
| 42 | // First: Grades |
||||
| 43 | $grades = array_filter($studyGroups, fn(StudyGroup $group) => $group->getType() === StudyGroupType::Grade); |
||||
|
0 ignored issues
–
show
It seems like
$studyGroups can also be of type iterable; however, parameter $array of array_filter() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 44 | |||||
| 45 | $output = $this->gradeConverter->convert( |
||||
| 46 | array_map(fn(StudyGroup $group) => $group->getGrades()->first(), $grades) |
||||
| 47 | ); |
||||
| 48 | |||||
| 49 | // Second: Individual groups |
||||
| 50 | $studyGroups = array_filter($studyGroups, fn(StudyGroup $group) => $group->getType() !== StudyGroupType::Grade); |
||||
| 51 | |||||
| 52 | $output += array_map(fn(StudyGroup $studyGroup) => $this->translator->trans('studygroup.string', [ |
||||
| 53 | '%name%' => $studyGroup->getName(), |
||||
| 54 | '%grade%' => implode(', ', $this->gradeConverter->convert($studyGroup->getGrades()->filter(function(Grade $grade) use($onlyGrades) { |
||||
| 55 | if(empty($onlyGrades)) { |
||||
| 56 | return true; |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | return in_array($grade->getId(), $onlyGrades); |
||||
| 60 | })->toArray())) |
||||
| 61 | ]), $studyGroups); |
||||
| 62 | |||||
| 63 | return implode(', ', $output); |
||||
| 64 | } |
||||
| 65 | } |