Passed
Pull Request — master (#172)
by Alex
05:36
created

AssociationStubBase::getForeignField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
use POData\Common\InvalidOperationException;
6
7
abstract class AssociationStubBase
8
{
9
    /**
10
     * @var AssociationStubRelationType
11
     */
12
    protected $multiplicity;
13
14
    /**
15
     * Foreign key field of this end of relation.
16
     *
17
     * @var string
18
     */
19
    protected $keyField;
20
21
    /**
22
     * Foreign key field of imtermate relation.
23
     *
24
     * @var string
25
     */
26
    protected $throughField;
27
28
    /**
29
     * Foreign key field of other end of relation.
30
     *
31
     * @var string
32
     */
33
    protected $foreignField;
34
35
    /**
36
     * @var string
37
     */
38
    protected $relationName;
39
40
    /**
41
     * Target type this relation points to, if known.  Is null for known-side polymorphic relations.
42
     *
43
     * @var string|null
44
     */
45
    protected $targType;
46
47
    /**
48
     * Base type this relation is attached to.
49
     *
50
     * @var string
51
     */
52
    protected $baseType;
53
54
    /**
55
     * @return string
56
     */
57
    public function getRelationName()
58
    {
59
        return $this->relationName;
60
    }
61
62
    /**
63
     * @param string $relationName
64
     */
65
    public function setRelationName($relationName)
66
    {
67
        $this->relationName = $relationName;
68
    }
69
70
    /**
71
     * @return AssociationStubRelationType
72
     */
73
    public function getMultiplicity()
74
    {
75
        return $this->multiplicity;
76
    }
77
78
    /**
79
     * @param AssociationStubRelationType $multiplicity
80
     */
81
    public function setMultiplicity(AssociationStubRelationType $multiplicity)
82
    {
83
        $this->multiplicity = $multiplicity;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getKeyField()
90
    {
91
        return $this->keyField;
92
    }
93
94
    /**
95
     * @param string $keyField
96
     */
97
    public function setKeyField($keyField)
98
    {
99
        $this->keyField = $keyField;
100
    }
101
102
    public function isCompatible(AssociationStubBase $otherStub)
103
    {
104
        $thisPoly = $this instanceof AssociationStubPolymorphic;
105
        $thatPoly = $otherStub instanceof AssociationStubPolymorphic;
106
        $thisMono = $this instanceof AssociationStubMonomorphic;
107
        $thatMono = $otherStub instanceof AssociationStubMonomorphic;
108
109
        if ($thisPoly && $thatMono) {
110
            return false;
111
        }
112
        if ($thisMono && $thatPoly) {
113
            return false;
114
        }
115
        if (!$this->isOk()) {
116
            return false;
117
        }
118
        if (!$otherStub->isOk()) {
119
            return false;
120
        }
121
        $thisMult = $this->getMultiplicity();
122
        $thatMult = $otherStub->getMultiplicity();
123
        return (AssociationStubRelationType::MANY() == $thisMult || $thisMult != $thatMult);
124
    }
125
126
    /**
127
     * Is this AssociationStub sane?
128
     */
129
    public function isOk()
130
    {
131
        if (null === $this->multiplicity) {
132
            return false;
133
        }
134
        $relName = $this->relationName;
135
        if (null === $relName || !is_string($relName) || empty($relName)) {
0 ignored issues
show
introduced by
The condition is_string($relName) is always true.
Loading history...
136
            return false;
137
        }
138
        $keyField = $this->keyField;
139
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
0 ignored issues
show
introduced by
The condition is_string($keyField) is always true.
Loading history...
140
            return false;
141
        }
142
        $baseType = $this->baseType;
143
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
0 ignored issues
show
introduced by
The condition is_string($baseType) is always true.
Loading history...
144
            return false;
145
        }
146
        $targType = $this->targType;
147
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
148
            return false;
149
        }
150
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
0 ignored issues
show
introduced by
The condition is_string($targType) is always true.
Loading history...
151
            return false;
152
        }
153
        $foreignField = $this->foreignField;
154
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
0 ignored issues
show
introduced by
The condition is_string($foreignField) is always true.
Loading history...
155
            return false;
156
        }
157
        return (null === $targType) === (null === $foreignField);
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getTargType()
164
    {
165
        return $this->targType;
166
    }
167
168
    /**
169
     * @param string $targType
170
     */
171
    public function setTargType($targType)
172
    {
173
        $this->targType = $targType;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getBaseType()
180
    {
181
        return $this->baseType;
182
    }
183
184
    /**
185
     * @param string $baseType
186
     */
187
    public function setBaseType($baseType)
188
    {
189
        $this->baseType = $baseType;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getForeignField()
196
    {
197
        return $this->foreignField;
198
    }
199
200
    /**
201
     * @param string $foreignField
202
     */
203
    public function setForeignField($foreignField)
204
    {
205
        $this->foreignField = $foreignField;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getThroughField()
212
    {
213
        return $this->throughField;
214
    }
215
216
    /**
217
     * @param string $foreignField
218
     */
219
    public function setThroughField($foreignField)
220
    {
221
        $this->throughField = $foreignField;
222
    }
223
224
    /**
225
     * Supply a canonical sort ordering to determine order in associations.
226
     *
227
     * @param AssociationStubBase $other
228
     *
229
     * @return int
230
     */
231
    public function compare(AssociationStubBase $other)
232
    {
233
        $thisClass = get_class($this);
234
        $otherClass = get_class($other);
235
        $classComp = strcmp($thisClass, $otherClass);
236
        if (0 !== $classComp) {
237
            return $classComp / abs($classComp);
238
        }
239
        $thisBase = $this->getBaseType();
240
        $otherBase = $other->getBaseType();
241
        $baseComp = strcmp($thisBase, $otherBase);
242
        if (0 !== $baseComp) {
243
            return $baseComp / abs($baseComp);
244
        }
245
        $thisMethod = $this->getRelationName();
246
        $otherMethod = $other->getRelationName();
247
        $methodComp = strcmp($thisMethod, $otherMethod);
248
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
249
    }
250
}
251