JsonBehavior::__get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SamIT\Yii2\Behaviors;
4
5
use SamIT\Yii2\Components\Map;
6
use yii\base\Behavior;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * JsonBehavior
11
 * Automatically converts to / from json before saving / after reading.
12
 *
13
 */
14
class JsonBehavior extends Behavior
15
{
16
    public $jsonAttributes = [];
17
    public $defaultAttribute;
18
19 5
    public function events()
20
    {
21
        return [
22 5
            ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',
23 5
            ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave',
24 5
            ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
25
        ];
26
    }
27
28
    /**
29
     *
30
     */
31 5
    protected function createMaps()
32
    {
33 5
        foreach($this->jsonAttributes as $attribute) {
34 5
            if(!$this->owner->$attribute instanceof Map) {
35 5
                $this->owner->$attribute = new Map($this->owner->$attribute);
36
            }
37
        }
38
39 5
    }
40
41 5
    public function attach($owner): void
42
    {
43 5
        parent::attach($owner);
44 5
        $this->createMaps();
45 5
    }
46
47 2
    public function afterFind(): void
48
    {
49 2
        $this->createMaps();
50 2
    }
51
52 1
    public function beforeSave(): bool
53
    {
54 1
        $this->createMaps();
55 1
        return true;
56
    }
57
58 4
    public function canGetProperty($name, $checkVars = true)
59
    {
60
        // Only do this if we have exactly 1 json attribute.
61 4
        return isset($this->defaultAttribute)
62 4
            || parent::canGetProperty($name, $checkVars);
63
    }
64
65 1
    public function canSetProperty($name, $checkVars = true)
66
    {
67 1
        return isset($this->defaultAttribute)
68 1
            || parent::canSetProperty($name, $checkVars);
69
    }
70
71 1
    public function __set($name, $value)
72
    {
73 1
        if (parent::canSetProperty($name, false)) {
74
            parent::__set($name, $value);
75 1
        } elseif (isset($this->defaultAttribute)) {
76 1
            $this->owner->{$this->defaultAttribute}[$name] = $value;
77
        } else {
78
            // Throw standard error:
79
            parent::__set($name, $value);
80
        }
81 1
    }
82
83 4
    public function __get($name)
84
    {
85 4
        if (isset($this->defaultAttribute)) {
86 4
            if (isset($this->owner->{$this->defaultAttribute}[$name])) {
87 3
                return $this->owner->{$this->defaultAttribute}[$name];
88
            }
89 1
            return null;
90
        }
91
92
        return parent::__get($name);
93
    }
94
95
    /**
96
     * Initializes the object.
97
     * This method is invoked at the end of the constructor after the object is initialized with the
98
     * given configuration.
99
     */
100 5
    public function init()
101
    {
102 5
        parent::init();
103 5
        if(!isset($this->defaultAttribute) && count($this->jsonAttributes) == 1) {
104 5
            $this->defaultAttribute = $this->jsonAttributes[0];
105
        }
106 5
    }
107
108
109
}