ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
IlObjectNormalizer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
36
41{
42 public function __construct(
43 protected readonly Transformations $tt,
44 ) {
45 }
46
47 #[\Override]
48 public function normalize($value): array|float|bool|int|string|null
49 {
50 if (!($value instanceof \ilObject)) {
51 throw new NormalizingException('Invalid value', $value);
52 }
53
54 // Icon, tile, translations and container settings are exported by the ILIASObject component.
55 // See: ilObjectDataSet
56 return [
57 'obj_id' => $this->tt->normalize(new Id($value->getId(), 'object')),
58 'title' => $value->getTitle(),
59 'description' => $value->getLongDescription(),
60 'type' => $value->getType(),
61 'owner' => $value->getOwner(),
62 'create_date' => $value->getCreateDate(),
63 'last_update' => $value->getLastUpdateDate(),
64 'import_id' => $value->getImportId(),
65 'properties' => $this->normalizeProperties($value->getObjectProperties()),
66 ];
67 }
68
69 private function normalizeProperties(Properties $properties): array
70 {
71 return [
72 'owner' => $properties->getOwner(),
73 'import_id' => $properties->getImportId(),
74 'title_and_description' => $this->normalizeProperty($properties->getPropertyTitleAndDescription()),
75 'title_and_icon_visibility' => $this->normalizeProperty($properties->getPropertyTitleAndIconVisibility()),
76 'header_action_visibility' => $this->normalizeProperty($properties->getPropertyHeaderActionVisibility()),
77 'info_tab_visibility' => $this->normalizeProperty($properties->getPropertyInfoTabVisibility()),
78 'translations' => $this->normalizeTranslations($properties->getPropertyTranslations()),
79 ];
80 }
81
82 private function normalizeProperty(Property $property): array|bool|int|string|null
83 {
84 if ($property instanceof TitleAndDescription) {
85 return [
86 'title' => $property->getTitle(),
87 'description' => $property->getDescription(),
88 'long_description' => $property->getLongDescription(),
89 ];
90 }
91
92 if ($property instanceof Online) {
93 return $property->getIsOnline();
94 }
95
96 if (
97 $property instanceof TitleAndIconVisibility
98 || $property instanceof HeaderActionVisibility
99 || $property instanceof InfoTabVisibility
100 ) {
101 return $property->getVisibility();
102 }
103
104 return null;
105 }
106
107 private function normalizeTranslations(Translations $translations): array
108 {
109 return [
110 'default_language' => $translations->getDefaultLanguage(),
111 'base_language' => $translations->getBaseLanguage(),
112 'languages' => array_map(fn(Language $language): array => [
113 'language_code' => $language->getLanguageCode(),
114 'title' => $language->getTitle(),
115 'description' => $language->getDescription(),
116 ], $translations->getLanguages()),
117 ];
118 }
119
120 #[\Override]
121 public function denormalize(array|float|bool|int|string|null $value, string $type): \ilObject
122 {
123 if ($type !== \ilObject::class && !in_array(\ilObject::class, class_parents($type))) {
124 throw new NormalizingException("Invalid type for ilObject: {$type}");
125 }
126
127 // Validate the class of the object by its type field
128 $object_type = $this->tt->string($value['type']);
129 $object_class = \ilObjectFactory::getClassByType($object_type);
130 if ($object_class !== $type) {
131 throw new NormalizingException("Expected {$type}, got object of type {$object_type} ({$object_class})");
132 }
133
134 // Create new object instance without id to avoid reading the object from the database
135 $object = new $object_class(0, false);
136
137 $object->setId($this->tt->denormalize($value['obj_id'], Id::class)->getId());
138 $object->setTitle($this->tt->string($value['title']));
139 $object->setDescription($this->tt->string($value['description']));
140 $object->setType($this->tt->string($value['type']));
141 $object->setOwner($this->tt->int($value['owner']));
142 $object->setImportId($this->tt->string($value['import_id']));
143
144 return $object;
145 }
146}
Class handles translation mode for an object.
Wrapper object for an object identifier and an optional object type the identifier belongs to.
Definition: Id.php:33
denormalize(array|float|bool|int|string|null $value, string $type)
Converts a normalized form back into a value.
Exception thrown when a value cannot be normalized or denormalized.
Provides a set of transformations for normalizing and denormalizing values.
static getClassByType(string $obj_type)
Class ilObject Basic functions for all objects.
A Normalizer is responsible for converting a value into a normalized form and back.
Definition: Normalizer.php:35