TranslitValidator::prepareForUrl()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 30
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
1
<?php
2
/**
3
 * Yii2 Translit Validator
4
 *
5
 * This file contains validator.
6
 *
7
 * @author  Martin Stolz <[email protected]>
8
 */
9
10
namespace herroffizier\yii2tv;
11
12
use URLify;
13
use yii\validators\Validator;
14
15
class TranslitValidator extends Validator
16
{
17
    /**
18
     * Override default behavior of validator - do not skip empty values.
19
     *
20
     * @var boolean
21
     */
22
    public $skipOnEmpty = false;
23
24
    /**
25
     * Name of attribute which value will be transliterated.
26
     *
27
     * @var string
28
     */
29
    public $sourceAttribute;
30
31
    /**
32
     * Whether to lower case transliterated string.
33
     *
34
     * @var boolean
35
     */
36
    public $lowercase = true;
37
38
    /**
39
     * Whether to prepare transliterated string for URL.
40
     *
41
     * If set to true, all invalid characters matched by $invalidRegexp will be
42
     * replaced with $invalidReplacement string.
43
     *
44
     * @var boolean
45
     */
46
    public $forUrl = true;
47
48
    /**
49
     * Regular expression that matched invalid characters for URL.
50
     *
51
     * By default matches all non-alphanumeric symbols.
52
     *
53
     * @var string
54
     */
55
    public $invalidRegexp = '/[^a-z0-9]+/i';
56
57
    /**
58
     * String with which all invalid characters will be replaced.
59
     *
60
     * @var string
61
     */
62
    public $invalidReplacement = '-';
63
64
    /**
65
     * Trim invalid characters at beginning and at end of given string.
66
     *
67
     * @var boolean
68
     */
69
    public $trimInvalid = false;
70
71
    /**
72
     * Cached escaped invalid replacement.
73
     *
74
     * @var string
75
     */
76
    protected $escapedInvalidReplacement = null;
77
78
    /**
79
     * Escape invalid replacement string.
80
     *
81
     * @return string
82
     */
83
    protected function getEscapedInvalidReplacement()
84
    {
85
        if ($this->escapedInvalidReplacement === null) {
86
            $this->escapedInvalidReplacement = addcslashes($this->invalidReplacement, '[]().?-*^$/:<>');
87
        }
88
89
        return $this->escapedInvalidReplacement;
90
    }
91
92
    /**
93
     * Prepare string for URL.
94
     *
95
     * @param  string $value
96
     * @return string
97
     */
98
    protected function prepareForUrl($value)
99
    {
100
        $escapedInvalidReplacement = $this->getEscapedInvalidReplacement();
101
102
        // First, replace all invalid characters with replacement string.
103
        $value =
104
            preg_replace(
105
                $this->invalidRegexp,
106
                $this->invalidReplacement,
107
                $value
108
            );
109
        // Second, replace all possible repeats of replacement string with single one.
110
        $value =
111
            preg_replace(
112
                '/('.$escapedInvalidReplacement.'){2,}/',
113
                $this->invalidReplacement,
114
                $value
115
            );
116
117
        if ($this->trimInvalid) {
118
            $value =
119
                preg_replace(
120
                    '/(^'.$escapedInvalidReplacement.'|'.$escapedInvalidReplacement.'$)/',
121
                    '',
122
                    $value
123
                );
124
        }
125
126
        return $value;
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function validateAttribute($model, $attribute)
133
    {
134
        // Do not overwrite non-empty values and do not parse empty values.
135
        if ($model->$attribute || !$model->{$this->sourceAttribute}) {
136
            return;
137
        }
138
139
        $value = URLify::downcode($model->{$this->sourceAttribute});
140
141
        if ($this->lowercase) {
142
            $value = strtolower($value);
143
        }
144
145
        if ($this->forUrl) {
146
            $value = $this->prepareForUrl($value);
147
        }
148
149
        $model->$attribute = $value;
150
    }
151
}
152