ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilObjectXMLParser.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  public array $object_data = [];
22 
23  private int $ref_id = 0;
24  private int $parent_id = 0;
25  private int $curr_obj = 0;
26  private array $time_target = [];
27  private string $cdata = '';
28 
29  public function __construct(string $a_xml_data = '', ?bool $throw_exception = false)
30  {
32  $this->setXMLContent($a_xml_data);
33  }
34 
35  public function getObjectData(): array
36  {
37  return $this->object_data;
38  }
39 
44  public function setHandlers($a_xml_parser): void
45  {
46  xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
47  xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
48  }
49 
53  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
54  {
55  switch ($a_name) {
56  case 'Objects':
57  $this->curr_obj = -1;
58  break;
59 
60  case 'Object':
62 
63  $this->addProperty('type', (string) $a_attribs['type']);
64  if (array_key_exists('obj_id', $a_attribs)) {
65  $this->addProperty(
66  'obj_id',
67  is_numeric($a_attribs['obj_id']) ? (int) $a_attribs["obj_id"] : ilUtil::__extractId(
68  $a_attribs["obj_id"] ?? '',
70  )
71  );
72  }
73  if (isset($a_attribs['offline'])) {
74  $this->addProperty('offline', $a_attribs['offline']);
75  }
76 
77 
78 
79  break;
80 
81  case 'ImportId':
82  case 'LastUpdate':
83  case 'CreateDate':
84  case 'Owner':
85  case 'Description':
86  case 'Title':
87  break;
88 
89  case 'References':
90  $this->time_target = [];
91  $this->ref_id = $a_attribs["ref_id"] ?? 0;
92  $this->parent_id = $a_attribs['parent_id'] ?? 0;
93  break;
94 
95  case 'TimeTarget':
96  $this->time_target['timing_type'] = $a_attribs['type'];
97  break;
98 
99  case 'Timing':
100  if (isset($a_attribs['visibility'])) {
101  $this->time_target['timing_visibility'] = $a_attribs['visibility'];
102  }
103  if (isset($a_attribs['starting_time'])) {
104  $this->time_target['starting_time'] = $a_attribs['starting_time'];
105  }
106  if (isset($a_attribs['ending_time'])) {
107  $this->time_target['ending_time'] = $a_attribs['ending_time'];
108  }
109  if (isset($a_attribs['ending_time']) && isset($a_attribs['starting_time'])) {
110  // Validate timing if both times are present
111  if ($a_attribs['ending_time'] < $a_attribs['starting_time']) {
112  throw new ilObjectXMLException('Starting time must be earlier than ending time.');
113  }
114  }
115  break;
116 
117  case 'Suggestion':
118  $this->time_target['changeable'] = $a_attribs['changeable'] ?? false;
119 
120  if (isset($a_attribs['starting_time'])) {
121  $this->time_target['suggestion_start'] = $a_attribs['starting_time'];
122  }
123  if (isset($a_attribs['ending_time'])) {
124  $this->time_target['suggestion_end'] = $a_attribs['ending_time'];
125  }
126  break;
127  }
128  }
129 
135  public function handlerEndTag($a_xml_parser, string $a_name): void
136  {
137  switch ($a_name) {
138  case 'Object':
139  case 'Objects':
140  break;
141 
142  case 'Title':
143  $this->addProperty('title', trim($this->cdata));
144  break;
145 
146  case 'Description':
147  $this->addProperty('description', trim($this->cdata));
148  break;
149 
150  case 'Owner':
151  $this->addProperty('owner', trim($this->cdata));
152  break;
153 
154  case 'CreateDate':
155  $this->addProperty('create_date', trim($this->cdata));
156  break;
157 
158  case 'LastUpdate':
159  $this->addProperty('last_update', trim($this->cdata));
160  break;
161 
162  case 'ImportId':
163  $this->addProperty('import_id', trim($this->cdata));
164  break;
165 
166  case 'References':
167  if ($this->ref_id !== 0 && $this->parent_id !== 0) {
168  $this->addReference($this->ref_id, $this->parent_id, $this->time_target);
169  }
170  break;
171  }
172 
173  $this->cdata = '';
174  }
175 
181  public function handlerCharacterData($a_xml_parser, string $a_data): void
182  {
183  if ($a_data !== "\n") {
184  // Replace multiple tabs with one space
185  $a_data = preg_replace("/\t+/", " ", $a_data);
186 
187  $this->cdata .= $a_data;
188  }
189  }
190 
196  private function addProperty(string $a_name, $a_value): void
197  {
198  $this->object_data[$this->curr_obj][$a_name] = $a_value;
199  }
200 
201  private function addReference(int $a_ref_id, int $a_parent_id, array $a_time_target): void
202  {
203  $reference['ref_id'] = $a_ref_id;
204  $reference['parent_id'] = $a_parent_id;
205  $reference['time_target'] = $a_time_target;
206 
207  if (isset($reference['time_target']['changeable']) && $reference['time_target']['changeable'] &&
208  !isset($reference['time_target']['suggestion_start'], $reference['time_target']['suggestion_end'])) {
209  throw new ilObjectXMLException(
210  'Missing attributes: "starting_time" and "ending_time" required for attribute "changeable"'
211  );
212  }
213 
214  $this->object_data[$this->curr_obj]['references'][] = $reference;
215  }
216 }
const IL_INST_ID
Definition: constants.php:40
addReference(int $a_ref_id, int $a_parent_id, array $a_time_target)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addProperty(string $a_name, $a_value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handlerCharacterData($a_xml_parser, string $a_data)
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerEndTag($a_xml_parser, string $a_name)
setXMLContent(string $a_xml_content)
__construct(string $a_xml_data='', ?bool $throw_exception=false)