ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.arField.php
Go to the documentation of this file.
1 <?php
2 
24 class arField
25 {
26  protected bool $is_unique = false;
27  public const FIELD_TYPE_TEXT = 'text'; // MySQL varchar, char
28  public const FIELD_TYPE_INTEGER = 'integer'; // MySQL tinyint, smallint, mediumint, int, bigint
29  public const FIELD_TYPE_FLOAT = 'float'; // MySQL double
30  public const FIELD_TYPE_DATE = 'date'; // MySQL date
31  public const FIELD_TYPE_TIME = 'time'; // MySQL time
32  public const FIELD_TYPE_TIMESTAMP = 'timestamp'; // MySQL datetime
33  public const FIELD_TYPE_CLOB = 'clob';
34  protected static array $allowed_attributes = [
35  self::FIELD_TYPE_TEXT => [
39  ],
40  self::FIELD_TYPE_INTEGER => [
45  ],
46  self::FIELD_TYPE_FLOAT => [arFieldList::IS_NOTNULL],
47  self::FIELD_TYPE_DATE => [arFieldList::IS_NOTNULL],
48  self::FIELD_TYPE_TIME => [arFieldList::IS_NOTNULL],
49  self::FIELD_TYPE_TIMESTAMP => [arFieldList::IS_NOTNULL],
50  self::FIELD_TYPE_CLOB => [arFieldList::IS_NOTNULL]
51  ];
52  protected static array $date_fields = [self::FIELD_TYPE_DATE, self::FIELD_TYPE_TIME, self::FIELD_TYPE_TIMESTAMP];
53 
54  public function loadFromArray(string $name, array $array): void
55  {
56  $this->setName($name);
57  foreach ($array as $key => $value) {
58  $this->{$key} = match ($value) {
59  'true' => true,
60  'false' => false,
61  default => $value,
62  };
63  }
64  }
65 
66  public function loadFromStdClass(string $name, stdClass $stdClass): void
67  {
68  $array = (array) $stdClass;
69  $this->loadFromArray($name, $array);
70  }
71 
75  public function getAttributesForConnector(): array
76  {
77  $return = [];
78  foreach (arFieldList::getAllowedConnectorFields() as $field_name) {
79  if (isset($this->{$field_name}) && $this->{$field_name} && self::isAllowedAttribute(
80  $this->getFieldType(),
81  $field_name
82  )) {
83  $return[arFieldList::mapKey($field_name)] = $this->{$field_name};
84  }
85  }
86 
87  return $return;
88  }
89 
93  public function getAttributesForDescription(): array
94  {
95  $return = [];
96  foreach (arFieldList::getAllowedDescriptionFields() as $field_name) {
97  if ($this->{$field_name} && self::isAllowedAttribute($this->getFieldType(), $field_name)) {
98  $return[arFieldList::mapKey($field_name)] = $this->{$field_name};
99  }
100  }
101 
102  return $return;
103  }
104 
105  public function isDateField(): bool
106  {
107  return self::isDateFieldType($this->getFieldType());
108  }
109 
110  protected string $fieldtype;
111  protected ?int $length = null;
112  protected bool $is_primary = false;
113  protected string $name = '';
114  protected bool $is_notnull = false;
115  protected bool $has_field = false;
116  protected bool $sequence = false;
117  protected bool $index = false;
118 
119  public function setFieldType(string $field_type): void
120  {
121  $this->fieldtype = $field_type;
122  }
123 
124  public function getFieldType(): string
125  {
126  return $this->fieldtype;
127  }
128 
129  public function setHasField(bool $has_field): void
130  {
131  $this->has_field = $has_field;
132  }
133 
134  public function getHasField(): bool
135  {
136  return $this->has_field;
137  }
138 
139  public function setLength(int $length): void
140  {
141  $this->length = $length;
142  }
143 
144  public function getLength(): ?int
145  {
146  return $this->length;
147  }
148 
149  public function setName(string $name): void
150  {
151  $this->name = $name;
152  }
153 
154  public function getName(): string
155  {
156  return $this->name;
157  }
158 
159  public function setNotNull(bool $not_null): void
160  {
161  $this->not_null = $not_null;
162  }
163 
164  public function getNotNull(): bool
165  {
166  return $this->not_null;
167  }
168 
169  public function setPrimary(bool $primary): void
170  {
171  $this->is_primary = $primary;
172  }
173 
174  public function getPrimary(): bool
175  {
176  return $this->is_primary;
177  }
178 
179  public function setSequence(bool $sequence): void
180  {
181  $this->sequence = $sequence;
182  }
183 
184  public function getSequence(): bool
185  {
186  return $this->sequence;
187  }
188 
189  public function setIndex(bool $index): void
190  {
191  $this->index = $index;
192  }
193 
194  public function getIndex(): bool
195  {
196  return $this->index;
197  }
198 
199  public static function isAllowedAttribute(string $type, string $field_name): bool
200  {
201  if ($field_name === arFieldList::FIELDTYPE) {
202  return true;
203  }
204  if ($field_name === arFieldList::HAS_FIELD) {
205  return true;
206  }
207  return in_array($field_name, self::$allowed_attributes[$type], true);
208  }
209 
210  public static function isDateFieldType($field_type): bool
211  {
212  return in_array($field_type, self::$date_fields, true);
213  }
214 }
const FIELD_TYPE_FLOAT
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static mapKey(string $key)
setFieldType(string $field_type)
loadFromStdClass(string $name, stdClass $stdClass)
const FIELD_TYPE_CLOB
static getAllowedConnectorFields()
const FIELD_TYPE_TEXT
bool $is_primary
string $fieldtype
bool $has_field
static array $date_fields
const FIELD_TYPE_INTEGER
static array $allowed_attributes
setHasField(bool $has_field)
const FIELD_TYPE_DATE
setPrimary(bool $primary)
loadFromArray(string $name, array $array)
const FIELD_TYPE_TIMESTAMP
setLength(int $length)
bool $sequence
setNotNull(bool $not_null)
static isAllowedAttribute(string $type, string $field_name)
static getAllowedDescriptionFields()
setIndex(bool $index)
const FIELD_TYPE_TIME
setSequence(bool $sequence)
bool $is_notnull
getAttributesForConnector()
string $name
getAttributesForDescription()
bool $is_unique
setName(string $name)
static isDateFieldType($field_type)