|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArjanWestdorp\Exposable\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ArjanWestdorp\Exposable\Signers\Signer; |
|
6
|
|
|
use ArjanWestdorp\Exposable\Exceptions\InvalidGuardException; |
|
7
|
|
|
use ArjanWestdorp\Exposable\Exceptions\InvalidExposableException; |
|
8
|
|
|
|
|
9
|
|
|
trait Exposable |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Expose the model. |
|
13
|
|
|
* |
|
14
|
|
|
* @return \Illuminate\Http\Response |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract public function expose(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Return the url on which we expose the model. |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
9 |
|
public function exposeUrl() |
|
24
|
|
|
{ |
|
25
|
9 |
|
return Signer::url(route('exposable.show', [ |
|
26
|
9 |
|
$this->getExposableKey(), |
|
27
|
8 |
|
$this->getKey(), |
|
|
|
|
|
|
28
|
8 |
|
]))->guard($this->getExposableGuard())->expire($this->getExposableLifetime())->sign(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the key to use when exposing this model. |
|
33
|
|
|
* This will be retrieved from the config. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
* @throws InvalidExposableException |
|
37
|
|
|
*/ |
|
38
|
9 |
|
public function getExposableKey() |
|
39
|
|
|
{ |
|
40
|
9 |
|
$exposables = collect(config('exposable.exposables'))->flip(); |
|
41
|
|
|
|
|
42
|
9 |
|
if (! $exposables->has(self::class)) { |
|
43
|
1 |
|
throw InvalidExposableException::exposableNotDefined(static::class); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
8 |
|
return $exposables->get(self::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the exposable guard for this model. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string|null |
|
53
|
|
|
*/ |
|
54
|
8 |
|
protected function getExposableGuard() |
|
55
|
|
|
{ |
|
56
|
8 |
|
if (property_exists($this, 'exposableGuard')) { |
|
57
|
|
|
return $this->exposableGuard; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
return $this->exposableGuard ?: config('exposable.default-guard'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set the exposable guard of the model. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $guard |
|
67
|
|
|
* @return $this |
|
68
|
|
|
* @throws InvalidGuardException |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function setExposableGuard($guard) |
|
71
|
|
|
{ |
|
72
|
2 |
|
if (! config()->has('exposable.guards.'.$guard)) { |
|
73
|
1 |
|
throw InvalidGuardException::guardNotDefined($guard); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$this->exposableGuard = $guard; |
|
77
|
|
|
|
|
78
|
1 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the exposable lifetime for this model. |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
8 |
|
protected function getExposableLifetime() |
|
87
|
|
|
{ |
|
88
|
8 |
|
if (property_exists($this, 'exposableLifetime')) { |
|
89
|
|
|
return $this->exposableLifetime; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
8 |
|
return $this->exposableLifetime ?: config('exposable.lifetime'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the exposable expire time in minutes. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $expire |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function setExposableLifetime($expire) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->exposableLifetime = $expire; |
|
104
|
|
|
|
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.