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

NumberBehavior   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 8 1
A map() 0 6 2
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
}