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