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