forked from riasvdv/statamic-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedirect.php
More file actions
167 lines (135 loc) · 4.1 KB
/
Redirect.php
File metadata and controls
167 lines (135 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Rias\StatamicRedirect\Data;
use Rias\StatamicRedirect\Contracts\Redirect as RedirectContract;
use Rias\StatamicRedirect\Data\Concerns\TracksQueriedRelations;
use Rias\StatamicRedirect\Enums\MatchTypeEnum;
use Rias\StatamicRedirect\Facades\Redirect as RedirectFacade;
use Statamic\Contracts\Data\Localization;
use Statamic\Data\ExistsAsFile;
use Statamic\Data\TracksQueriedColumns;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class Redirect implements Localization, RedirectContract
{
use FluentlyGetsAndSets;
use ExistsAsFile;
use TracksQueriedColumns;
use TracksQueriedRelations;
/** @var string|int|null */
protected $id;
/** @var bool */
protected $enabled = true;
/** @var string */
protected $source;
/** @var string */
protected $source_md5;
/** @var string */
protected $destination;
/** @var int */
protected $type = 301;
/** @var string */
protected $locale;
/** @var string */
protected $matchType = MatchTypeEnum::EXACT;
/** @var int */
protected $order;
/** @var string|null */
protected $description;
public function id($id = null)
{
return $this->fluentlyGetOrSet('id')->args(func_get_args());
}
public function enabled($enabled = null)
{
return $this->fluentlyGetOrSet('enabled')->args(func_get_args());
}
public function source($source = null)
{
return $this->fluentlyGetOrSet('source')->args(func_get_args());
}
public function source_md5($source_md5 = null)
{
return $this->fluentlyGetOrSet('source_md5')->args(func_get_args());
}
public function destination($destination = null)
{
return $this->fluentlyGetOrSet('destination')->args(func_get_args());
}
public function type($type = null)
{
return $this->fluentlyGetOrSet('type')->args(func_get_args());
}
public function matchType($matchType = null)
{
return $this->fluentlyGetOrSet('matchType')->args(func_get_args());
}
public function order($order = null)
{
return $this->fluentlyGetOrSet('order')->args(func_get_args());
}
public function locale($locale = null)
{
return $this
->fluentlyGetOrSet('locale')
->setter(function ($locale) {
return $locale instanceof \Statamic\Sites\Site ? $locale->handle() : $locale;
})
->getter(function ($locale) {
return $locale ?? Site::default()->handle();
})
->args(func_get_args());
}
public function setLocaleFromFilePath($path)
{
$explodedPath = explode('/', $path);
$locale = $explodedPath[count($explodedPath) - 2]; // locale is 2nd last path segment
return $this->locale($locale);
}
public function site()
{
return Site::get($this->locale());
}
public function path()
{
return vsprintf('%s/%s/%s%s.yaml', [
rtrim(Stache::store('redirects')->directory(), '/'),
$this->locale(),
! is_null($this->order()) ? $this->order() . '_' : '',
$this->id(),
]);
}
public function description($matchType = null)
{
return $this->fluentlyGetOrSet('description')->args(func_get_args());
}
public function save()
{
return RedirectFacade::save($this);
}
public function delete()
{
return RedirectFacade::delete($this);
}
public function fileData()
{
return [
'id' => $this->id(),
'enabled' => $this->enabled(),
'source' => $this->source(),
'destination' => $this->destination(),
'type' => $this->type(),
'match_type' => $this->matchType(),
'description' => $this->description(),
'order' => $this->order(),
];
}
public function value($key)
{
return $this->get($key);
}
public function get($key, $fallback = null)
{
return $this->$key() ?? $fallback;
}
}