MultiFilter::validateAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 4
c 2
b 1
f 1
nc 3
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace betadog\yii1;
4
5
/**
6
 * Usage:
7
 *
8
 * ['name', 'betadog\\yii1\\MultiFilter', 'filters' => ['trim', 'strtolower',]],
9
 *
10
 */
11
class MultiFilter extends \CValidator
12
{
13
    /** @var array */
14
    protected $filters;
15
16
    public function setFilters($value)
17
    {
18
        $this->filters = (array)$value;
19
    }
20
21
    protected function validateAttribute($object, $attribute)
22
    {
23
        foreach ($this->filters as $filter) {
24
            if (!is_callable($filter)) {
25
                throw new \CException(\Yii::t('yii', 'The "filter" property must be specified with a valid callback.'));
26
            }
27
28
            $object->$attribute = call_user_func_array($filter, [$object->$attribute]);
29
        }
30
    }
31
}