1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\objects; |
10
|
|
|
|
11
|
|
|
use flipbox\organizations\records\UserType; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Flipbox Factory <[email protected]> |
15
|
|
|
* @since 1.0.0 |
16
|
|
|
*/ |
17
|
|
|
trait UserTypeMutatorTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param int|null $id |
21
|
|
|
*/ |
22
|
|
|
abstract protected function internalSetTypeId(int $id = null); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return int|null |
26
|
|
|
*/ |
27
|
|
|
abstract protected function internalGetTypeId(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param UserType|null $type |
31
|
|
|
*/ |
32
|
|
|
abstract protected function internalSetType(UserType $type = null); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return UserType|null |
36
|
|
|
*/ |
37
|
|
|
abstract protected function internalGetType(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $id |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setTypeId(int $id = null) |
44
|
|
|
{ |
45
|
|
|
$this->internalSetTypeId($id); |
46
|
|
|
|
47
|
|
|
if (null !== $this->internalGetType() && $id != $this->internalGetType()->id) { |
48
|
|
|
$this->internalSetType(null); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int|null |
56
|
|
|
*/ |
57
|
|
|
public function getTypeId() |
58
|
|
|
{ |
59
|
|
|
if (null === $this->internalGetTypeId() && null != $this->internalGetType()) { |
60
|
|
|
$this->setTypeId($this->internalGetType()->id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->internalGetTypeId(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $type |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setType($type = null) |
71
|
|
|
{ |
72
|
|
|
$this->internalSetType(null); |
73
|
|
|
$this->internalSetTypeId(null); |
74
|
|
|
|
75
|
|
|
if ($type = $this->internalResolveType($type)) { |
76
|
|
|
$this->internalSetType($type); |
|
|
|
|
77
|
|
|
$this->internalSetTypeId($type->id); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return UserType|null |
85
|
|
|
*/ |
86
|
|
|
public function getType() |
87
|
|
|
{ |
88
|
|
|
if ($this->internalGetType() === null) { |
89
|
|
|
$type = $this->resolveType(); |
90
|
|
|
$this->setType($type); |
91
|
|
|
return $type; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$typeId = $this->internalGetTypeId(); |
95
|
|
|
if ($typeId !== null && $typeId != $this->internalGetType()->id) { |
96
|
|
|
$this->internalSetType(null); |
97
|
|
|
return $this->getType(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->internalGetType(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return UserType|null |
105
|
|
|
*/ |
106
|
|
|
protected function resolveType() |
107
|
|
|
{ |
108
|
|
|
if ($type = $this->resolveTypeFromId()) { |
109
|
|
|
return $type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return UserType|null |
117
|
|
|
*/ |
118
|
|
|
private function resolveTypeFromId() |
119
|
|
|
{ |
120
|
|
|
if (null === $this->internalGetTypeId()) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return UserType::findOne($this->internalGetTypeId()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $type |
129
|
|
|
* @return UserType|null |
130
|
|
|
*/ |
131
|
|
|
protected function internalResolveType($type = null) |
132
|
|
|
{ |
133
|
|
|
if ($type === null) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($type instanceof UserType) { |
138
|
|
|
return $type; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return UserType::findOne($type); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: