ILIAS  release_8 Revision v8.24
class.arConverter.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 *****************************************************************************/
24class arConverter
25{
26 public const REGEX = "/([a-z]*)\\(([0-9]*)\\)/u";
27 protected static array $field_map = array(
28 'varchar' => arField::FIELD_TYPE_TEXT,
31 'tinyint' => arField::FIELD_TYPE_INTEGER,
32 'smallint' => arField::FIELD_TYPE_INTEGER,
33 'mediumint' => arField::FIELD_TYPE_INTEGER,
35 );
36 protected static array $length_map = array(
44 11 => 4,
45 4 => 1,
46 )
47 );
48 protected string $table_name = '';
49 protected string $class_name = '';
50 protected array $structure = array();
51 protected array $ids = array();
52
53 public function __construct(string $table_name, string $class_name)
54 {
55 $this->setClassName($class_name);
56 $this->setTableName($table_name);
57 $this->readStructure();
58 }
59
60 public function readStructure(): void
61 {
62 $sql = 'DESCRIBE ' . $this->getTableName();
63 $res = self::getDB()->query($sql);
64 while ($data = self::getDB()->fetchObject($res)) {
65 $this->addStructure($data);
66 }
67 }
68
69 public function downloadClassFile(): void
70 {
71 $header = "<?php
72require_once('./Services/ActiveRecord/class.ActiveRecord.php');
73
80class {CLASS_NAME} extends ActiveRecord {
81
86 static function returnDbTableName() {
87 return '{TABLE_NAME}';
88 }
89
93 public function getConnectorContainerName() {
94 return '{TABLE_NAME}';
95 }
96";
97 $txt = str_replace(
98 ['{CLASS_NAME}', '{TABLE_NAME}'],
99 [$this->getClassName(), $this->getTableName()],
100 $header
101 );
102 $all_members = '';
103 foreach ($this->getStructure() as $str) {
104 $member = "/**
105 * @var {DECLARATION}
106 *\n";
107 foreach ($this->returnAttributesForField($str) as $name => $value) {
108 $member .= ' * @con_' . $name . ' ' . $value . "\n";
109 }
110
111 $member .= "*/
112 protected \${FIELD_NAME};
113
114 ";
115
116 $member = str_replace(['{FIELD_NAME}', '{DECLARATION}'], [$str->Field, ' '], $member);
117
118 $all_members .= $member;
119 }
120 $txt = $txt . $all_members . '
121}
122
123?>';
124
125 // echo '<pre>' . print_r(, 1) . '</pre>';
126
127 header('Content-type: application/x-httpd-php');
128 header("Content-Disposition: attachment; filename=\"class." . $this->getClassName() . ".php\"");
129 echo $txt;
130 exit;
131 }
132
136 protected function returnAttributesForField(stdClass $field): array
137 {
138 $attributes = array();
140 $attributes[arFieldList::FIELDTYPE] = self::lookupFieldType($field->Type);
141 $attributes[arFieldList::LENGTH] = self::lookupFieldLength($field->Type);
142
143 if ($field->Null === 'NO') {
145 }
146
147 if ($field->Key === 'PRI') {
149 }
150
151 return $attributes;
152 }
153
154 protected static function lookupFieldType(string $field_name): string
155 {
156 preg_match(self::REGEX, $field_name, $matches);
157
158 return self::$field_map[$matches[1]];
159 }
160
164 protected static function lookupFieldLength(string $field_name)
165 {
166 $field_type = self::lookupFieldType($field_name);
167
168 preg_match(self::REGEX, $field_name, $matches);
169
170 if (self::$length_map[$field_type][$matches[2]]) {
171 return self::$length_map[$field_type][$matches[2]];
172 }
173
174 return $matches[2];
175 }
176
177 public static function getDB(): ilDBInterface
178 {
179 global $DIC;
180
181 return $DIC['ilDB'];
182 }
183
184 public function setTableName(string $table_name): void
185 {
186 $this->table_name = $table_name;
187 }
188
189 public function getTableName(): string
190 {
191 return $this->table_name;
192 }
193
197 public function setStructure(array $structure): void
198 {
199 $this->structure = $structure;
200 }
201
205 public function getStructure(): array
206 {
207 return $this->structure;
208 }
209
210 public function addStructure(stdClass $structure): void
211 {
212 if (!in_array($structure->Field, $this->ids)) {
213 $this->structure[] = $structure;
214 $this->ids[] = $structure->Field;
215 }
216 }
217
218 public function setClassName(string $class_name): void
219 {
220 $this->class_name = $class_name;
221 }
222
223 public function getClassName(): string
224 {
225 return $this->class_name;
226 }
227}
const FIELD_TYPE_TEXT
const FIELD_TYPE_FLOAT
const FIELD_TYPE_DATE
const FIELD_TYPE_TIME
const FIELD_TYPE_INTEGER
const FIELD_TYPE_TIMESTAMP
const FIELD_TYPE_CLOB
$txt
Definition: error.php:13
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
exit
Definition: login.php:28
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247
$attributes
Definition: metadata.php:248
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc