|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spinen\ClickUp\Support\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
6
|
|
|
use Spinen\ClickUp\Exceptions\InvalidRelationshipException; |
|
7
|
|
|
use Spinen\ClickUp\Exceptions\NoClientException; |
|
8
|
|
|
use Spinen\ClickUp\Exceptions\TokenException; |
|
9
|
|
|
use Spinen\ClickUp\Support\Builder; |
|
10
|
|
|
use Spinen\ClickUp\Support\Model; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class BelongsTo |
|
14
|
|
|
* |
|
15
|
|
|
* @package Spinen\ClickUp\Support\Relations |
|
16
|
|
|
*/ |
|
17
|
|
|
class BelongsTo extends Relation |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The child model instance of the relation. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $child; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The foreign key of the parentModel model. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $foreignKey; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new belongs to relationship instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Builder $builder |
|
35
|
|
|
* @param Model $child |
|
36
|
|
|
* @param string $foreignKey |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
* @throws InvalidRelationshipException |
|
40
|
|
|
*/ |
|
41
|
41 |
|
public function __construct(Builder $builder, Model $child, $foreignKey) |
|
42
|
|
|
{ |
|
43
|
41 |
|
$this->foreignKey = $foreignKey; |
|
44
|
|
|
|
|
45
|
|
|
// In the underlying base relationship class, this variable is referred to as |
|
46
|
|
|
// the "parentModel" since most relationships are not inversed. But, since this |
|
47
|
|
|
// one is we will create a "child" variable for much better readability. |
|
48
|
41 |
|
$this->child = $child; |
|
49
|
|
|
|
|
50
|
41 |
|
parent::__construct($builder->whereId($this->getForeignKey()), $this->getChild()); |
|
51
|
41 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the child Model |
|
55
|
|
|
* |
|
56
|
|
|
* @return Model |
|
57
|
|
|
*/ |
|
58
|
41 |
|
public function getChild(): Model |
|
59
|
|
|
{ |
|
60
|
41 |
|
return $this->child; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the foreign key's name |
|
65
|
|
|
* |
|
66
|
|
|
* @return integer|string |
|
67
|
|
|
*/ |
|
68
|
41 |
|
public function getForeignKey() |
|
69
|
|
|
{ |
|
70
|
41 |
|
return $this->getChild()->{$this->getForeignKeyName()}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the name of the foreign key's name |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
41 |
|
public function getForeignKeyName(): string |
|
79
|
|
|
{ |
|
80
|
41 |
|
return $this->foreignKey; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the results of the relationship. |
|
85
|
|
|
* |
|
86
|
|
|
* @return Model|null |
|
87
|
|
|
* @throws GuzzleException |
|
88
|
|
|
* @throws InvalidRelationshipException |
|
89
|
|
|
* @throws NoClientException |
|
90
|
|
|
* @throws TokenException |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function getResults(): ?Model |
|
93
|
|
|
{ |
|
94
|
2 |
|
if (!$this->getForeignKey()) { |
|
95
|
1 |
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $this->getBuilder() |
|
99
|
1 |
|
->get() |
|
100
|
1 |
|
->first(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|