ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilADTBasedObject.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
26 abstract class ilADTBasedObject
27 {
28  protected ilADT $properties;
29  protected array $db_errors = [];
30 
31  protected ilDBInterface $db;
32  protected ilLanguage $lng;
33 
38  public function __construct()
39  {
40  global $DIC;
41  $this->db = $DIC->database();
42  $this->lng = $DIC->language();
43  $this->properties = $this->initProperties();
44 
45  // :TODO: to keep constructor "open" we COULD use func_get_args()
46  $this->parsePrimary(func_get_args());
47  $this->read();
48  }
49 
50 
51  //
52  // properties
53  //
54 
59  abstract protected function initProperties(): ilADT;
60 
64  public function getProperties(): ilADT
65  {
66  return $this->properties;
67  }
68 
73  public function isValid(): bool
74  {
75  return $this->properties->isValid();
76  }
77 
86  public function __call($a_method, $a_value)
87  {
88  $type = substr($a_method, 0, 3);
89  switch ($type) {
90  case "get":
91  $parsed = strtolower(preg_replace("/([A-Z])/", " $1", substr($a_method, 3)));
92  $parsed = str_replace(" ", "_", trim($parsed));
93  if (!$this->properties->hasElement($parsed)) {
94  throw new Exception("ilADTObject unknown property " . $parsed);
95  }
96  return $this->properties->getElement($parsed);
97 
98  default:
99  throw new Exception("ilADTObject unknown type: " . $type);
100  }
101  }
102 
103 
104  //
105  // CRUD / active record
106  //
107 
113  abstract protected function parsePrimary(array $a_args): void;
114 
119  abstract protected function hasPrimary(): bool;
120 
125  abstract protected function createPrimaryKeyb(): bool;
126 
131  abstract protected function initDBBridge(ilADTDBBridge $a_adt_db): void;
132 
138  {
139  if (!$this->hasPrimary()) {
140  throw new Exception("ilADTBasedObject no primary");
141  }
142 
143  $factory = ilADTFactory::getInstance();
144  $adt_db = $factory->getDBBridgeForInstance($this->properties);
145  $this->initDBBridge($adt_db);
146 
147  // use custom error handling
148  //FIXME
149  //$this->db->exception = "ilADTDBException";
150 
152  return $factory->getActiveRecordInstance($adt_db);
153  }
154 
159  public function read(): bool
160  {
161  if ($this->hasPrimary()) {
162  $rec = $this->initActiveRecordInstance();
163  return $rec->read();
164  }
165  return false;
166  }
167 
172  public function create(): bool
173  {
174  if ($this->hasPrimary()) {
175  return $this->update();
176  }
177 
178  if ($this->isValid()) {
179  if ($this->createPrimaryKeyb()) {
180  try {
181  $rec = $this->initActiveRecordInstance();
182  $rec->create();
183  } catch (ilADTDBException $e) {
184  $this->db_errors[$e->getColumn()][] = $e->getCode();
185  return false;
186  }
187  return true;
188  }
189  }
190  return false;
191  }
192 
197  public function update(): bool
198  {
199  if (!$this->hasPrimary()) {
200  return $this->create();
201  }
202 
203  if ($this->isValid()) {
204  try {
205  $rec = $this->initActiveRecordInstance();
206  $rec->update();
207  } catch (ilADTDBException $e) {
208  $this->db_errors[$e->getColumn()][] = $e->getCode();
209  return false;
210  }
211  return true;
212  }
213  return false;
214  }
215 
220  public function delete(): bool
221  {
222  if ($this->hasPrimary()) {
223  $rec = $this->initActiveRecordInstance();
224  $rec->delete();
225  return true;
226  }
227  return false;
228  }
229 
234  public function getDBErrors(): array
235  {
236  return $this->db_errors;
237  }
238 
244  public function translateDBErrorCodes(array $a_codes): array
245  {
246  $res = array();
247 
248  foreach ($a_codes as $code) {
249  switch ($code) {
250  default:
251  $res[] = "Unknown ADT error code " . $code;
252  break;
253  }
254  }
255  return $res;
256  }
257 
263  public function getAllTranslatedErrors(string $delimiter = "\n"): string
264  {
265  $tmp = array();
266 
267  foreach ($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id) {
268  $tmp[] = $element_id . " [validation]: " . $this->getProperties()->translateErrorCode($error_code);
269  }
270 
271  foreach ($this->getDBErrors() as $element_id => $codes) {
272  $tmp[] = $element_id . " [db]: " . implode($delimiter, $this->translateDBErrorCodes($codes));
273  }
274 
275  if (count($tmp)) {
276  return get_class($this) . $delimiter . implode($delimiter, $tmp);
277  }
278  return '';
279  }
280 }
$res
Definition: ltiservices.php:66
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
hasPrimary()
Check if currently has primary.
initDBBridge(ilADTDBBridge $a_adt_db)
Init (properties) DB bridge.
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
initProperties()
Init properties (aka set ADT definition)
ADT base class.
Definition: class.ilADT.php:25
__construct()
Constructor Tries to read record from DB, in accordance to current ILIAS behaviour.
parsePrimary(array $a_args)
Parse incoming primary key.
getDBErrors()
Get DB errors.
ADT DB bridge base class.
update()
Update record (only if valid)
global $DIC
Definition: shib_login.php:26
__call($a_method, $a_value)
Get property magic method ("get<PropertyName>()") Setters are type-specific and cannot be magic...
getProperties()
Get all properties.
getAllTranslatedErrors(string $delimiter="\)
Get translated error codes (DB, Validation)
ADT Active Record helper class This class expects a valid primary for all actions! ...
ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted...
createPrimaryKeyb()
Create new primary key, e.g.
create()
Create record (only if valid)