1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Gedmo\Exception\InvalidArgumentException; |
7
|
|
|
use Gedmo\Sluggable\Mapping\Driver\Fluent as FluentDriver; |
8
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
9
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
10
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
11
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
12
|
|
|
|
13
|
|
|
class Sluggable implements Buildable, Extension |
14
|
|
|
{ |
15
|
|
|
const MACRO_METHOD = 'sluggable'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ExtensibleClassMetadata |
19
|
|
|
*/ |
20
|
|
|
protected $classMetadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $fieldName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $fields = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $handlers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $style = 'default'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $dateFormat = 'Y-m-d-H:i'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $updatable = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $unique = true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var null |
59
|
|
|
*/ |
60
|
|
|
protected $unique_base = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $separator = '-'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $prefix = ''; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $suffix = ''; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* List of types which are valid for slug and sluggable fields |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
private $validTypes = [ |
83
|
|
|
'string', |
84
|
|
|
'text', |
85
|
|
|
'integer', |
86
|
|
|
'int', |
87
|
|
|
'datetime', |
88
|
|
|
'citext', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
93
|
|
|
* @param string $fieldName |
94
|
|
|
* @param array|string $fields |
95
|
|
|
*/ |
96
|
|
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $fields) |
97
|
|
|
{ |
98
|
|
|
$this->isValidField($classMetadata, $fieldName); |
99
|
|
|
|
100
|
|
|
$this->classMetadata = $classMetadata; |
101
|
|
|
$this->fieldName = $fieldName; |
102
|
|
|
$this->baseOn($fields); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the name of the actual extension. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getExtensionName() |
111
|
|
|
{ |
112
|
|
|
return FluentDriver::EXTENSION_NAME; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public static function enable() |
119
|
|
|
{ |
120
|
|
|
Field::macro(static::MACRO_METHOD, function (Field $builder, $fields) { |
121
|
|
|
return new static($builder->getClassMetadata(), $builder->getName(), $fields); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Execute the build process |
127
|
|
|
*/ |
128
|
|
|
public function build() |
129
|
|
|
{ |
130
|
|
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
131
|
|
|
'slugs' => [ |
132
|
|
|
$this->fieldName => $this->makeConfiguration() |
133
|
|
|
] |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array|string $fields |
139
|
|
|
* @return Sluggable |
140
|
|
|
*/ |
141
|
|
|
public function baseOn($fields) |
142
|
|
|
{ |
143
|
|
|
$this->fields = is_array($fields) ? $fields : [$fields]; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array|string $handlers |
150
|
|
|
* @return Sluggable |
151
|
|
|
*/ |
152
|
|
|
public function handlers($handlers) |
153
|
|
|
{ |
154
|
|
|
$this->handlers = is_array($handlers) ? $handlers : [$handlers]; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $style |
161
|
|
|
* @return Sluggable |
162
|
|
|
*/ |
163
|
|
|
public function style($style) |
164
|
|
|
{ |
165
|
|
|
$this->style = $style; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $dateFormat |
172
|
|
|
* @return Sluggable |
173
|
|
|
*/ |
174
|
|
|
public function dateFormat($dateFormat) |
175
|
|
|
{ |
176
|
|
|
$this->dateFormat = $dateFormat; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param bool $updatable |
183
|
|
|
* @return Sluggable |
184
|
|
|
*/ |
185
|
|
|
public function updatable($updatable = true) |
186
|
|
|
{ |
187
|
|
|
$this->updatable = $updatable; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param bool $unique |
194
|
|
|
* @return Sluggable |
195
|
|
|
*/ |
196
|
|
|
public function unique($unique = true) |
197
|
|
|
{ |
198
|
|
|
$this->unique = $unique; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param null $unique_base |
205
|
|
|
* @return Sluggable |
206
|
|
|
*/ |
207
|
|
|
public function uniqueBase($unique_base) |
208
|
|
|
{ |
209
|
|
|
$this->unique_base = $unique_base; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $separator |
216
|
|
|
* @return Sluggable |
217
|
|
|
*/ |
218
|
|
|
public function separator($separator) |
219
|
|
|
{ |
220
|
|
|
$this->separator = $separator; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $prefix |
227
|
|
|
* @return Sluggable |
228
|
|
|
*/ |
229
|
|
|
public function prefix($prefix) |
230
|
|
|
{ |
231
|
|
|
$this->prefix = $prefix; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $suffix |
238
|
|
|
* @return Sluggable |
239
|
|
|
*/ |
240
|
|
|
public function suffix($suffix) |
241
|
|
|
{ |
242
|
|
|
$this->suffix = $suffix; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Checks if $field type is valid as Sluggable field |
249
|
|
|
* |
250
|
|
|
* @param ClassMetadata $meta |
251
|
|
|
* @param string $field |
252
|
|
|
* |
253
|
|
|
* @throws InvalidArgumentException |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
protected function isValidField(ClassMetadata $meta, $field) |
257
|
|
|
{ |
258
|
|
|
$mapping = $meta->getFieldMapping($field); |
259
|
|
|
|
260
|
|
|
if (!$mapping || !in_array($mapping['type'], $this->validTypes)) { |
261
|
|
|
throw new InvalidArgumentException('Sluggable field is not a valid field type'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return true; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
private function makeConfiguration() |
271
|
|
|
{ |
272
|
|
|
return [ |
273
|
|
|
'fields' => $this->fields, |
274
|
|
|
'handlers' => $this->handlers, |
275
|
|
|
'slug' => $this->fieldName, |
276
|
|
|
'style' => $this->style, |
277
|
|
|
'dateFormat' => $this->dateFormat, |
278
|
|
|
'updatable' => $this->updatable, |
279
|
|
|
'unique' => $this->unique, |
280
|
|
|
'unique_base' => $this->unique_base, |
281
|
|
|
'separator' => $this->separator, |
282
|
|
|
'prefix' => $this->prefix, |
283
|
|
|
'suffix' => $this->suffix |
284
|
|
|
]; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|