ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
SuggestedSolutionNormalizer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
36{
37 public function __construct(
38 private readonly Transformations $tt,
39 ) {
40 }
41
42 #[\Override]
43 public function normalize($value): array|float|bool|int|string|null
44 {
45 if (!($value instanceof SuggestedSolution)) {
46 throw new NormalizingException('Invalid value', $value);
47 }
48
49 $normalized = [
50 'id' => $this->tt->normalize(new Id($value->getId(), 'suggested_solution')),
51 'question_id' => $this->tt->normalize(new Id($value->getQuestionId(), 'question')),
52 'subquestion_index' => $value->getSubquestionIndex(),
53 'import_id' => $value->getImportId(),
54 'last_update' => $this->tt->normalize($value->getLastUpdate()),
55 ];
56
57 if ($value instanceof SuggestedSolutionLink) {
58 $normalized['type'] = $value->getType();
59 $normalized['internal_link'] = $value->getInternalLink();
60 }
61
62 if ($value instanceof SuggestedSolutionFile) {
63 $normalized['type'] = $value->getType();
64 $normalized['title'] = $value->getTitle();
65 $normalized['mime'] = $value->getMime();
66 $normalized['size'] = $value->getSize();
67
68 $normalized['file'] = $this->tt->normalize(
69 new QuestionImage($value->getFilename(), $value->getQuestionId(), QuestionImage::TYPE_SOLUTION)
70 );
71
72 }
73
74 return $normalized;
75 }
76
77 #[\Override]
78 public function denormalize(array|float|bool|int|string|null $value, string $type): SuggestedSolution
79 {
80 if ($type !== SuggestedSolution::class && !in_array(SuggestedSolution::class, class_parents($type))) {
81 throw new NormalizingException("Invalid type: {$type}");
82 }
83
84 // If abstract class expected we need to lookup the concrete type from the normalized value
85 $denormalized_type = $this->tt->string($value['type']);
86 if ($type === SuggestedSolution::class) {
87 $type = match($denormalized_type) {
88 SuggestedSolution::TYPE_FILE => SuggestedSolutionFile::class,
89 SuggestedSolution::TYPE_LM => SuggestedSolutionLink::class,
90 SuggestedSolution::TYPE_LM_CHAPTER => SuggestedSolutionLink::class,
91 SuggestedSolution::TYPE_LM_PAGE => SuggestedSolutionLink::class,
92 SuggestedSolution::TYPE_GLOSARY_TERM => SuggestedSolutionLink::class,
93 default => throw new NormalizingException("Invalid denormalized type: {$denormalized_type}"),
94 };
95 }
96
97 $id = $this->tt->denormalize($value['id'], Id::class)->getId();
98 $question_id = $this->tt->denormalize($value['question_id'], Id::class)->getId();
99 $subquestion_index = $this->tt->int($value['subquestion_index']);
100 $import_id = $this->tt->string($value['import_id']);
101 $last_update = $this->tt->denormalize($value['last_update'], \DateTimeImmutable::class);
102
103 switch ($type) {
104 case SuggestedSolutionFile::class:
105 return new SuggestedSolutionFile(
106 $id,
107 $question_id,
108 $subquestion_index,
109 $import_id,
110 $last_update,
111 $denormalized_type,
112 ''
113 )
114 ->withTitle($this->tt->string($value['title']))
115 ->withFilename($this->tt->denormalize($value['file'], QuestionImage::class)->getFilename())
116 ->withMime($this->tt->string($value['mime']))
117 ->withSize($this->tt->int($value['size']));
118 case SuggestedSolutionLink::class:
119 return new SuggestedSolutionLink(
120 $id,
121 $question_id,
122 $subquestion_index,
123 $import_id,
124 $last_update,
125 $denormalized_type,
126 $this->tt->string($value['internal_link'])
127 );
128 default:
129 throw new NormalizingException("Invalid type: {$type}");
130 }
131 }
132}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Wrapper object for an object identifier and an optional object type the identifier belongs to.
Definition: Id.php:33
Exception thrown when a value cannot be normalized or denormalized.
Provides a set of transformations for normalizing and denormalizing values.
Wrapper object for an image file within a question (e.g.
denormalize(array|float|bool|int|string|null $value, string $type)
Converts a normalized form back into a value.
A Normalizer is responsible for converting a value into a normalized form and back.
Definition: Normalizer.php:35