mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-18 11:48:06 +00:00
Add support for SubjectConverterManager awareness in converters and refactor subject retrieval.
- Introduced `SubjectConverterManagerAwareTrait` and `SubjectConverterManagerAwareInterface` for injecting subject converter manager into converters. - Added `getSubjectsForEntity` method in `SubjectConverterManager` to centralize subject retrieval logic. - Updated converters to set the subject converter manager when applicable.
This commit is contained in:
@@ -28,16 +28,25 @@ final readonly class SubjectConverterManager implements SubjectConverterManagerI
|
||||
* @throws ConvertSubjectException
|
||||
*/
|
||||
public function convertEntityToSubjects(mixed $subject): array
|
||||
{
|
||||
$subjects = $this->getSubjectsForEntity($subject);
|
||||
|
||||
if ($subjects instanceof Subject) {
|
||||
return [$subjects->asArray()];
|
||||
}
|
||||
|
||||
return array_map(static fn (Subject $subject) => $subject->asArray(), $subjects);
|
||||
}
|
||||
|
||||
public function getSubjectsForEntity(mixed $subject): Subject|array
|
||||
{
|
||||
foreach ($this->converters as $converter) {
|
||||
if ($converter instanceof SubjectConverterManagerAwareInterface) {
|
||||
$converter->setSubjectConverterManager($this);
|
||||
}
|
||||
|
||||
if ($converter->supportsConvert($subject)) {
|
||||
$subjects = $converter->convert($subject);
|
||||
|
||||
if ($subjects instanceof Subject) {
|
||||
return [$subjects->asArray()];
|
||||
}
|
||||
|
||||
return array_map(static fn (Subject $subject) => $subject->asArray(), $subjects);
|
||||
return $converter->convert($subject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user