PurifyBehavior::beforeValidate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace coderius\comments\components\behaviors;
4
5
/*
6
 * @package myblog
7
 * @file PurifyBehavior.php created 03.05.2018 10:17:46
8
 *
9
 * @copyright Copyright (C) 2018 Sergio coderius <coderius>
10
 * @license This program is free software: GNU General Public License
11
 */
12
13
use yii\base\Behavior;
14
use yii\db\ActiveRecord;
15
use yii\helpers\HtmlPurifier;
16
17
/**
18
 * Class PurifyBehavior.
19
 */
20
class PurifyBehavior extends Behavior
21
{
22
    /**
23
     * @var array attributes
24
     */
25
    public $attributes = [];
26
    /**
27
     * @var null The config to use for HtmlPurifier
28
     */
29
    public $config = null;
30
31
    /**
32
     * Declares event handlers for the [[owner]]'s events.
33
     *
34
     * Child classes may override this method to declare what PHP callbacks should
35
     * be attached to the events of the [[owner]] component.
36
     *
37
     * The callbacks will be attached to the [[owner]]'s events when the behavior is
38
     * attached to the owner; and they will be detached from the events when
39
     * the behavior is detached from the component.
40
     **/
41
    public function events()
42
    {
43
        return [
44
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
45
        ];
46
    }
47
48
    /**
49
     * Before validate event.
50
     */
51
    public function beforeValidate()
52
    {
53
        foreach ($this->attributes as $attribute) {
54
            $this->owner->$attribute = HtmlPurifier::process($this->owner->$attribute, $this->config);
55
        }
56
    }
57
}
58