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