ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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)
38  self::IS_PRIMARY,
39  self::IS_NOTNULL,
40  self::FIELDTYPE,
41  self::LENGTH,
42  self::SEQUENCE,
43  self::INDEX,
44  ];
45  protected static array $allowed_connector_fields = [self::IS_NOTNULL, self::FIELDTYPE, self::LENGTH];
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  if (isset(self::$key_maps[$key])) {
62  return self::$key_maps[$key];
63  }
64 
65  return $key;
66  }
67 
71  public static function getAllowedConnectorFields(): array
72  {
73  return self::$allowed_connector_fields;
74  }
75 
79  public static function getAllowedDescriptionFields(): array
80  {
81  return self::$allowed_description_fields;
82  }
83 
84  public static function getInstance(ActiveRecord $activeRecord): \arFieldList
85  {
86  $self = new self($activeRecord);
87  $self->initRawFields($activeRecord);
88  $self->initFields();
89 
90  return $self;
91  }
92 
96  public static function getInstanceFromStorage(\ActiveRecord $activeRecord): \arFieldList
97  {
98  $self = new self($activeRecord);
99  $self->initRawFields($activeRecord);
100  $self->initFields();
101 
102  return $self;
103  }
104 
108  public function getArrayForConnector(): array
109  {
110  $return = [];
111  foreach ($this->getFields() as $arField) {
112  $return[$arField->getName()] = $arField->getAttributesForConnector();
113  }
114 
115  return $return;
116  }
117 
118  protected function initFields(): void
119  {
120  foreach ($this->getRawFields() as $fieldname => $attributes) {
121  if (self::checkAttributes($attributes)) {
122  $arField = new arField();
123  $arField->loadFromArray($fieldname, $attributes);
124  $this->fields[] = $arField;
125  if ($arField->getPrimary()) {
126  $this->setPrimaryField($arField);
127  }
128  }
129  }
130  }
131 
132  public function getFieldByName(string $field_name): ?arField
133  {
134  $field = null;
135  static $field_map;
136  $field_key = $this->activeRecord->getConnectorContainerName() . '.' . $field_name;
137  if (is_array($field_map) && array_key_exists($field_key, $field_map)) {
138  return $field_map[$field_key];
139  }
140  foreach ($this->getFields() as $field) {
141  if ($field->getName() === $field_name) {
142  $field_map[$field_key] = $field;
143 
144  return $field;
145  }
146  }
147  return null;
148  }
149 
150  public function isField(string $field_name): bool
151  {
152  $is_field = false;
153  foreach ($this->getFields() as $arField) {
154  if ($arField->getName() === $field_name) {
155  $is_field = true;
156  }
157  }
158 
159  return $is_field;
160  }
161 
162  public function getPrimaryFieldName(): string
163  {
164  return $this->getPrimaryField()->getName();
165  }
166 
167  public function getPrimaryFieldType(): string
168  {
169  return $this->getPrimaryField()->getFieldType();
170  }
171 
172  protected function initRawFields(ActiveRecord $ar): void
173  {
174  $regex = "/[\t ]*\\* @(" . implode('|', self::$prefixes) . ")_([a-zA-Z0-9_]+)[\t ]+([a-zA-Z0-9_]+)/u";
175  $reflection = new ReflectionClass($ar);
176  $raw_fields = [];
177  foreach ($reflection->getProperties() as $property) {
178  if (in_array($property->getName(), self::$protected_names)) {
179  continue;
180  }
181  $properties_array = [];
182  $has_property = false;
183  foreach (explode("\n", $property->getDocComment()) as $line) {
184  if (preg_match($regex, $line, $matches)) {
185  $has_property = true;
186  $properties_array[$matches[2]] = $matches[3];
187  }
188  }
189  if ($has_property) {
190  $raw_fields[$property->getName()] = $properties_array;
191  }
192  }
193 
194  $this->setRawFields($raw_fields);
195  }
196 
197  protected static function isAllowedAttribute(string $attribute_name): bool
198  {
199  return in_array($attribute_name, array_merge(self::$allowed_description_fields, [self::HAS_FIELD]), true);
200  }
201 
202  protected static function checkAttributes(array $attributes): bool
203  {
204  if (isset($attributes[self::HAS_FIELD]) && $attributes[self::HAS_FIELD] === 'true') {
205  foreach (array_keys($attributes) as $atr) {
206  if (!self::isAllowedAttribute($atr)) {
207  return false;
208  }
209  }
210  } else {
211  return false;
212  }
213 
214  return true;
215  }
216 
220  public function setFields(array $fields): void
221  {
222  $this->fields = $fields;
223  }
224 
228  public function getFields(): array
229  {
230  return $this->fields;
231  }
232 
233  public function setPrimaryField(\arField $arField): void
234  {
235  $this->primary_field = $arField;
236  }
237 
241  public function getPrimaryField(): \arField|array
242  {
243  return $this->primary_field;
244  }
245 
249  public function setRawFields(array $raw_fields): void
250  {
251  $this->raw_fields = $raw_fields;
252  }
253 
257  public function getRawFields(): array
258  {
259  return $this->raw_fields;
260  }
261 
265  public function setPrimaryFields(array $primary_fields): void
266  {
267  $this->primary_fields = $primary_fields;
268  }
269 
273  public function getPrimaryFields(): array
274  {
275  return $this->primary_fields;
276  }
277 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFields(array $fields)
static mapKey(string $key)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance(ActiveRecord $activeRecord)
static getAllowedConnectorFields()
static getInstanceFromStorage(\ActiveRecord $activeRecord)
static array $prefixes
static isAllowedAttribute(string $attribute_name)
isField(string $field_name)
setPrimaryFields(array $primary_fields)
setPrimaryField(\arField $arField)
__construct(protected ActiveRecord $activeRecord)
arFieldList constructor.
static array $allowed_connector_fields
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static checkAttributes(array $attributes)
setRawFields(array $raw_fields)
arField array $primary_field
static getAllowedDescriptionFields()
static array $allowed_description_fields
getFieldByName(string $field_name)
static array $protected_names
initRawFields(ActiveRecord $ar)
static array $key_maps