ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADTTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 protected int $id;
28 protected ilADT $properties;
29
30 public const INTERESTS_NONE = 0;
31 public const INTERESTS_LANGUAGES = 1;
32 public const INTERESTS_IT = 2;
33
34 // properties
35
36 protected function initProperties(): ilADT
37 {
38 // this could be generated from XML or code comments or whatever
39
40 $factory = ilADTFactory::getInstance();
41
42 $properties_def = $factory->getDefinitionInstanceByType("Group");
43
44 $name = $factory->getDefinitionInstanceByType("Text");
45 $name->setMaxLength(255);
46 $properties_def->addElement("name", $name);
47
48 $status = $factory->getDefinitionInstanceByType("Boolean");
49 $properties_def->addElement("active", $status);
50
51 // example options from ilLanguage
52 $this->lng->loadLanguageModule("meta");
53 $options = array();
54 foreach ($this->lng->getInstalledLanguages() as $lang) {
55 $options[$lang] = $this->lng->txt("meta_l_" . $lang);
56 }
57
58 $lang = $factory->getDefinitionInstanceByType("Enum");
59 $lang->setNumeric(false);
60 $lang->setOptions($options);
61 $properties_def->addElement("lang", $lang);
62
63 $age = $factory->getDefinitionInstanceByType("Integer");
64 $age->setMin(0);
65 $age->setMax(120);
66 $properties_def->addElement("age", $age);
67
68 $weight = $factory->getDefinitionInstanceByType("Float");
69 $weight->setMin(0);
70 $weight->setMax(500);
71 $properties_def->addElement("weight", $weight);
72
73 // null?
74 $home = $factory->getDefinitionInstanceByType("Location");
75 $properties_def->addElement("home", $home);
76
77 $tags = $factory->getDefinitionInstanceByType("MultiText");
78 $tags->setMaxLength(255);
79 $tags->setMaxSize(5);
80 $properties_def->addElement("tags", $tags);
81
82 $options = array(
83 self::INTERESTS_NONE => $this->lng->txt("test_interests_none"),
84 self::INTERESTS_LANGUAGES => $this->lng->txt("test_interests_languages"),
85 self::INTERESTS_IT => $this->lng->txt("test_interests_it")
86 );
87
88 $intr = $factory->getDefinitionInstanceByType("MultiEnum");
89 $intr->setOptions($options);
90 $properties_def->addElement("interests", $intr);
91
92 $date = $factory->getDefinitionInstanceByType("Date");
93 $properties_def->addElement("entry_date", $date);
94
95 $dt = $factory->getDefinitionInstanceByType("DateTime");
96 $properties_def->addElement("last_login", $dt);
97
98 // convert ADT definitions to proper ADTs
99 return $factory->getInstanceByDefinition($properties_def);
100 }
101
102
103 // CRUD/DB
104
105 // simple sequence example
106
107 protected function initDBBridge(ilADTDBBridge $a_adt_db): void
108 {
109 $a_adt_db->setTable("adt_test");
110 $a_adt_db->setPrimary(array("id" => array("integer", $this->id)));
111 }
112
113 protected function parsePrimary(array $a_args): void
114 {
115 $this->id = (int) $a_args[0];
116 }
117
118 protected function hasPrimary(): bool
119 {
120 return (bool) $this->id;
121 }
122
123 protected function createPrimaryKeyb(): bool
124 {
125 $this->id = $this->db->nextId("adt_test");
126
127 // INSERT is only done if createPrimaryKey() returns TRUE!
128 return true;
129 }
130}
ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted.
ADT DB bridge base class.
setTable(string $a_table)
setPrimary(array $a_value)
Set primary fields (in MDB2 format)
This is a ADT-based example object It has all supported ADTs and shows DB sequence-handling.
const INTERESTS_NONE
hasPrimary()
Check if currently has primary.
createPrimaryKeyb()
Create new primary key, e.g.
const INTERESTS_IT
initProperties()
Init properties (aka set ADT definition)
parsePrimary(array $a_args)
Parse incoming primary key.
const INTERESTS_LANGUAGES
initDBBridge(ilADTDBBridge $a_adt_db)
Init (properties) DB bridge.
ilADT $properties
ADT base class.
Definition: class.ilADT.php:26
$lang
Definition: xapiexit.php:25