ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.arFieldList.php
Go to the documentation of this file.
1<?php
2
25{
26 public const HAS_FIELD = 'has_field';
27 public const IS_PRIMARY = 'is_primary';
28 public const IS_NOTNULL = 'is_notnull';
29 public const FIELDTYPE = 'fieldtype';
30 public const LENGTH = 'length';
31 public const SEQUENCE = 'sequence';
32 public const INDEX = 'index';
33 protected static array $prefixes = ['db', 'con'];
34 protected static array $protected_names = ['arConnector', 'arFieldList'];
35 protected static array $allowed_description_fields = [
36 'is_unique',
37 // There are many classes which already use this (without any function)
44 ];
46 protected \arField|array $primary_field;
47 protected array $primary_fields = [];
48 protected array $raw_fields = [];
49 protected array $fields = [];
50 protected static array $key_maps = [self::FIELDTYPE => 'type', self::IS_NOTNULL => 'notnull'];
51
55 public function __construct(protected ActiveRecord $activeRecord)
56 {
57 }
58
59 public static function mapKey(string $key): string
60 {
61 return self::$key_maps[$key] ?? $key;
62 }
63
67 public static function getAllowedConnectorFields(): array
68 {
70 }
71
75 public static function getAllowedDescriptionFields(): array
76 {
78 }
79
80 public static function getInstance(ActiveRecord $activeRecord): \arFieldList
81 {
82 $self = new self($activeRecord);
83 $self->initRawFields($activeRecord);
84 $self->initFields();
85
86 return $self;
87 }
88
92 public static function getInstanceFromStorage(\ActiveRecord $activeRecord): \arFieldList
93 {
94 $self = new self($activeRecord);
95 $self->initRawFields($activeRecord);
96 $self->initFields();
97
98 return $self;
99 }
100
104 public function getArrayForConnector(): array
105 {
106 $return = [];
107 foreach ($this->getFields() as $arField) {
108 $return[$arField->getName()] = $arField->getAttributesForConnector();
109 }
110
111 return $return;
112 }
113
114 protected function initFields(): void
115 {
116 foreach ($this->getRawFields() as $fieldname => $attributes) {
117 if (self::checkAttributes($attributes)) {
118 $arField = new arField();
119 $arField->loadFromArray($fieldname, $attributes);
120 $this->fields[] = $arField;
121 if ($arField->getPrimary()) {
122 $this->setPrimaryField($arField);
123 }
124 }
125 }
126 }
127
128 public function getFieldByName(string $field_name): ?arField
129 {
130 $field = null;
131 static $field_map;
132 $field_key = $this->activeRecord->getConnectorContainerName() . '.' . $field_name;
133 if (is_array($field_map) && array_key_exists($field_key, $field_map)) {
134 return $field_map[$field_key];
135 }
136 foreach ($this->getFields() as $field) {
137 if ($field->getName() === $field_name) {
138 $field_map[$field_key] = $field;
139
140 return $field;
141 }
142 }
143 return null;
144 }
145
146 public function isField(string $field_name): bool
147 {
148 $is_field = false;
149 foreach ($this->getFields() as $arField) {
150 if ($arField->getName() === $field_name) {
151 $is_field = true;
152 }
153 }
154
155 return $is_field;
156 }
157
158 public function getPrimaryFieldName(): string
159 {
160 return $this->getPrimaryField()->getName();
161 }
162
163 public function getPrimaryFieldType(): string
164 {
165 return $this->getPrimaryField()->getFieldType();
166 }
167
168 protected function initRawFields(ActiveRecord $ar): void
169 {
170 $regex = "/[\t ]*\\* @(" . implode('|', self::$prefixes) . ")_([a-zA-Z0-9_]+)[\t ]+([a-zA-Z0-9_]+)/u";
171 $reflection = new ReflectionClass($ar);
172 $raw_fields = [];
173 foreach ($reflection->getProperties() as $property) {
174 if (in_array($property->getName(), self::$protected_names)) {
175 continue;
176 }
177 $properties_array = [];
178 $has_property = false;
179 foreach (explode("\n", $property->getDocComment()) as $line) {
180 if (preg_match($regex, $line, $matches)) {
181 $has_property = true;
182 $properties_array[$matches[2]] = $matches[3];
183 }
184 }
185 if ($has_property) {
186 $raw_fields[$property->getName()] = $properties_array;
187 }
188 }
189
190 $this->setRawFields($raw_fields);
191 }
192
193 protected static function isAllowedAttribute(string $attribute_name): bool
194 {
195 return in_array($attribute_name, array_merge(self::$allowed_description_fields, [self::HAS_FIELD]), true);
196 }
197
198 protected static function checkAttributes(array $attributes): bool
199 {
200 if (isset($attributes[self::HAS_FIELD]) && $attributes[self::HAS_FIELD] === 'true') {
201 foreach (array_keys($attributes) as $atr) {
202 if (!self::isAllowedAttribute($atr)) {
203 return false;
204 }
205 }
206 } else {
207 return false;
208 }
209
210 return true;
211 }
212
216 public function setFields(array $fields): void
217 {
218 $this->fields = $fields;
219 }
220
224 public function getFields(): array
225 {
226 return $this->fields;
227 }
228
229 public function setPrimaryField(\arField $arField): void
230 {
231 $this->primary_field = $arField;
232 }
233
237 public function getPrimaryField(): \arField|array
238 {
240 }
241
245 public function setRawFields(array $raw_fields): void
246 {
247 $this->raw_fields = $raw_fields;
248 }
249
253 public function getRawFields(): array
254 {
255 return $this->raw_fields;
256 }
257
261 public function setPrimaryFields(array $primary_fields): void
262 {
263 $this->primary_fields = $primary_fields;
264 }
265
269 public function getPrimaryFields(): array
270 {
272 }
273}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static array $allowed_description_fields
static array $protected_names
getFieldByName(string $field_name)
static checkAttributes(array $attributes)
static mapKey(string $key)
static getAllowedDescriptionFields()
setPrimaryFields(array $primary_fields)
__construct(protected ActiveRecord $activeRecord)
arFieldList constructor.
static array $prefixes
initRawFields(ActiveRecord $ar)
isField(string $field_name)
setFields(array $fields)
static array $key_maps
static getInstance(ActiveRecord $activeRecord)
setRawFields(array $raw_fields)
static array $allowed_connector_fields
arField array $primary_field
static getAllowedConnectorFields()
setPrimaryField(\arField $arField)
static isAllowedAttribute(string $attribute_name)
static getInstanceFromStorage(\ActiveRecord $activeRecord)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...