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