ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.assAnswerMatchingTerm.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
33{
34 protected string $text;
35 protected string $picture;
36 protected int $identifier;
37
38 public function __construct(string $text = "", string $picture = "", int $identifier = 0)
39 {
40 $this->text = $text;
41 $this->picture = $picture;
42
43 $this->identifier = ($identifier !== 0) ? $identifier : $this->createIdentifier();
44 }
45
46 protected function createIdentifier(): int
47 {
48 $id = random_int(1, 100000);
49 return $id;
50 }
51
52 public function getText(): string
53 {
54 return $this->text;
55 }
56 public function withText(string $text): self
57 {
58 $clone = clone $this;
59 $clone->text = $text;
60 return $clone;
61 }
62
63 public function getPicture(): string
64 {
65 return $this->picture;
66 }
67 public function withPicture(string $picture): self
68 {
69 $clone = clone $this;
70 $clone->picture = $picture;
71 return $clone;
72 }
73
74 public function getIdentifier(): int
75 {
76 return $this->identifier;
77 }
78 public function withIdentifier(int $identifier): self
79 {
80 $clone = clone $this;
81 $clone->identifier = $identifier;
82 return $clone;
83 }
84
85 #[\Override]
86 public function toNormalized(
87 Transformations $transformations,
88 array $context = []
89 ): array|float|bool|int|string|null
90 {
91 return [
92 'text' => $this->text,
93 'picture' => $this->picture
94 ? $transformations->normalize(new QuestionImage($this->picture, $context['question_id'] ?? null))
95 : null,
96 'identifier' => $this->identifier,
97 ];
98 }
99
100 #[\Override]
101 public function fromNormalized(
102 array $normalized,
103 Transformations $transformations
104 ): self
105 {
106 return $this->withText($transformations->string($normalized['text']))
107 ->withPicture($transformations->denormalize($normalized['picture'], QuestionImage::class)?->getFilename() ?? '')
108 ->withIdentifier($transformations->int($normalized['identifier']));
109 }
110}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Provides a set of transformations for normalizing and denormalizing values.
denormalize(array|float|bool|int|string|null $normalized, string|object $expected)
Wrapper object for an image file within a question (e.g.
Class for matching question terms.
__construct(string $text="", string $picture="", int $identifier=0)
fromNormalized(array $normalized, Transformations $transformations)
Restore the object from its normalized representation.
toNormalized(Transformations $transformations, array $context=[])
Convert the internal state of the implementing object into a language-neutral array structure.