ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 26 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 38 of file class.ilADTBasedObject.php.

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 }
initProperties()
Init properties (aka set ADT definition)
parsePrimary(array $a_args)
Parse incoming primary key.
global $DIC
Definition: shib_login.php:26

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 86 of file class.ilADTBasedObject.php.

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 }

◆ create()

ilADTBasedObject::create ( )

Create record (only if valid)

Returns
bool

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

172 : 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 }
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 220 of file class.ilADTBasedObject.php.

220 : bool
221 {
222 if ($this->hasPrimary()) {
223 $rec = $this->initActiveRecordInstance();
224 $rec->delete();
225 return true;
226 }
227 return false;
228 }

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 263 of file class.ilADTBasedObject.php.

263 : 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 }
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 234 of file class.ilADTBasedObject.php.

234 : array
235 {
236 return $this->db_errors;
237 }

References $db_errors.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ getProperties()

ilADTBasedObject::getProperties ( )

Get all properties.

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

64 : ilADT
65 {
66 return $this->properties;
67 }
ADT base class.
Definition: class.ilADT.php:26

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 137 of file class.ilADTBasedObject.php.

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 }
ADT Active Record helper class This class expects a valid primary for all actions!
initDBBridge(ilADTDBBridge $a_adt_db)
Init (properties) DB bridge.

References 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 73 of file class.ilADTBasedObject.php.

73 : bool
74 {
75 return $this->properties->isValid();
76 }

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 159 of file class.ilADTBasedObject.php.

159 : bool
160 {
161 if ($this->hasPrimary()) {
162 $rec = $this->initActiveRecordInstance();
163 return $rec->read();
164 }
165 return false;
166 }

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 244 of file class.ilADTBasedObject.php.

244 : 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 }
$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 197 of file class.ilADTBasedObject.php.

197 : 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 }
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 31 of file class.ilADTBasedObject.php.

◆ $db_errors

array ilADTBasedObject::$db_errors = []
protected

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

Referenced by getDBErrors().

◆ $lng

ilLanguage ilADTBasedObject::$lng
protected

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

◆ $properties

ilADT ilADTBasedObject::$properties
protected

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

Referenced by getProperties().


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