-
Notifications
You must be signed in to change notification settings - Fork 22
/
Sluggable.php
297 lines (252 loc) · 5.79 KB
/
Sluggable.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Sluggable\Mapping\Driver\Fluent as FluentDriver;
use LaravelDoctrine\Fluent\Buildable;
use LaravelDoctrine\Fluent\Builders\Field;
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
use LaravelDoctrine\Fluent\Extensions\Extension;
class Sluggable implements Buildable, Extension
{
const MACRO_METHOD = 'sluggable';
/**
* @var ExtensibleClassMetadata
*/
protected $classMetadata;
/**
* @var string
*/
protected $fieldName;
/**
* @var array
*/
protected $fields = [];
/**
* @var array
*/
protected $handlers = [];
/**
* @var string
*/
protected $style = 'default';
/**
* @var string
*/
protected $dateFormat = 'Y-m-d-H:i';
/**
* @var bool
*/
protected $updatable = true;
/**
* @var bool
*/
protected $unique = true;
/**
* @var null
*/
protected $unique_base = null;
/**
* @var string
*/
protected $separator = '-';
/**
* @var string
*/
protected $prefix = '';
/**
* @var string
*/
protected $suffix = '';
/**
* List of types which are valid for slug and sluggable fields.
*
* @var array
*/
private $validTypes = [
'string',
'text',
'integer',
'int',
'datetime',
'citext',
];
/**
* @param ExtensibleClassMetadata $classMetadata
* @param string $fieldName
* @param array|string $fields
*/
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $fields)
{
$this->classMetadata = $classMetadata;
$this->fieldName = $fieldName;
$this->baseOn($fields);
}
/**
* Return the name of the actual extension.
*
* @return string
*/
public function getExtensionName()
{
return FluentDriver::EXTENSION_NAME;
}
/**
* @return void
*/
public static function enable()
{
Field::macro(static::MACRO_METHOD, function (Field $builder, $fields) {
return new static($builder->getClassMetadata(), $builder->getName(), $fields);
});
}
/**
* Execute the build process.
*/
public function build()
{
$this->isValidField($this->classMetadata, $this->fieldName);
$this->classMetadata->appendExtension($this->getExtensionName(), [
'slugs' => [
$this->fieldName => $this->makeConfiguration(),
],
]);
}
/**
* @param array|string $fields
*
* @return Sluggable
*/
public function baseOn($fields)
{
$this->fields = is_array($fields) ? $fields : [$fields];
return $this;
}
/**
* @param array|string $handlers
*
* @return Sluggable
*/
public function handlers($handlers)
{
$this->handlers = is_array($handlers) ? $handlers : [$handlers];
return $this;
}
/**
* @param string $style
*
* @return Sluggable
*/
public function style($style)
{
$this->style = $style;
return $this;
}
/**
* @param string $dateFormat
*
* @return Sluggable
*/
public function dateFormat($dateFormat)
{
$this->dateFormat = $dateFormat;
return $this;
}
/**
* @param bool $updatable
*
* @return Sluggable
*/
public function updatable($updatable = true)
{
$this->updatable = $updatable;
return $this;
}
/**
* @param bool $unique
*
* @return Sluggable
*/
public function unique($unique = true)
{
$this->unique = $unique;
return $this;
}
/**
* @param null $unique_base
*
* @return Sluggable
*/
public function uniqueBase($unique_base)
{
$this->unique_base = $unique_base;
return $this;
}
/**
* @param string $separator
*
* @return Sluggable
*/
public function separator($separator)
{
$this->separator = $separator;
return $this;
}
/**
* @param string $prefix
*
* @return Sluggable
*/
public function prefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
/**
* @param string $suffix
*
* @return Sluggable
*/
public function suffix($suffix)
{
$this->suffix = $suffix;
return $this;
}
/**
* Checks if $field type is valid as Sluggable field.
*
* @param ClassMetadataInfo $meta
* @param string $field
*
* @throws InvalidArgumentException
*
* @return bool
*/
protected function isValidField(ClassMetadataInfo $meta, $field)
{
$mapping = $meta->getFieldMapping($field);
if (!$mapping || !in_array($mapping['type'], $this->validTypes)) {
throw new InvalidArgumentException('Sluggable field is not a valid field type');
}
return true;
}
/**
* @return array
*/
private function makeConfiguration()
{
return [
'fields' => $this->fields,
'handlers' => $this->handlers,
'slug' => $this->fieldName,
'style' => $this->style,
'dateFormat' => $this->dateFormat,
'updatable' => $this->updatable,
'unique' => $this->unique,
'unique_base' => $this->unique_base,
'separator' => $this->separator,
'prefix' => $this->prefix,
'suffix' => $this->suffix,
];
}
}