1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasMorphTypeTrait |
7
|
|
|
* @package Nip\Records\Relations\Traits |
8
|
|
|
*/ |
9
|
|
|
trait HasMorphTypeTrait |
10
|
|
|
{ |
11
|
|
|
protected $morphPrefix = 'parent'; |
12
|
|
|
|
13
|
|
|
protected $morphTypeField = null; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $params |
18
|
|
|
*/ |
19
|
2 |
|
public function checkParamMorphPrefix($params) |
20
|
|
|
{ |
21
|
2 |
|
if (isset($params['morphPrefix'])) { |
22
|
2 |
|
$this->setMorphPrefix($params['morphPrefix']); |
23
|
2 |
|
unset($params['morphPrefix']); |
24
|
|
|
} |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $params |
29
|
|
|
*/ |
30
|
2 |
|
public function checkParamMorphTypeField($params) |
31
|
|
|
{ |
32
|
2 |
|
if (isset($params['morphTypeField'])) { |
33
|
|
|
$this->setMorphTypeField($params['morphTypeField']); |
34
|
|
|
unset($params['morphTypeField']); |
35
|
|
|
} |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
11 |
|
public function getMorphPrefix(): string |
42
|
|
|
{ |
43
|
11 |
|
return $this->morphPrefix; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $morphPrefix |
48
|
|
|
*/ |
49
|
2 |
|
public function setMorphPrefix(string $morphPrefix) |
50
|
|
|
{ |
51
|
2 |
|
$this->morphPrefix = $morphPrefix; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
10 |
|
public function generateFK() |
58
|
|
|
{ |
59
|
10 |
|
return $this->getMorphPrefix() . '_id'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return null |
64
|
|
|
*/ |
65
|
11 |
|
public function getMorphTypeField() |
66
|
|
|
{ |
67
|
11 |
|
if ($this->morphTypeField === null) { |
68
|
11 |
|
$this->setMorphTypeField( |
69
|
11 |
|
$this->getMorphPrefix() . '_type' |
70
|
|
|
); |
71
|
|
|
} |
72
|
11 |
|
return $this->morphTypeField; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param null $morphTypeField |
|
|
|
|
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
11 |
|
public function setMorphTypeField($morphTypeField) |
80
|
|
|
{ |
81
|
11 |
|
$this->morphTypeField = $morphTypeField; |
82
|
11 |
|
} |
83
|
|
|
} |
84
|
|
|
|