Passed
Pull Request — master (#6)
by Alexander
04:02
created

FlexibleTimestampBehavior   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 29
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 6 1
A map() 0 10 3
1
<?php
2
3
namespace Horat1us\Yii\Behaviors;
4
5
use Carbon\Carbon;
6
use yii\base\Behavior;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * Class FlexibleTimestampBehavior
11
 * @package Horat1us\Yii\Behaviors
12
 */
13
class FlexibleTimestampBehavior extends Behavior
14
{
15
    /** @var  string[] */
16
    public $attributes;
17
18
    /**
19
     * @return array
20
     */
21 1
    public function events()
22
    {
23
        return [
24 1
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'map',
25
        ];
26
    }
27
28
    /**
29
     * @return void
30
     */
31 1
    public function map()
32
    {
33 1
        foreach ($this->attributes as $attribute) {
34 1
            if (!is_numeric($this->owner->{$attribute})) {
35 1
                continue;
36
            }
37
38 1
            $this->owner->{$attribute} = Carbon::createFromTimestamp($this->owner->{$attribute})->toDateString();
39
        }
40
    }
41
}