Completed
Push — devel ( 480836...129972 )
by Philippe
02:31
created

SplitToArrayBehavior::updateAttribute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 6
nc 4
nop 0
crap 4
1
<?php
2
/**
3
 * SplitToArrayBehavior.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author pgaultier
8
 * @copyright 2010-2016 Ibitux
9
 * @license http://www.ibitux.com/license license
10
 * @version XXX
11
 * @link http://www.ibitux.com
12
 * @package sweelix\oauth2\server\behaviors
13
 */
14
15
namespace sweelix\oauth2\server\behaviors;
16
17
use sweelix\oauth2\server\models\BaseModel;
18
use yii\base\Behavior;
19
20
/**
21
 * This behavior change one attribute to array by splitting the string.
22
 *
23
 * @author Philippe Gaultier <[email protected]>
24
 * @copyright 2010-2016 Philippe Gaultier
25
 * @license http://www.sweelix.net/license license
26
 * @version XXX
27
 * @link http://www.sweelix.net
28
 * @package sweelix\oauth2\server\behaviors
29
 * @since XXX
30
 */
31
class SplitToArrayBehavior extends Behavior
32
{
33
    /**
34
     * @var array list of attributes to update
35
     */
36
    public $attributes = [];
37
38
    /**
39
     * @var string separator used to split the string
40
     */
41
    public $separator = ' ';
42
43
    /**
44
     * @inheritdoc
45
     */
46 21
    public function events()
47
    {
48
        return [
49 21
            BaseModel::EVENT_BEFORE_UPDATE => 'updateAttribute',
50 21
            BaseModel::EVENT_BEFORE_INSERT => 'updateAttribute',
51 21
        ];
52
    }
53
54
    /**
55
     * @since XXX
56
     */
57 17
    public function updateAttribute()
58
    {
59 17
        foreach($this->attributes as $attribute) {
60 17
            if (empty($this->owner->{$attribute}) === true) {
61 14
                $this->owner->{$attribute} = [];
62 17
            } elseif (is_array($this->owner->{$attribute}) === false) {
63 3
                $this->owner->{$attribute} = explode($this->separator, $this->owner->{$attribute});
64 3
            }
65 17
        }
66 17
    }
67
68
}
69