ILIAS  release_8 Revision v8.24
ilADTBasedObject Class Reference

ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted. More...

+ Inheritance diagram for ilADTBasedObject:
+ Collaboration diagram for ilADTBasedObject:

Public Member Functions

 __construct ()
 Constructor Tries to read record from DB, in accordance to current ILIAS behaviour. More...
 
 getProperties ()
 Get all properties. More...
 
 isValid ()
 Validate. More...
 
 __call ($a_method, $a_value)
 Get property magic method ("get<PropertyName>()") Setters are type-specific and cannot be magic. More...
 
 read ()
 Read record. More...
 
 create ()
 Create record (only if valid) More...
 
 update ()
 Update record (only if valid) More...
 
 delete ()
 Delete record. More...
 
 getDBErrors ()
 Get DB errors. More...
 
 translateDBErrorCodes (array $a_codes)
 Translate DB error codes. More...
 
 getAllTranslatedErrors (string $delimiter="\n")
 Get translated error codes (DB, Validation) More...
 

Protected Member Functions

 initProperties ()
 Init properties (aka set ADT definition) More...
 
 parsePrimary (array $a_args)
 Parse incoming primary key. More...
 
 hasPrimary ()
 Check if currently has primary. More...
 
 createPrimaryKeyb ()
 Create new primary key, e.g. More...
 
 initDBBridge (ilADTDBBridge $a_adt_db)
 Init (properties) DB bridge. More...
 
 initActiveRecordInstance ()
 Init active record helper for current table, primary and properties. More...
 

Protected Attributes

ilADT $properties
 
array $db_errors = []
 
ilDBInterface $db
 
ilLanguage $lng
 

Detailed Description

ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted.

Author
Jörg Lützenkirchen luetz.nosp@m.enki.nosp@m.rchen.nosp@m.@lei.nosp@m.fos.c.nosp@m.om

Definition at line 12 of file class.ilADTBasedObject.php.

Constructor & Destructor Documentation

◆ __construct()

ilADTBasedObject::__construct ( )

Constructor Tries to read record from DB, in accordance to current ILIAS behaviour.

Definition at line 24 of file class.ilADTBasedObject.php.

25 {
26 global $DIC;
27 $this->db = $DIC->database();
28 $this->lng = $DIC->language();
29 $this->properties = $this->initProperties();
30
31 // :TODO: to keep constructor "open" we COULD use func_get_args()
32 $this->parsePrimary(func_get_args());
33 $this->read();
34 }
initProperties()
Init properties (aka set ADT definition)
parsePrimary(array $a_args)
Parse incoming primary key.
global $DIC
Definition: feed.php:28

References $DIC, initProperties(), ILIAS\Repository\lng(), parsePrimary(), and read().

+ Here is the call graph for this function:

Member Function Documentation

◆ __call()

ilADTBasedObject::__call (   $a_method,
  $a_value 
)

Get property magic method ("get<PropertyName>()") Setters are type-specific and cannot be magic.

Parameters
string$a_method
mixed$a_value
Returns
ilADT
Exceptions
Exception

Definition at line 72 of file class.ilADTBasedObject.php.

73 {
74 $type = substr($a_method, 0, 3);
75 switch ($type) {
76 case "get":
77 $parsed = strtolower(preg_replace("/([A-Z])/", " $1", substr($a_method, 3)));
78 $parsed = str_replace(" ", "_", trim($parsed));
79 if (!$this->properties->hasElement($parsed)) {
80 throw new Exception("ilADTObject unknown property " . $parsed);
81 }
82 return $this->properties->getElement($parsed);
83
84 default:
85 throw new Exception("ilADTObject unknown type: " . $type);
86 }
87 }
$type

References $type.

◆ create()

ilADTBasedObject::create ( )

Create record (only if valid)

Returns
bool

Definition at line 158 of file class.ilADTBasedObject.php.

158 : bool
159 {
160 if ($this->hasPrimary()) {
161 return $this->update();
162 }
163
164 if ($this->isValid()) {
165 if ($this->createPrimaryKeyb()) {
166 try {
167 $rec = $this->initActiveRecordInstance();
168 $rec->create();
169 } catch (ilADTDBException $e) {
170 $this->db_errors[$e->getColumn()][] = $e->getCode();
171 return false;
172 }
173 return true;
174 }
175 }
176 return false;
177 }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
createPrimaryKeyb()
Create new primary key, e.g.
hasPrimary()
Check if currently has primary.
update()
Update record (only if valid)

References Vendor\Package\$e, createPrimaryKeyb(), hasPrimary(), initActiveRecordInstance(), isValid(), and update().

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createPrimaryKeyb()

ilADTBasedObject::createPrimaryKeyb ( )
abstractprotected

Create new primary key, e.g.

sequence

Returns
bool

Reimplemented in ilADTTest.

Referenced by create().

+ Here is the caller graph for this function:

◆ delete()

ilADTBasedObject::delete ( )

Delete record.

Returns
bool

Definition at line 206 of file class.ilADTBasedObject.php.

206 : bool
207 {
208 if ($this->hasPrimary()) {
209 $rec = $this->initActiveRecordInstance();
210 $rec->delete();
211 return true;
212 }
213 return false;
214 }

References hasPrimary(), and initActiveRecordInstance().

+ Here is the call graph for this function:

◆ getAllTranslatedErrors()

ilADTBasedObject::getAllTranslatedErrors ( string  $delimiter = "\n")

Get translated error codes (DB, Validation)

Parameters
string$delimiter
Returns
string

Definition at line 249 of file class.ilADTBasedObject.php.

249 : string
250 {
251 $tmp = array();
252
253 foreach ($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id) {
254 $tmp[] = $element_id . " [validation]: " . $this->getProperties()->translateErrorCode($error_code);
255 }
256
257 foreach ($this->getDBErrors() as $element_id => $codes) {
258 $tmp[] = $element_id . " [db]: " . implode($delimiter, $this->translateDBErrorCodes($codes));
259 }
260
261 if (count($tmp)) {
262 return get_class($this) . $delimiter . implode($delimiter, $tmp);
263 }
264 return '';
265 }
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
getDBErrors()
Get DB errors.
getProperties()
Get all properties.

References getDBErrors(), getProperties(), and translateDBErrorCodes().

+ Here is the call graph for this function:

◆ getDBErrors()

ilADTBasedObject::getDBErrors ( )

Get DB errors.

Returns
array

Definition at line 220 of file class.ilADTBasedObject.php.

220 : array
221 {
222 return $this->db_errors;
223 }

References $db_errors.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ getProperties()

ilADTBasedObject::getProperties ( )

Get all properties.

Definition at line 50 of file class.ilADTBasedObject.php.

50 : ilADT
51 {
52 return $this->properties;
53 }
ADT base class.
Definition: class.ilADT.php:12

References $properties.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ hasPrimary()

ilADTBasedObject::hasPrimary ( )
abstractprotected

Check if currently has primary.

Returns
bool

Reimplemented in ilADTTest.

Referenced by create(), delete(), initActiveRecordInstance(), read(), and update().

+ Here is the caller graph for this function:

◆ initActiveRecordInstance()

ilADTBasedObject::initActiveRecordInstance ( )
protected

Init active record helper for current table, primary and properties.

Returns
ilADTActiveRecord

@noinspection PhpParamsInspection

Definition at line 123 of file class.ilADTBasedObject.php.

124 {
125 if (!$this->hasPrimary()) {
126 throw new Exception("ilADTBasedObject no primary");
127 }
128
130 $adt_db = $factory->getDBBridgeForInstance($this->properties);
131 $this->initDBBridge($adt_db);
132
133 // use custom error handling
134 //FIXME
135 //$this->db->exception = "ilADTDBException";
136
138 return $factory->getActiveRecordInstance($adt_db);
139 }
ADT Active Record helper class This class expects a valid primary for all actions!
initDBBridge(ilADTDBBridge $a_adt_db)
Init (properties) DB bridge.
$factory
Definition: metadata.php:75

References $factory, ilADTFactory\getInstance(), hasPrimary(), and initDBBridge().

Referenced by create(), delete(), read(), and update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initDBBridge()

ilADTBasedObject::initDBBridge ( ilADTDBBridge  $a_adt_db)
abstractprotected

Init (properties) DB bridge.

Parameters
ilADTDBBridge$a_adt_db

Reimplemented in ilADTTest.

Referenced by initActiveRecordInstance().

+ Here is the caller graph for this function:

◆ initProperties()

ilADTBasedObject::initProperties ( )
abstractprotected

Init properties (aka set ADT definition)

Returns
ilADT

Reimplemented in ilADTTest.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ isValid()

ilADTBasedObject::isValid ( )

Validate.

Returns
bool

Definition at line 59 of file class.ilADTBasedObject.php.

59 : bool
60 {
61 return $this->properties->isValid();
62 }

Referenced by create(), and update().

+ Here is the caller graph for this function:

◆ parsePrimary()

ilADTBasedObject::parsePrimary ( array  $a_args)
abstractprotected

Parse incoming primary key.

Parameters
array$a_args
See also
__construct()

Reimplemented in ilADTTest.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ read()

ilADTBasedObject::read ( )

Read record.

Returns
bool

Definition at line 145 of file class.ilADTBasedObject.php.

145 : bool
146 {
147 if ($this->hasPrimary()) {
148 $rec = $this->initActiveRecordInstance();
149 return $rec->read();
150 }
151 return false;
152 }

References hasPrimary(), and initActiveRecordInstance().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateDBErrorCodes()

ilADTBasedObject::translateDBErrorCodes ( array  $a_codes)

Translate DB error codes.

Parameters
array$a_codes
Returns
array

Definition at line 230 of file class.ilADTBasedObject.php.

230 : array
231 {
232 $res = array();
233
234 foreach ($a_codes as $code) {
235 switch ($code) {
236 default:
237 $res[] = "Unknown ADT error code " . $code;
238 break;
239 }
240 }
241 return $res;
242 }
$res
Definition: ltiservices.php:69

References $res.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ update()

ilADTBasedObject::update ( )

Update record (only if valid)

Returns
bool

Definition at line 183 of file class.ilADTBasedObject.php.

183 : bool
184 {
185 if (!$this->hasPrimary()) {
186 return $this->create();
187 }
188
189 if ($this->isValid()) {
190 try {
191 $rec = $this->initActiveRecordInstance();
192 $rec->update();
193 } catch (ilADTDBException $e) {
194 $this->db_errors[$e->getColumn()][] = $e->getCode();
195 return false;
196 }
197 return true;
198 }
199 return false;
200 }
create()
Create record (only if valid)

References Vendor\Package\$e, create(), hasPrimary(), initActiveRecordInstance(), and isValid().

Referenced by create().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $db

ilDBInterface ilADTBasedObject::$db
protected

Definition at line 17 of file class.ilADTBasedObject.php.

◆ $db_errors

array ilADTBasedObject::$db_errors = []
protected

Definition at line 15 of file class.ilADTBasedObject.php.

Referenced by getDBErrors().

◆ $lng

ilLanguage ilADTBasedObject::$lng
protected

Definition at line 18 of file class.ilADTBasedObject.php.

◆ $properties

ilADT ilADTBasedObject::$properties
protected

Definition at line 14 of file class.ilADTBasedObject.php.

Referenced by getProperties().


The documentation for this class was generated from the following file: