1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sluggable\Model\Behavior; |
5
|
|
|
|
6
|
|
|
use ArrayObject; |
7
|
|
|
use Cake\Datasource\EntityInterface; |
8
|
|
|
use Cake\Event\Event; |
9
|
|
|
use Cake\ORM\Behavior; |
10
|
|
|
use Cake\ORM\Entity; |
11
|
|
|
use Cake\ORM\Query; |
12
|
|
|
use Cake\Utility\Text; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sluggable behavior |
16
|
|
|
*/ |
17
|
|
|
class SluggableBehavior extends Behavior |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Default configuration. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $_defaultConfig = [ |
25
|
|
|
'field' => 'name', |
26
|
|
|
'slug' => 'slug', |
27
|
|
|
'replacement' => '-', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* slug description |
32
|
|
|
* |
33
|
|
|
* @param \Cake\ORM\Entity $entity The entity that is going to be a slug |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function slug(Entity $entity) |
37
|
|
|
{ |
38
|
|
|
$config = $this->getConfig(); |
39
|
|
|
$value = $entity->get($config['field']); |
40
|
|
|
$value = str_replace( |
41
|
|
|
['é', 'è', 'ê', 'ë', 'à', 'â', 'î', 'ï', 'ô', 'ù', 'û', 'ç', 'á', 'í', 'ó', 'ú', 'ñ'], |
42
|
|
|
['e', 'e', 'e', 'e', 'a', 'a', 'i', 'i', 'o', 'u', 'u', 'c', 'a', 'i', 'o', 'u', 'n'], |
43
|
|
|
$value |
44
|
|
|
); |
45
|
|
|
$value = strtolower(Text::slug($value, $config['replacement'])); |
46
|
|
|
$entity->set($config['slug'], substr($value, 0, 100)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* [beforeSave description] |
51
|
|
|
* @param \Cake\Event\Event $event The beforeSave event that was fired |
52
|
|
|
* @param \Cake\Datasource\EntityInterface $entity [The entity that is going to be saved] |
53
|
|
|
* @param \ArrayObject $options [description] |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->slug($entity); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.