1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace LaravelUi5\OData\Edm;
6:
7: use LaravelUi5\OData\Edm\Contracts\Annotation\AnnotationInterface;
8: use LaravelUi5\OData\Edm\Contracts\Type\EnumTypeInterface;
9: use LaravelUi5\OData\Edm\Contracts\FunctionInterface;
10: use LaravelUi5\OData\Edm\Contracts\SchemaInterface;
11: use LaravelUi5\OData\Edm\Contracts\Type\ComplexTypeInterface;
12: use LaravelUi5\OData\Edm\Contracts\Type\EntityTypeInterface;
13: use LaravelUi5\OData\Edm\Contracts\Type\TypeDefinitionInterface;
14:
15: final readonly class Schema implements SchemaInterface
16: {
17: use HasAnnotations;
18:
19: /**
20: * @param list<EntityTypeInterface> $entityTypes
21: * @param list<ComplexTypeInterface> $complexTypes
22: * @param list<EnumTypeInterface> $enumTypes
23: * @param list<TypeDefinitionInterface> $typeDefinitions
24: * @param list<FunctionInterface> $functions all overloads flat; getFunctions() groups by name
25: * @param list<AnnotationInterface> $annotations
26: */
27: public function __construct(
28: private string $namespace,
29: private ?string $alias = null,
30: private array $entityTypes = [],
31: private array $complexTypes = [],
32: private array $enumTypes = [],
33: private array $typeDefinitions = [],
34: private array $functions = [],
35: array $annotations = [],
36: ) {
37: $this->annotations = $annotations;
38: }
39:
40: public function getNamespace(): string
41: {
42: return $this->namespace;
43: }
44:
45: public function getAlias(): ?string
46: {
47: return $this->alias;
48: }
49:
50: // ── Entity types ─────────────────────────────────────────────────────────
51:
52: public function getEntityTypes(): array
53: {
54: return $this->entityTypes;
55: }
56:
57: public function getEntityType(string $name): ?EntityTypeInterface
58: {
59: foreach ($this->entityTypes as $type) {
60: if ($type->getName() === $name) {
61: return $type;
62: }
63: }
64: return null;
65: }
66:
67: // ── Complex types ─────────────────────────────────────────────────────────
68:
69: public function getComplexTypes(): array
70: {
71: return $this->complexTypes;
72: }
73:
74: public function getComplexType(string $name): ?ComplexTypeInterface
75: {
76: foreach ($this->complexTypes as $type) {
77: if ($type->getName() === $name) {
78: return $type;
79: }
80: }
81: return null;
82: }
83:
84: // ── Enum types ────────────────────────────────────────────────────────────
85:
86: public function getEnumTypes(): array
87: {
88: return $this->enumTypes;
89: }
90:
91: public function getEnumType(string $name): ?EnumTypeInterface
92: {
93: foreach ($this->enumTypes as $type) {
94: if ($type->getName() === $name) {
95: return $type;
96: }
97: }
98: return null;
99: }
100:
101: // ── Type definitions ──────────────────────────────────────────────────────
102:
103: public function getTypeDefinitions(): array
104: {
105: return $this->typeDefinitions;
106: }
107:
108: public function getTypeDefinition(string $name): ?TypeDefinitionInterface
109: {
110: foreach ($this->typeDefinitions as $def) {
111: if ($def->getName() === $name) {
112: return $def;
113: }
114: }
115: return null;
116: }
117:
118: // ── Functions ─────────────────────────────────────────────────────────────
119:
120: public function getFunctions(): array
121: {
122: $grouped = [];
123: foreach ($this->functions as $function) {
124: $grouped[$function->getName()][] = $function;
125: }
126: return $grouped;
127: }
128:
129: public function getFunction(string $name): array
130: {
131: $result = [];
132: foreach ($this->functions as $function) {
133: if ($function->getName() === $name) {
134: $result[] = $function;
135: }
136: }
137: return $result;
138: }
139: }
140: