ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.arFieldList.php
Go to the documentation of this file.
1 <?php
2 
3 /******************************************************************************
4  *
5  * This file is part of ILIAS, a powerful learning management system.
6  *
7  * ILIAS is licensed with the GPL-3.0, you should have received a copy
8  * of said license along with the source code.
9  *
10  * If this is not the case or you just want to try ILIAS, you'll find
11  * us at:
12  * https://www.ilias.de
13  * https://github.com/ILIAS-eLearning
14  *
15  *****************************************************************************/
22 {
23  public const HAS_FIELD = 'has_field';
24  public const IS_PRIMARY = 'is_primary';
25  public const IS_NOTNULL = 'is_notnull';
26  public const FIELDTYPE = 'fieldtype';
27  public const LENGTH = 'length';
28  public const SEQUENCE = 'sequence';
29  public const INDEX = 'index';
30  protected static array $prefixes = array('db', 'con');
31  protected static array $protected_names = array('arConnector', 'arFieldList');
32  protected static array $allowed_description_fields = array(
33  'is_unique', // There are many classes which already use this (without any function)
34  self::IS_PRIMARY,
35  self::IS_NOTNULL,
36  self::FIELDTYPE,
37  self::LENGTH,
38  self::SEQUENCE,
39  self::INDEX
40  );
41  protected static array $allowed_connector_fields = array(
42  self::IS_NOTNULL,
43  self::FIELDTYPE,
44  self::LENGTH,
45  );
49  protected $primary_field;
50  protected array $primary_fields = array();
51  protected array $raw_fields = array();
52  protected array $fields = array();
53  protected \ActiveRecord $ar;
54  protected static array $key_maps = array(
55  self::FIELDTYPE => 'type',
56  self::IS_NOTNULL => 'notnull',
57  );
58 
62  public function __construct(ActiveRecord $ar)
63  {
64  $this->ar = $ar;
65  }
66 
67  public static function mapKey(string $key): string
68  {
69  if (isset(self::$key_maps[$key])) {
70  return self::$key_maps[$key];
71  }
72 
73  return $key;
74  }
75 
76 
77  public static function getAllowedConnectorFields(): array
78  {
79  return self::$allowed_connector_fields;
80  }
81 
82  public static function getAllowedDescriptionFields(): array
83  {
84  return self::$allowed_description_fields;
85  }
86 
87  public static function getInstance(ActiveRecord $ar): \arFieldList
88  {
89  $arFieldList = new self($ar);
90  $arFieldList->initRawFields($ar);
91  $arFieldList->initFields();
92 
93  return $arFieldList;
94  }
95 
99  public static function getInstanceFromStorage(\ActiveRecord $ar): \arFieldList
100  {
101  $arFieldList = new self($ar);
102  $arFieldList->initRawFields($ar);
103  $arFieldList->initFields();
104 
105  return $arFieldList;
106  }
107 
108 
109  public function getArrayForConnector(): array
110  {
111  $return = array();
112  foreach ($this->getFields() as $field) {
113  $return[$field->getName()] = $field->getAttributesForConnector();
114  }
115 
116  return $return;
117  }
118 
119  protected function initFields(): void
120  {
121  foreach ($this->getRawFields() as $fieldname => $attributes) {
122  if (self::checkAttributes($attributes)) {
123  $arField = new arField();
124  $arField->loadFromArray($fieldname, $attributes);
125  $this->fields[] = $arField;
126  if ($arField->getPrimary()) {
127  $this->setPrimaryField($arField);
128  }
129  }
130  }
131  }
132 
133  public function getFieldByName(string $field_name): ?arField
134  {
135  $field = null;
136  static $field_map;
137  $field_key = $this->ar->getConnectorContainerName() . '.' . $field_name;
138  if (is_array($field_map) && array_key_exists($field_key, $field_map)) {
139  return $field_map[$field_key];
140  }
141  foreach ($this->getFields() as $field) {
142  if ($field->getName() === $field_name) {
143  $field_map[$field_key] = $field;
144 
145  return $field;
146  }
147  }
148  return null;
149  }
150 
151 
152  public function isField(string $field_name): bool
153  {
154  $is_field = false;
155  foreach ($this->getFields() as $field) {
156  if ($field->getName() === $field_name) {
157  $is_field = true;
158  }
159  }
160 
161  return $is_field;
162  }
163 
164  public function getPrimaryFieldName(): string
165  {
166  return $this->getPrimaryField()->getName();
167  }
168 
169  public function getPrimaryFieldType(): string
170  {
171  return $this->getPrimaryField()->getFieldType();
172  }
173 
174  protected function initRawFields(ActiveRecord $ar): void
175  {
176  $regex = "/[ ]*\\* @(" . implode('|', self::$prefixes) . ")_([a-zA-Z0-9_]*)[ ]*([a-zA-Z0-9_]*)/u";
177  $reflectionClass = new ReflectionClass($ar);
178  $raw_fields = array();
179  foreach ($reflectionClass->getProperties() as $property) {
180  if (in_array($property->getName(), self::$protected_names)) {
181  continue;
182  }
183  $properties_array = array();
184  $has_property = false;
185  foreach (explode("\n", $property->getDocComment()) as $line) {
186  if (preg_match($regex, $line, $matches)) {
187  $has_property = true;
188  $properties_array[(string) $matches[2]] = $matches[3];
189  }
190  }
191  if ($has_property) {
192  $raw_fields[$property->getName()] = $properties_array;
193  }
194  }
195 
196  $this->setRawFields($raw_fields);
197  }
198 
199  protected static function isAllowedAttribute(string $attribute_name): bool
200  {
201  return in_array($attribute_name, array_merge(self::$allowed_description_fields, array(self::HAS_FIELD)), true);
202  }
203 
204  protected static function checkAttributes(array $attributes): bool
205  {
206  if (isset($attributes[self::HAS_FIELD]) && $attributes[self::HAS_FIELD] === 'true') {
207  foreach (array_keys($attributes) as $atr) {
208  if (!self::isAllowedAttribute($atr)) {
209  return false;
210  }
211  }
212  } else {
213  return false;
214  }
215 
216  return true;
217  }
218 
222  public function setFields(array $fields): void
223  {
224  $this->fields = $fields;
225  }
226 
230  public function getFields(): array
231  {
232  return $this->fields;
233  }
234 
235  public function setPrimaryField(\arField $primary_field): void
236  {
237  $this->primary_field = $primary_field;
238  }
239 
243  public function getPrimaryField()
244  {
245  return $this->primary_field;
246  }
247 
251  public function setRawFields(array $raw_fields): void
252  {
253  $this->raw_fields = $raw_fields;
254  }
255 
256  public function getRawFields(): array
257  {
258  return $this->raw_fields;
259  }
260 
261  public function setPrimaryFields(array $primary_fields): void
262  {
263  $this->primary_fields = $primary_fields;
264  }
265 
266  public function getPrimaryFields(): array
267  {
268  return $this->primary_fields;
269  }
270 }
Class arField.
$attributes
Definition: metadata.php:248
setFields(array $fields)
static mapKey(string $key)
static getInstance(ActiveRecord $ar)
ActiveRecord $ar
$errors fields
Definition: imgupload.php:67
static getAllowedConnectorFields()
static array $prefixes
static isAllowedAttribute(string $attribute_name)
isField(string $field_name)
setPrimaryFields(array $primary_fields)
string $key
Consumer key/client ID value.
Definition: System.php:193
static array $allowed_connector_fields
setPrimaryField(\arField $primary_field)
Class arFieldList.
static checkAttributes(array $attributes)
setRawFields(array $raw_fields)
static getInstanceFromStorage(\ActiveRecord $ar)
static getAllowedDescriptionFields()
static array $allowed_description_fields
getFieldByName(string $field_name)
__construct(ActiveRecord $ar)
arFieldList constructor.
static array $protected_names
initRawFields(ActiveRecord $ar)
static array $key_maps