ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
LOMReader.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
25class LOMReader implements ReaderInterface
26{
27 protected array $definition_array;
28 protected int $depth = 0;
29
30 public function __construct(
31 int $depth = 0,
32 ?array $definition_array = null
33 ) {
34 if ($this->depth > 20) {
35 throw new \ilMDStructureException('LOM Structure is nested to deep.');
36 }
37 if (!is_null($definition_array)) {
38 $this->definition_array = $definition_array;
39 return;
40 }
41 $this->definition_array = $this->getDefinitionArray();
42 $this->depth = $depth;
43 }
44
45 protected function getDefinitionArray(): array
46 {
47 return require(__DIR__ . "/../../../StructureDefinition/LOMStructure.php");
48 }
49
51 {
52 return new Definition(
53 $this->name(),
54 $this->unique(),
55 $this->dataType()
56 );
57 }
58
62 public function subDefinitions(): \Generator
63 {
64 $sub_definitions = $this->definition_array['sub'] ?? [];
65 foreach ($this->definition_array['sub'] as $sub_definition) {
66 $reader = new LOMReader(
67 $this->depth + 1,
68 $sub_definition
69 );
70 yield $reader;
71 }
72 }
73
74 protected function name(): string
75 {
76 if (!isset($this->definition_array['name'])) {
77 $this->throwStructureException('missing name');
78 }
79 return $this->definition_array['name'];
80 }
81
82 protected function unique(): bool
83 {
84 if (!isset($this->definition_array['unique'])) {
85 $this->throwStructureException('missing unique');
86 }
87 return (bool) $this->definition_array['unique'];
88 }
89
90 protected function dataType(): Type
91 {
92 if (
93 !isset($this->definition_array['type']) ||
94 is_null($type = $this->definition_array['type'])
95 ) {
96 $this->throwStructureException('invalid data type');
97 }
98 return $type;
99 }
100
101 protected function throwStructureException(string $error): void
102 {
103 throw new \ilMDStructureException(
104 'LOM definition is invalid:' . $error
105 );
106 }
107}
__construct(int $depth=0, ?array $definition_array=null)
Definition: LOMReader.php:30
ilErrorHandling $error
Definition: class.ilias.php:69