Passed
Pull Request — master (#171)
by Alex
04:02
created

EntityGubbins::getFieldNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities;
4
5
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association;
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubMonomorphic;
9
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubPolymorphic;
10
use POData\Common\InvalidOperationException;
11
use POData\Providers\Metadata\ResourceEntityType;
12
13
class EntityGubbins
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var string
22
     */
23
    private $className;
24
25
    /**
26
     * @var EntityField[]
27
     */
28
    private $keyFields = [];
29
30
    /**
31
     * @var EntityField[]
32
     */
33
    private $fields = [];
34
35
    /**
36
     * @var AssociationStubBase[]
37
     */
38
    private $stubs = [];
39
40
    /**
41
     * @var Association[]
42
     */
43
    private $associations = [];
44
    /**
45
     * @var ResourceEntityType
46
     */
47
    private $odataResourceType;
48
49
    /**
50
     * @return ResourceEntityType
51
     */
52
    public function getOdataResourceType()
53
    {
54
        return $this->odataResourceType;
55
    }
56
57
    /**
58
     * @param ResourceEntityType $odataType
59
     */
60
    public function setOdataResourceType(ResourceEntityType $odataType)
61
    {
62
        if ($odataType->isAbstract()) {
63
            $msg = 'OData resource entity type must be concrete';
64
            throw new \InvalidArgumentException($msg);
65
        }
66
        $this->odataResourceType = $odataType;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getName()
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * @param string $name
79
     */
80
    public function setName($name)
81
    {
82
        $this->name = $name;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getClassName()
89
    {
90
        return $this->className;
91
    }
92
93
    /**
94
     * @param string $className
95
     */
96
    public function setClassName($className)
97
    {
98
        $this->className = $className;
99
    }
100
101
    /**
102
     * @return EntityField[]
103
     */
104
    public function getKeyFields()
105
    {
106
        return $this->keyFields;
107
    }
108
109
    /**
110
     * @return EntityField[]
111
     */
112
    public function getFields()
113
    {
114
        return $this->fields;
115
    }
116
117
    /**
118
     * @param EntityField[] $fields
119
     */
120
    public function setFields(array $fields)
121
    {
122
        if (0 == count($fields)) {
123
            $msg = 'Fields array must not be empty for '.$this->getClassName();
124
            throw new \Exception($msg);
125
        }
126
        $keys = [];
127
        foreach ($fields as $propName => $field) {
128
            if (!$field instanceof EntityField) {
129
                $msg = 'Fields array must only have EntityField objects for '.$this->getClassName();
130
                throw new \Exception($msg);
131
            }
132
            if ($field->getIsKeyField()) {
133
                $keys[$propName] = $field;
134
            }
135
        }
136
        if (0 == count($keys)) {
137
            $msg = 'No key field supplied in fields array for '.$this->getClassName();
138
            throw new \Exception($msg);
139
        }
140
        $this->fields = $fields;
141
        $this->keyFields = $keys;
142
    }
143
144
    /**
145
     * @return AssociationStubBase[]
146
     */
147
    public function getStubs()
148
    {
149
        return $this->stubs;
150
    }
151
152
    /**
153
     * @param AssociationStubBase[] $stubs
154
     */
155
    public function setStubs(array $stubs)
156
    {
157
        foreach ($stubs as $field) {
158
            if (!$field instanceof AssociationStubBase) {
159
                $msg = 'Stubs array must only have AssociationStubBase objects';
160
                throw new \Exception($msg);
161
            }
162
        }
163
        $this->stubs = $stubs;
164
    }
165
166
    /**
167
     * @param Association $association
168
     * @param bool        $isFirst
169
     */
170
    public function addAssociation(Association $association, $isFirst = true)
171
    {
172
        if ($association instanceof AssociationMonomorphic) {
173
            $stub = $isFirst ? $association->getFirst() : $association->getLast();
174
            if (null === $stub || (!in_array($stub, $this->stubs) && !($stub instanceof AssociationStubPolymorphic))) {
175
                throw new \InvalidArgumentException('Association cannot be connected to this entity');
176
            }
177
            $propertyName = $stub->getRelationName();
178
        }
179
        if (!isset($propertyName)) {
180
            throw new InvalidOperationException('');
181
        }
182
        $this->associations[$propertyName] = $association;
183
    }
184
185
    /**
186
     * @return String[]
187
     */
188
    protected function getFieldNames()
189
    {
190
        $fieldNames = [];
191
        foreach ($this->fields as $field) {
192
            $fieldNames[] = $field->getName();
193
        }
194
        return $fieldNames;
195
    }
196
197
    /**
198
     * @return String[]
199
     */
200
    protected function getAssociationNames()
201
    {
202
        if (empty($this->stubs)) {
203
            return [];
204
        }
205
        $assocationNames = [];
206
        foreach ($this->stubs as $stub) {
207
            $assocationNames[] = $stub->getRelationName();
208
        }
209
        return $assocationNames;
210
    }
211
212
    /**
213
     * @return Associations\Association[]
214
     */
215
    public function getAssociations()
216
    {
217
        return $this->associations;
218
    }
219
220
    /**
221
     * @param $relName
222
     * @return Association|null
223
     */
224
    public function resolveAssociation($relName)
225
    {
226
        $this->associations = is_array($this->associations) ? $this->associations : [];
0 ignored issues
show
introduced by
The condition is_array($this->associations) is always true.
Loading history...
227
        return array_key_exists($relName, $this->associations) ? $this->associations[$relName] : null;
228
    }
229
230
    /**
231
     * @return bool
232
     */
233
    public function isOK()
234
    {
235
        $fieldNames = $this->getFieldNames();
236
        $associationNames = $this->getAssociationNames();
237
        $intersection = array_intersect($fieldNames, $associationNames);
238
        return 0 === count($intersection);
239
    }
240
}
241