|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
|
7
|
|
|
use Nip\Records\Locator\Exceptions\InvalidModelException; |
|
8
|
|
|
use Nip\Records\Relations\Relation; |
|
9
|
|
|
use Nip\Records\Traits\Relations\HasRelationsRecordsTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait HasWithTrait |
|
13
|
|
|
* @package Nip\Records\Relations\Traits |
|
14
|
|
|
*/ |
|
15
|
|
|
trait HasWithTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var RecordManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $with = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var null|string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $withPK = null; |
|
26
|
|
|
|
|
27
|
|
|
/** @noinspection PhpDocMissingThrowsInspection |
|
28
|
|
|
* @return RecordManager |
|
29
|
|
|
*/ |
|
30
|
8 |
|
public function getWith() |
|
31
|
|
|
{ |
|
32
|
8 |
|
if ($this->with == null) { |
|
33
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
34
|
3 |
|
$this->initWith(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
8 |
|
return $this->with; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param RecordManager|HasRelationsRecordsTrait $object |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
8 |
|
public function setWith($object) |
|
45
|
|
|
{ |
|
46
|
8 |
|
$this->with = $object; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
8 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @throws Exception |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function initWith() |
|
55
|
|
|
{ |
|
56
|
3 |
|
$className = $this->getWithClass(); |
|
57
|
3 |
|
$this->setWithClass($className); |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @noinspection PhpDocMissingThrowsInspection |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function getWithClass() |
|
64
|
|
|
{ |
|
65
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
66
|
3 |
|
return inflector()->pluralize($this->getName()); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $params |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function checkParamWith($params) |
|
73
|
|
|
{ |
|
74
|
5 |
|
if (isset($params['with'])) { |
|
75
|
|
|
$this->setWith($params['with']); |
|
76
|
|
|
unset($params['with']); |
|
77
|
|
|
} |
|
78
|
5 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $params |
|
82
|
|
|
*/ |
|
83
|
5 |
|
public function checkParamWithPK($params) |
|
84
|
|
|
{ |
|
85
|
5 |
|
if (isset($params['withPK'])) { |
|
86
|
2 |
|
$this->setWithPK($params['withPK']); |
|
87
|
2 |
|
unset($params['withPK']); |
|
88
|
|
|
} |
|
89
|
5 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** @noinspection PhpDocMissingThrowsInspection |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
*/ |
|
94
|
3 |
|
public function setWithClass($name) |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
3 |
|
$manager = $this->getModelManagerInstance($name); |
|
|
|
|
|
|
98
|
3 |
|
$this->setWith($manager); |
|
99
|
|
|
} catch (InvalidModelException $exception) { |
|
100
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
101
|
|
|
throw new Exception( |
|
102
|
|
|
'Cannot instance records [' . $name . '] in ' . $this->debugString() |
|
|
|
|
|
|
103
|
|
|
. '|| with message ' . $exception->getMessage() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
3 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function getWithPK() |
|
112
|
|
|
{ |
|
113
|
2 |
|
if ($this->withPK === null) { |
|
114
|
1 |
|
$this->initWithPK(); |
|
115
|
|
|
} |
|
116
|
2 |
|
return $this->withPK; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string|null $withPK |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function setWithPK(string $withPK) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$this->withPK = $withPK; |
|
125
|
2 |
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
protected function initWithPK() |
|
128
|
|
|
{ |
|
129
|
1 |
|
$this->withPK = $this->generateWithPK(); |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
1 |
|
protected function generateWithPK() |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->getWith()->getPrimaryKey(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.