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

SplitToArrayBehavior::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
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