1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\MFU\Behaviors\Owner; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Itstructure\MFU\Interfaces\{HasOwnerInterface, BeingOwnerInterface}; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Behavior |
10
|
|
|
* @package Itstructure\MFU\Behaviors\Owner |
11
|
|
|
*/ |
12
|
|
|
abstract class Behavior |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $attributes = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $findChildModelKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $attributeValue |
26
|
|
|
* @return HasOwnerInterface|null |
27
|
|
|
*/ |
28
|
|
|
abstract protected function getChildModel($attributeValue): ?HasOwnerInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param int $ownerId |
32
|
|
|
* @param string $ownerName |
33
|
|
|
* @param string|null $ownerAttribute |
34
|
|
|
* @param bool $removeDependencies |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
abstract protected function removeOwner(int $ownerId, string $ownerName, string $ownerAttribute = null, bool $removeDependencies = false): bool; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $attributes |
41
|
|
|
* @param string $findChildModelKey |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
|
public static function getInstance(array $attributes, string $findChildModelKey = 'id') |
45
|
|
|
{ |
46
|
|
|
return new static($attributes, $findChildModelKey); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Behavior constructor. |
51
|
|
|
* @param array $attributes |
52
|
|
|
* @param string $findChildModelKey |
53
|
|
|
*/ |
54
|
|
|
protected function __construct(array $attributes, string $findChildModelKey) |
55
|
|
|
{ |
56
|
|
|
$this->attributes = $attributes; |
57
|
|
|
$this->findChildModelKey = $findChildModelKey; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param BeingOwnerInterface|Model $ownerModel |
62
|
|
|
*/ |
63
|
|
|
public function link(BeingOwnerInterface $ownerModel): void |
64
|
|
|
{ |
65
|
|
|
foreach ($this->attributes as $attributeName) { |
66
|
|
|
$this->linkOwner($ownerModel, $attributeName, $ownerModel->getBehaviorValue($attributeName)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param BeingOwnerInterface|Model $ownerModel |
72
|
|
|
*/ |
73
|
|
|
public function refresh(BeingOwnerInterface $ownerModel): void |
74
|
|
|
{ |
75
|
|
|
foreach ($this->attributes as $attributeName) { |
76
|
|
|
$this->removeOwner($ownerModel->getPrimaryKey(), $ownerModel->getItsName(), $attributeName); |
77
|
|
|
$this->linkOwner($ownerModel, $attributeName, $ownerModel->getBehaviorValue($attributeName)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param BeingOwnerInterface|Model $ownerModel |
83
|
|
|
*/ |
84
|
|
|
public function clear(BeingOwnerInterface $ownerModel): void |
85
|
|
|
{ |
86
|
|
|
$this->removeOwner($ownerModel->getPrimaryKey(), $ownerModel->getItsName(), null, $ownerModel->getRemoveDependencies()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param BeingOwnerInterface|Model $ownerModel |
91
|
|
|
* @param $attributeName |
92
|
|
|
* @param $attributeValue |
93
|
|
|
*/ |
94
|
|
|
protected function linkOwner(BeingOwnerInterface $ownerModel, $attributeName, $attributeValue): void |
95
|
|
|
{ |
96
|
|
|
if (is_array($attributeValue)) { |
97
|
|
|
foreach ($attributeValue as $item) { |
98
|
|
|
if (empty($item)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
$this->linkOwner($ownerModel, $attributeName, $item); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} else if (!empty($attributeValue)) { |
105
|
|
|
$childModel = $this->getChildModel($attributeValue); |
106
|
|
|
if (empty($childModel)) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$childModel->addOwner($ownerModel->getPrimaryKey(), $ownerModel->getItsName(), $attributeName); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|