|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model; |
|
4
|
|
|
|
|
5
|
|
|
use App\Classes\Interfaces\Linkable; |
|
6
|
|
|
use App\Classes\Interfaces\Linker; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Link |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $external |
|
14
|
|
|
* |
|
15
|
|
|
* @property Linkable $to |
|
16
|
|
|
* |
|
17
|
|
|
* @package App |
|
18
|
|
|
*/ |
|
19
|
|
|
class Link extends Model |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The table associated with the model. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $table = 'links'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The attributes that are not mass assignable. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $guarded = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The table date columns, casted to Carbon. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo|Collection |
|
45
|
|
|
*/ |
|
46
|
|
|
public function from() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->morphTo(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo|Collection|Linkable |
|
53
|
|
|
*/ |
|
54
|
|
|
public function to() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->morphTo(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function url() |
|
63
|
|
|
{ |
|
64
|
|
|
if($this->isExternal()) |
|
65
|
|
|
return $this->external; |
|
66
|
|
|
|
|
67
|
|
|
return $this->to->route(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isExternal() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->external ? true : false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Linker $model |
|
80
|
|
|
* @param Linkable|null $object |
|
81
|
|
|
* @param string $external |
|
82
|
|
|
* @return $this |
|
|
|
|
|
|
83
|
|
|
*/ |
|
84
|
|
|
public function connect(Linker $model, Linkable $object = null, string $external = "") |
|
85
|
|
|
{ |
|
86
|
|
|
if ($model && $object) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->setAttribute('from_id', $model->getKey()); |
|
89
|
|
|
|
|
90
|
|
|
$this->setAttribute('from_type', $model->getMorphClass()); |
|
91
|
|
|
|
|
92
|
|
|
$this->setAttribute('to_id', $object->getKey()); |
|
93
|
|
|
|
|
94
|
|
|
$this->setAttribute('to_type', $object->getMorphClass()); |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($model && $external) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->setAttribute('from_id', $model->getKey()); |
|
102
|
|
|
|
|
103
|
|
|
$this->setAttribute('from_type', $model->getMorphClass()); |
|
104
|
|
|
|
|
105
|
|
|
$this->setAttribute('external', $external); |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.