1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Luminark\Url\Traits; |
4
|
|
|
|
5
|
|
|
use Luminark\Url\Models\Url; |
6
|
|
|
|
7
|
|
|
trait HasUrlTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Temporary internal variable for updated URI. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $_uri = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Url object representing the URL which points to this resource. |
18
|
|
|
* |
19
|
|
|
* @return Illuminate\Database\Eloquent\Relations\MorphOne |
20
|
|
|
*/ |
21
|
|
|
public function url() |
22
|
|
|
{ |
23
|
|
|
return $this->morphOne($this->getUrlClass(), 'resource'); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Overrides Eloquent Model's default attribute getting and gets |
28
|
|
|
* the currently set URI. |
29
|
|
|
* |
30
|
|
|
* @return string|null URI |
31
|
|
|
*/ |
32
|
|
|
public function getUriAttribute() |
33
|
|
|
{ |
34
|
|
|
return $this->_uri === false |
35
|
|
|
? ($this->url ? $this->url->uri : null) |
|
|
|
|
36
|
|
|
: $this->_uri; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Overrides Eloquent Model's default attribute setting to store the URI |
41
|
|
|
* in memory only. Override this method if you need to have |
42
|
|
|
* the URI stored as model attribute in the database. |
43
|
|
|
*/ |
44
|
|
|
public function setUriAttribute($uri) |
45
|
|
|
{ |
46
|
|
|
$this->_uri = $uri === false ? null : $uri; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Saves the URI and related URL object for the model. |
51
|
|
|
* |
52
|
|
|
* @return Luminark\Url\Interfaces\HasUrlInterface URL resource object |
53
|
|
|
*/ |
54
|
|
|
public function saveUri($uri = null) |
55
|
|
|
{ |
56
|
|
|
$urlClass = $this->getUrlClass(); |
57
|
|
|
$originalUrl = $this->url; |
58
|
|
|
$uri = $uri ?: $this->uri; |
|
|
|
|
59
|
|
|
|
60
|
|
|
if ( ! is_null($uri)) { |
61
|
|
|
$uri = $this->prepareUri($uri); |
62
|
|
|
$this->validateUri($uri); |
63
|
|
|
} |
64
|
|
|
// Check if this resource object is already associated with a Url object |
65
|
|
|
if ( ! $originalUrl && ! is_null($uri)) { |
66
|
|
|
// Associate a new Url object with current resource object |
67
|
|
|
$url = $urlClass::create(['uri' => $uri]); |
68
|
|
|
$url->resource()->associate($this); |
69
|
|
|
$url->save(); |
70
|
|
|
// Dissociate content from URL if uri set to null |
71
|
|
|
} elseif (is_null($uri)) { |
72
|
|
|
$originalUrl->resource()->dissociate(); |
73
|
|
|
$originalUrl->delete(); |
74
|
|
|
$this->url = null; |
75
|
|
|
// Redirect old Url object to new Url object |
76
|
|
|
} elseif ($originalUrl && $originalUrl->uri !== $uri) { |
77
|
|
|
$originalUrl->resource()->dissociate(); |
78
|
|
|
$originalUrl->save(); |
79
|
|
|
$newUrl = $urlClass::firstOrCreate(['uri' => $uri]); |
80
|
|
|
$this->redirectUrl($originalUrl, $newUrl); |
81
|
|
|
$newUrl->resource()->associate($this); |
82
|
|
|
$newUrl->save(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Refresh the model with updated URL object |
86
|
|
|
$this->load('url'); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets the class for URL object needed to define Eloquent relationship |
93
|
|
|
* between resource and URL models. Override this method if Url class |
94
|
|
|
* is being extended. |
95
|
|
|
* |
96
|
|
|
* @return string Url class |
97
|
|
|
*/ |
98
|
|
|
protected function getUrlClass() |
99
|
|
|
{ |
100
|
|
|
return Url::class; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Transforms URI value as required before storing it. |
105
|
|
|
* |
106
|
|
|
* @return string Transformed URI value |
107
|
|
|
*/ |
108
|
|
|
protected function prepareUri($uri) |
109
|
|
|
{ |
110
|
|
|
// Remove starting and trailing slash from URI |
111
|
|
|
$uri = preg_replace('/\/$/', '', $uri); |
112
|
|
|
$uri = preg_replace('/^\//', '', $uri); |
113
|
|
|
$uri = strtolower($uri); |
114
|
|
|
|
115
|
|
|
return $uri; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Validates the URL value and makes sure it is unique. Override this |
120
|
|
|
* method if custom validation is needed. |
121
|
|
|
* |
122
|
|
|
* @return boolean URI validity status |
123
|
|
|
*/ |
124
|
|
|
protected function validateUri($uri) |
125
|
|
|
{ |
126
|
|
|
$urlClass = $this->getUrlClass(); |
127
|
|
|
$url = $urlClass::find($uri); |
128
|
|
|
|
129
|
|
|
return $url ? false : true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Modifies the $originalUrl to redirect to $newUrl. |
134
|
|
|
* |
135
|
|
|
* @param Url $originalUrl The URL that will be redirecting |
136
|
|
|
* @param Url $newUrl The URL that will be redirected to |
137
|
|
|
*/ |
138
|
|
|
protected function redirectUrl(Url $originalUrl, Url $newUrl) |
139
|
|
|
{ |
140
|
|
|
$newUrl->redirectsTo()->dissociate(); |
141
|
|
|
$newUrl->save(); |
142
|
|
|
$originalUrl->redirectsTo()->associate($newUrl); |
143
|
|
|
$originalUrl->save(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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
Idable
provides a methodequalsId
that 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.