Passed
Push — master ( f43c0a...452f90 )
by Alexander
34s
created

NumberBehavior::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Horat1us\Yii\Behaviors;
4
5
use yii\base\Behavior;
6
use yii\db\ActiveRecord;
7
8
/**
9
 * Class PhoneBehavior
10
 * @package Horat1us\Yii\Behaviors
11
 */
12
class NumberBehavior extends Behavior
13
{
14
    /** @var string|string[] */
15
    public $attributes = [];
16
17
    /**
18
     * @return array
19
     */
20
    public function events()
21
    {
22
        return [
23
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'map',
24
            ActiveRecord::EVENT_BEFORE_INSERT => 'map',
25
            ActiveRecord::EVENT_BEFORE_UPDATE => 'map',
26
        ];
27
    }
28
29
    /**
30
     * @return void
31
     */
32
    public function map()
33
    {
34
        foreach ((array)$this->attributes as $attribute) {
35
            $this->owner->{$attribute} = preg_replace("/\D/", '', $this->owner->{$attribute});
36
        }
37
    }
38
}