1 | <?php |
||
2 | /** |
||
3 | * Retour plugin for Craft CMS 3.x |
||
4 | * |
||
5 | * Retour allows you to intelligently redirect legacy URLs, so that you don't |
||
6 | * lose SEO value when rebuilding & restructuring a website |
||
7 | * |
||
8 | * @link https://nystudio107.com/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
9 | * @copyright Copyright (c) 2018 nystudio107 |
||
0 ignored issues
–
show
|
|||
10 | */ |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | namespace nystudio107\retour\validators; |
||
13 | |||
14 | use nystudio107\retour\helpers\Text as TextHelper; |
||
15 | |||
16 | use craft\helpers\StringHelper; |
||
17 | |||
18 | use yii\base\InvalidConfigException; |
||
19 | use yii\validators\Validator; |
||
20 | |||
21 | /** |
||
0 ignored issues
–
show
|
|||
22 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
23 | * @package Retour |
||
0 ignored issues
–
show
|
|||
24 | * @since 3.0.0 |
||
0 ignored issues
–
show
|
|||
25 | */ |
||
0 ignored issues
–
show
|
|||
26 | class DbStringValidator extends Validator |
||
27 | { |
||
28 | /** |
||
0 ignored issues
–
show
|
|||
29 | * @var null|int |
||
30 | */ |
||
31 | public $max = null; |
||
32 | |||
33 | |||
34 | /** |
||
0 ignored issues
–
show
|
|||
35 | * @inheritdoc |
||
36 | * @throws InvalidConfigException |
||
0 ignored issues
–
show
|
|||
37 | */ |
||
0 ignored issues
–
show
|
|||
38 | public function init() |
||
39 | { |
||
40 | parent::init(); |
||
41 | if ($this->max === null) { |
||
42 | throw new InvalidConfigException('The "max" property must be set.'); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
0 ignored issues
–
show
|
|||
47 | * @inheritdoc |
||
48 | */ |
||
0 ignored issues
–
show
|
|||
49 | public function validateAttribute($model, $attribute) |
||
50 | { |
||
51 | $value = $model->$attribute; |
||
52 | $value = TextHelper::cleanupText($value); |
||
53 | $value = StringHelper::encodeMb4($value); |
||
54 | $value = TextHelper::truncate($value, $this->max); |
||
55 | $model->$attribute = $value; |
||
56 | } |
||
57 | } |
||
58 |