-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSlugBehavior.php
More file actions
442 lines (390 loc) · 13.4 KB
/
SlugBehavior.php
File metadata and controls
442 lines (390 loc) · 13.4 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
namespace Muffin\Slug\Model\Behavior;
use ArrayObject;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use Cake\Validation\Validator;
use InvalidArgumentException;
use Muffin\Slug\SluggerInterface;
use RuntimeException;
/**
* Slug behavior.
*/
class SlugBehavior extends Behavior
{
/**
* Configuration.
*
* - field: Name of the field (column) to hold the slug. Defaults to `slug`.
* - displayField: Name of the field(s) to build the slug from. Defaults to
* the `\Cake\ORM\Table::displayField()`.
* - separator: Defaults to `-`.
* - replacements: Hash of characters (or strings) to custom replace before
* generating the slug.
* - maxLength: Maximum length of a slug. Defaults to the field's limit as
* defined in the schema (when possible). Otherwise, no limit.
* - slugger: Class that implements the `Muffin\Slug\SlugInterface`. Defaults
* to `Muffin\Slug\Slugger\CakeSlugger`.
* - unique: Tells if slugs should be unique. Set this to a callable if you
* want to customize how unique slugs are generated. Defaults to `true`.
* - virtual: Tells if slugs are a virtual property of the entity or not to skip
* validating the column's existence and length.
* - scope: Extra conditions or a callable `$callable($entity)` used when
* checking a slug for uniqueness.
* - implementedEvents: Events this behavior listens to. Defaults to
* `['Model.buildValidator' => 'buildValidator', 'Model.beforeSave' => 'beforeSave']`.
* By default the behavior adds validation for the `displayField` fields
* to make them required on record creating. If you don't want these auto
* added validations you can set `implementedEvents` to just
* `['Model.beforeSave' => 'beforeSave']`.
* - onUpdate: Boolean indicating whether slug should be updated when
* updating record, defaults to `false`.
* - onDirty: Boolean indicating whether slug should be updated when
* slug field is dirty, defaults to `false`.
*
* @var array
*/
protected $_defaultConfig = [
'field' => 'slug',
'displayField' => null,
'separator' => '-',
'replacements' => [
'#' => 'hash',
'?' => 'question',
'+' => 'and',
'&' => 'and',
'"' => '',
"'" => '',
],
'maxLength' => null,
'slugger' => 'Muffin\Slug\Slugger\CakeSlugger',
'unique' => true,
'virtual' => false,
'scope' => [],
'implementedEvents' => [
'Model.buildValidator' => 'buildValidator',
'Model.beforeSave' => 'beforeSave',
],
'implementedFinders' => [
'slugged' => 'findSlugged',
],
'implementedMethods' => [
'slug' => 'slug',
],
'onUpdate' => false,
'onDirty' => false,
];
/**
* Slugger instance or callable
*
* @var \Muffin\Slug\SluggerInterface|callable
*/
protected $_slugger;
/**
* Constructor.
*
* @param \Cake\ORM\Table $table The table this behavior is attached to.
* @param array $config The config for this behavior.
*/
public function __construct(Table $table, array $config = [])
{
if (!empty($config['implementedEvents'])) {
$this->_defaultConfig['implementedEvents'] = $config['implementedEvents'];
unset($config['implementedEvents']);
}
parent::__construct($table, $config);
}
/**
* Initialize behavior
*
* @param array $config The configuration settings provided to this behavior.
* @return void
*/
public function initialize(array $config)
{
if (!$this->getConfig('displayField')) {
$this->setConfig('displayField', $this->_table->getDisplayField());
}
if ($this->getConfig('unique') === true) {
$this->setConfig('unique', [$this, '_uniqueSlug']);
}
if ($this->getConfig('virtual')) {
return;
}
$field = $this->getConfig('field');
if (!$this->getTable()->hasField($field)) {
throw new RuntimeException(sprintf(
'SlugBehavior: Table `%s` is missing field `%s`',
$this->getTable()->getTable(),
$field
));
}
$fieldSchema = $this->_table->getSchema()->getColumn($field);
if ($this->getConfig('maxLength') === null) {
if ($fieldSchema['length'] === null) {
throw new RuntimeException(sprintf(
'SlugBehavior: The schema for field `%s.%s` has no length defined',
$this->getTable()->getTable(),
$field
));
}
$this->setConfig(
'maxLength',
$fieldSchema['length']
);
}
}
/**
* Get/set slugger instance.
*
* @param \Muffin\Slug\SluggerInterface|callable $slugger Sets slugger instance if passed.
* If no argument is passed return slugger intance based on behavior config.
* @return callable|\Muffin\Slug\SluggerInterface|null
*/
public function slugger($slugger = null)
{
if ($slugger !== null) {
$this->_slugger = $slugger;
return null;
}
if ($this->_slugger !== null) {
return $this->_slugger;
}
$slugger = $this->getConfig('slugger');
if (is_string($slugger)) {
$this->_slugger = new $slugger();
} elseif (is_array($slugger) && isset($slugger['className'])) {
$this->_slugger = new $slugger['className']();
unset($slugger['className']);
$this->_slugger->config = $slugger + $this->_slugger->config;
} else {
$this->_slugger = $slugger;
}
return $this->_slugger;
}
/**
* Returns list of event this behavior is interested in.
*
* @return array
*/
public function implementedEvents()
{
return $this->getConfig('implementedEvents');
}
/**
* Callback for Model.buildValidator event.
*
* @param \Cake\Event\Event $event The beforeSave event that was fired.
* @param \Cake\Validation\Validator $validator Validator instance.
* @param string $name Validator name.
* @return void
*/
public function buildValidator(Event $event, Validator $validator, $name)
{
foreach ((array)$this->getConfig('displayField') as $field) {
if (strpos($field, '.') === false) {
$validator->requirePresence($field, 'create')
->notEmpty($field);
}
}
}
/**
* Callback for Model.beforeSave event.
*
* @param \Cake\Event\Event $event The afterSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that was saved.
* @param \ArrayObject $options Options.
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$onUpdate = $this->getConfig('onUpdate');
if (!$entity->isNew() && !$onUpdate) {
return;
}
$onDirty = $this->getConfig('onDirty');
$field = $this->getConfig('field');
if (
!$onDirty
&& $entity->isDirty($field)
&& (!$entity->isNew() || (!empty($entity->{$field})))
) {
return;
}
$separator = $this->getConfig('separator');
if ($entity->isDirty($field) && !empty($entity->{$field})) {
$slug = $this->slug($entity, $entity->{$field}, $separator);
$entity->set($field, $slug);
return;
}
$parts = $this->_getPartsFromEntity($entity);
if (empty($parts)) {
return;
}
$slug = $this->slug($entity, implode($separator, $parts), $separator);
$entity->set($field, $slug);
}
/**
* Gets the parts from an entity
*
* @param \Cake\Datasource\EntityInterface $entity Entity
* @return array
*/
protected function _getPartsFromEntity($entity)
{
$parts = [];
foreach ((array)$this->getConfig('displayField') as $displayField) {
$value = Hash::get($entity, $displayField);
if ($value === null && !$entity->isNew()) {
return [];
}
if (!empty($value) || is_numeric($value)) {
$parts[] = $value;
}
}
return $parts;
}
/**
* Custom finder.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query Query.
*/
public function findSlugged(Query $query, array $options)
{
if (!isset($options['slug'])) {
throw new InvalidArgumentException('The `slug` key is required by the `slugged` finder.');
}
return $query->where([
$this->_table->aliasField($this->getConfig('field')) => $options['slug'],
]);
}
/**
* Generates slug.
*
* @param \Cake\ORM\Entity|string $entity Entity to create slug for
* @param string $string String to create slug for.
* @param string|null $separator Separator.
* @return string Slug.
*/
public function slug($entity, $string = null, $separator = null)
{
if ($separator === null) {
$separator = $this->getConfig('separator');
}
if (is_string($entity)) {
if ($string !== null) {
$separator = $string;
}
$string = $entity;
unset($entity);
} elseif (($entity instanceof Entity) && $string === null) {
$string = $this->_getSlugStringFromEntity($entity, $separator);
}
$slug = $this->_slug($string, $separator);
$unique = $this->getConfig('unique');
if (isset($entity) && $unique) {
$slug = $unique($entity, $slug, $separator);
}
return $slug;
}
/**
* Gets the slug string based on an entity
*
* @param \Cake\Datasource\EntityInterface $entity Entity
* @param string $separator Separator
* @return string
*/
protected function _getSlugStringFromEntity($entity, $separator)
{
$string = [];
foreach ((array)$this->getConfig('displayField') as $field) {
if ($entity->getError($field)) {
throw new InvalidArgumentException(sprintf('Error while generating the slug, the field `%s` contains an invalid value.', $field));
}
$string[] = $value = Hash::get($entity, $field);
}
return implode($separator, $string);
}
/**
* Builds the conditions
*
* @param \Cake\ORM\Entity $entity Entity.
* @param string $slug Slug
* @return array
*/
protected function _conditions($entity, $slug)
{
/** @var string $primaryKey */
$primaryKey = $this->_table->getPrimaryKey();
$field = $this->_table->aliasField($this->getConfig('field'));
$conditions = [$field => $slug];
if (is_callable($this->getConfig('scope'))) {
$scope = $this->getConfig('scope');
$conditions += $scope($entity);
} else {
$conditions += $this->getConfig('scope');
}
if ($id = $entity->{$primaryKey}) {
$conditions['NOT'][$this->_table->aliasField($primaryKey)] = $id;
}
return $conditions;
}
/**
* Returns a unique slug.
*
* @param \Cake\ORM\Entity $entity Entity.
* @param string $slug Slug.
* @param string $separator Separator.
* @return string Unique slug.
*/
protected function _uniqueSlug(Entity $entity, $slug, $separator)
{
/** @var string $primaryKey */
$primaryKey = $this->_table->getPrimaryKey();
$field = $this->_table->aliasField($this->getConfig('field'));
$conditions = $this->_conditions($entity, $slug);
$i = 0;
$suffix = '';
$length = $this->getConfig('maxLength');
while ($this->_table->exists($conditions)) {
$i++;
$suffix = $separator . $i;
if ($length && $length < mb_strlen($slug . $suffix)) {
$slug = mb_substr($slug, 0, $length - mb_strlen($suffix));
}
$conditions[$field] = $slug . $suffix;
}
return $slug . $suffix;
}
/**
* Proxies the defined slugger's `slug` method.
*
* @param string $string String to create a slug from.
* @param string $separator String to use as separator/separator.
* @return string Slug.
*/
protected function _slug($string, $separator)
{
$replacements = $this->getConfig('replacements');
$callable = $this->slugger();
if (is_object($callable) && $callable instanceof SluggerInterface) {
$callable = [$callable, 'slug'];
}
$slug = $callable(str_replace(array_keys($replacements), $replacements, $string), $separator);
if (!empty($this->getConfig('maxLength'))) {
$slug = Text::truncate(
$slug,
$this->getConfig('maxLength'),
['ellipsis' => '']
);
}
return $slug;
}
}