Test Failed
Push — master ( c6d115...4138a2 )
by philippe
02:27 queued 10s
created

SluggableBehavior::findSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
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)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function beforeSave(Event $event, EntityInterface $entity, /** @scrutinizer ignore-unused */ ArrayObject $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function beforeSave(/** @scrutinizer ignore-unused */ Event $event, EntityInterface $entity, ArrayObject $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        $this->slug($entity);
58
    }
59
}
60