1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard\Model; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
|
6
|
|
|
class SyncRelationship |
7
|
|
|
{ |
8
|
|
|
/** @var string */ |
9
|
|
|
private $relatedClass; |
10
|
|
|
|
11
|
|
|
/** @var string */ |
12
|
|
|
private $foreignKey; |
13
|
|
|
|
14
|
|
|
/** @var string|null */ |
15
|
|
|
private $ownerKey; |
16
|
|
|
|
17
|
|
|
/** @var string|null */ |
18
|
|
|
private $relation; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SyncRelationship constructor. |
22
|
|
|
* @param string $relatedClass |
23
|
|
|
*/ |
24
|
2 |
|
public function __construct(string $relatedClass) |
25
|
|
|
{ |
26
|
2 |
|
$this->relatedClass = $relatedClass; |
27
|
2 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $relatedClass |
31
|
|
|
* @return SyncRelationship |
32
|
|
|
*/ |
33
|
2 |
|
public static function create(string $relatedClass) : SyncRelationship |
34
|
|
|
{ |
35
|
2 |
|
$class = get_called_class(); |
36
|
2 |
|
return new $class($relatedClass); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
2 |
|
public function getRelatedClass(): string |
43
|
|
|
{ |
44
|
2 |
|
return $this->relatedClass; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Foreign key |
49
|
|
|
* |
50
|
|
|
* @param string $foreignKey |
51
|
|
|
* @return SyncRelationship |
52
|
|
|
*/ |
53
|
1 |
|
public function foreignKey(string $foreignKey) : SyncRelationship |
54
|
|
|
{ |
55
|
1 |
|
$this->foreignKey = $foreignKey; |
56
|
1 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get foreign key |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
public function getForeignKey() : string |
65
|
|
|
{ |
66
|
2 |
|
if ($this->foreignKey) { |
67
|
1 |
|
return $this->foreignKey; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var Model $eloquentModel */ |
71
|
1 |
|
$eloquentModel = new $this->relatedClass(); |
72
|
1 |
|
return $eloquentModel->getForeignKey(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Owner key |
77
|
|
|
* |
78
|
|
|
* @param string $ownerKey |
79
|
|
|
* @return SyncRelationship |
80
|
|
|
*/ |
81
|
1 |
|
public function ownerKey(string $ownerKey) : SyncRelationship |
82
|
|
|
{ |
83
|
1 |
|
$this->ownerKey = $ownerKey; |
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
2 |
|
public function getOwnerKey(): ?string |
91
|
|
|
{ |
92
|
2 |
|
return $this->ownerKey; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Relation |
97
|
|
|
* |
98
|
|
|
* @param string $relation |
99
|
|
|
* @return SyncRelationship |
100
|
|
|
*/ |
101
|
|
|
public function relation(string $relation) : SyncRelationship |
102
|
|
|
{ |
103
|
|
|
$this->relation = $relation; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string|null |
109
|
|
|
*/ |
110
|
2 |
|
public function getRelation(): ?string |
111
|
|
|
{ |
112
|
2 |
|
return $this->relation; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|