SluggableBehavior   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 4
c 6
b 1
f 0
dl 0
loc 59
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 18 4
1
<?php
2
3
/**
4
 * This behavior allows automatically generate slug and store it to the model attribute.
5
 * It necessary for creating user-friendly URL's.
6
 *
7
 * Example of usage:
8
 * Append this code to `behaviors()` in model class
9
 * <pre>
10
 *      'sluggableBehavior' => array(
11
 *          'class'          => 'ext.yii-sluggable-behavior.SluggableBehavior',
12
 *          'delimiter'      => '-',    // words delimiter
13
 *          'sluggable_attr' => 'name', // name of attr what need to be "slugged"
14
 *          'slug_attr'      => 'slug', // attr for store slug
15
 *          'allow_update'   => true,   // allow update slug or not
16
 *          'length'         => 5,      // length of words to place into slug
17
 *      ),
18
 * </pre>
19
 */
20
class SluggableBehavior extends CActiveRecordBehavior
0 ignored issues
show
Bug introduced by
The type CActiveRecordBehavior was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
{
22
    /**
23
     * Delimiter for building slug string, default equal to `-`,
24
     * can be changed whe enable behavior.
25
     *
26
     * @var string
27
     */
28
    public $delimiter = '-';
29
30
    /**
31
     * Holds name of model attribute that need to bee slugged
32
     *
33
     * @var string
34
     */
35
    public $sluggable_attr;
36
37
    /**
38
     * Name of the model attribute what will represent slug
39
     *
40
     * @var string
41
     */
42
    public $slug_attr;
43
44
    /**
45
     * Allow change slug attribute when update model
46
     *
47
     * @var bool
48
     */
49
    public $allow_update = false;
50
51
    /**
52
     * Define length of slug, useful when title too long, and you want avoid bulky URL's
53
     *
54
     * @var integer
55
     */
56
    public $length;
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function beforeSave()
62
    {
63
        $model = $this->getOwner();
64
65
        if (!($this->allow_update && $model->isNewRecord)) { // check if we could update slug
66
            return;
67
        }
68
69
        $sluggable_attribute = $this->sluggable_attr;
70
        $slugWords           = explode(' ', $model->$sluggable_attribute);
71
        foreach ($slugWords as $key => $word) {
72
            $parsedWord  = preg_replace('/[^A-Za-z0-9\-]/', '', strtolower($word));
73
            $words[$key] = $parsedWord;
74
        }
75
76
        $slugWords = array_slice($slugWords, 0, $this->length);
77
78
        $model->$sluggable_attribute = implode($this->delimiter, $slugWords);
79
    }
80
}
81