Issues (126)

src/Library/EdmEntityContainer.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\Library;
6
7
use AlgoWeb\ODataMetadata\Enums\ContainerElementKind;
8
use AlgoWeb\ODataMetadata\Enums\SchemaElementKind;
9
use AlgoWeb\ODataMetadata\Exception\InvalidOperationException;
10
use AlgoWeb\ODataMetadata\Helpers\EntityContainerHelpers;
11
use AlgoWeb\ODataMetadata\Helpers\SchemaElementHelpers;
12
use AlgoWeb\ODataMetadata\Helpers\VocabularyAnnotatableHelpers;
13
use AlgoWeb\ODataMetadata\Interfaces\Expressions\IEntitySetReferenceExpression;
14
use AlgoWeb\ODataMetadata\Interfaces\Expressions\IExpression;
15
use AlgoWeb\ODataMetadata\Interfaces\Expressions\IPathExpression;
16
use AlgoWeb\ODataMetadata\Interfaces\IEntityContainer;
17
use AlgoWeb\ODataMetadata\Interfaces\IEntityContainerElement;
18
use AlgoWeb\ODataMetadata\Interfaces\IEntitySet;
19
use AlgoWeb\ODataMetadata\Interfaces\IEntityType;
20
use AlgoWeb\ODataMetadata\Interfaces\IFunctionBase;
21
use AlgoWeb\ODataMetadata\Interfaces\IFunctionImport;
22
use AlgoWeb\ODataMetadata\Interfaces\ITypeReference;
23
use AlgoWeb\ODataMetadata\Internal\RegistrationHelper;
24
use AlgoWeb\ODataMetadata\StringConst;
25
26
class EdmEntityContainer extends EdmElement implements IEntityContainer
27
{
28
    use EntityContainerHelpers;
29
    use SchemaElementHelpers;
30
    use VocabularyAnnotatableHelpers;
31
    /**
32
     * @var string
33
     */
34
    private $namespaceName;
35
    /**
36
     * @var string
37
     */
38
    private $name;
39
    /**
40
     * @var array<IEntityContainerElement>
41
     */
42
    private $containerElements = [];
43
    /**
44
     * @var array<string, IEntitySet>
45
     */
46
    private $entitySetDictionary = [];
47
    /**
48
     * @var array<string, object>
49
     */
50
    private $functionImportDictionary = [];
51
    /**
52
     * @var bool
53
     */
54
    private $isDefault = false;
55
    /**
56
     * @var bool
57
     */
58
    private $isLazyLoadEnabled = true;
59
60
    /**
61
     * Initializes a new instance of the EdmEntityContainer class.
62
     *
63
     * @param string    $namespaceName     namespace of the entity container
64
     * @param string    $name              name of the entity container
65
     * @param bool|null $isDefault
66
     * @param bool|null $isLazyLoadEnabled
67
     */
68
    public function __construct(
69
        string $namespaceName,
70
        string $name,
71
        bool $isDefault = null,
72
        bool $isLazyLoadEnabled = null
73
    ) {
74
        $this->namespaceName     = $namespaceName;
75
        $this->name              = $name;
76
        $this->isDefault         = $isDefault;
77
        $this->isLazyLoadEnabled = $isLazyLoadEnabled;
78
    }
79
80
    /**
81
     * Gets a collection of the elements of this entity container.
82
     *
83
     * @return array|IEntityContainerElement[]
84
     */
85
    public function getElements(): array
86
    {
87
        return $this->containerElements;
88
    }
89
90
    /**
91
     *  Searches for an entity set with the given name in this entity container and returns null if no such set exists.
92
     *
93
     * @param  string          $setName The name of the element being found
94
     * @return IEntitySet|null the requested element, or null if the element does not exist
95
     */
96
    public function findEntitySet(string $setName): ?IEntitySet
97
    {
98
        return array_key_exists($setName, $this->entitySetDictionary) ? $this->entitySetDictionary[$setName] : null;
99
    }
100
101
    /**
102
     * Searches for function imports with the given name in this entity container and returns empty enumerable if no
103
     * such function import exists.
104
     *
105
     * @param  string                  $functionName the name of the function import being found
106
     * @return array|IFunctionImport[] a group of the requested function imports, or an empty enumerable if no
107
     *                                              such function import exists
108
     */
109
    public function findFunctionImports(string $functionName): array
110
    {
111
        if (array_key_exists($functionName, $this->functionImportDictionary)) {
112
            $element = $this->functionImportDictionary[$functionName];
113
            return is_array($element) ? $element : [$element];
0 ignored issues
show
The condition is_array($element) is always false.
Loading history...
114
        }
115
        return [];
116
    }
117
118
    /**
119
     * @return string|null gets the name of this element
120
     */
121
    public function getName(): ?string
122
    {
123
        return $this->name;
124
    }
125
126
    /**
127
     * @return SchemaElementKind gets the kind of this schema element
128
     */
129
    public function getSchemaElementKind(): SchemaElementKind
130
    {
131
        return SchemaElementKind::EntityContainer();
132
    }
133
134
    /**
135
     * @return string gets the namespace this schema element belongs to
136
     */
137
    public function getNamespace(): string
138
    {
139
        return $this->namespaceName;
140
    }
141
    /**
142
     * Creates and adds a function import to this entity container.
143
     *
144
     * @param  string            $name          name of the function import
145
     * @param  ITypeReference    $returnType    return type of the function import
146
     * @param  IExpression|null  $entitySet     An entity set containing entities returned by this function import.
147
     *                                          The two expression kinds supported are IEntitySetReferenceExpression
148
     *                                          and IPathExpression.
149
     * @param  bool|null         $sideEffecting a value indicating whether this function import has side-effects
150
     * @param  bool|null         $composable    can this function import be composed inside expressions
151
     * @param  bool|null         $bindable      can this function import be used as an extension method for the type
152
     *                                          of the first parameter of this function import
153
     * @return EdmFunctionImport created function import
154
     */
155
    public function addFunctionImport(
156
        string $name,
157
        ITypeReference $returnType,
158
        ?IExpression $entitySet,
159
        ?bool $sideEffecting,
160
        ?bool $composable,
161
        ?bool $bindable
162
    ): EdmFunctionImport {
163
        assert(
164
            $entitySet instanceof IEntitySetReferenceExpression || $entitySet instanceof IPathExpression,
165
            'The two expression kinds supported are IEntitySetReferenceExpression and IPathExpression.'
166
        );
167
        $functionImport = new EdmFunctionImport(
168
            $this,
169
            $name,
170
            $returnType,
171
            $entitySet,
172
            true === $sideEffecting,
173
            true === $composable,
174
            true === $bindable
175
        );
176
        $this->addElement($functionImport);
177
        return $functionImport;
178
    }
179
180
181
    /**
182
     *  Creates and adds an entity set to this entity container.
183
     *
184
     * @param  string       $name        name of the entity set
185
     * @param  IEntityType  $elementType the entity type of the elements in this entity set
186
     * @return EdmEntitySet created entity set
187
     */
188
    public function addEntitySet(string $name, IEntityType $elementType): EdmEntitySet
189
    {
190
        $entitySet = new EdmEntitySet($this, $name, $elementType);
191
        $this->addElement($entitySet);
192
        return $entitySet;
193
    }
194
195
    /**
196
     * Adds an entity container element to this entity container.
197
     * @param IEntityContainerElement $element the element to add
198
     */
199
    public function addElement(IEntityContainerElement $element): void
200
    {
201
        $this->containerElements[] = $element;
202
203
        $name = $element->getName();
204
        if (null === $name) {
205
            throw new InvalidOperationException(StringConst::EdmModel_Validator_Syntactic_MissingName());
206
        }
207
208
        switch ($element->getContainerElementKind()) {
209
            case ContainerElementKind::EntitySet():
210
                RegistrationHelper::addElement(
211
                    $element,
212
                    $name,
213
                    $this->entitySetDictionary,
214
                    [RegistrationHelper::class, 'createAmbiguousEntitySetBinding']
215
                );
216
                break;
217
            case ContainerElementKind::FunctionImport():
218
                assert($element instanceof IFunctionBase);
219
                RegistrationHelper::addFunction($element, $name, $this->functionImportDictionary);
220
                break;
221
            case ContainerElementKind::None():
222
                throw new InvalidOperationException(StringConst::EdmEntityContainer_CannotUseElementWithTypeNone());
223
            default:
224
                throw new InvalidOperationException(
225
                    StringConst::UnknownEnumVal_ContainerElementKind(
226
                        $element->getContainerElementKind()->getKey()
227
                    )
228
                );
229
        }
230
    }
231
232
    public function isDefault(): ?bool
233
    {
234
        return $this->isDefault;
235
    }
236
237
238
    public function isLazyLoadEnabled(): ?bool
239
    {
240
        return $this->isLazyLoadEnabled;
241
    }
242
}
243