Passed
Pull Request — master (#7)
by Alexander
02:25
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 1
    public function events()
21
    {
22
        return [
23 1
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'map',
24 1
            ActiveRecord::EVENT_BEFORE_INSERT => 'map',
25 1
            ActiveRecord::EVENT_BEFORE_UPDATE => 'map',
26
        ];
27
    }
28
29
    /**
30
     * @return void
31
     */
32 1
    public function map()
33
    {
34 1
        foreach ((array)$this->attributes as $attribute) {
35 1
            $this->owner->{$attribute} = $this->format($this->owner->{$attribute});
36
        }
37 1
    }
38
39
    /**
40
     * @param $value
41
     * @return mixed
42
     */
43 1
    private function format($value)
44
    {
45 1
        return preg_replace("/\D/", '', $value);
46
    }
47
}