Completed
Push — master ( 077fc4...b28347 )
by Martin
02:45
created

HasSerializableValuesTrait::isFillable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Luminark\SerializableValues\Traits;
4
5
trait HasSerializableValuesTrait
6
{
7
    protected function getSerializableAttributes()
8
    {
9
        return [];
10
    }
11
12
    public function getValuesAttribute($values)
13
    {
14
        return $this->unserialize($values);
15
    }
16
17
    public function setValuesAttribute(array $values)
18
    {
19
        $values = array_merge($this->values, $values);
0 ignored issues
show
Bug introduced by
The property values does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $values = array_only($values, $this->getSerializableAttributes());
21
        $this->attributes['values'] = $this->serialize($values);
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    public function getOriginal($key = null, $default = null)
25
    {
26
        $originalValues = $this->unserialize(parent::getOriginal('values'));
27
        if (array_key_exists($key, $originalValues)) {
28
            return array_get($originalValues, $key, $default);
29
        }
30
31
        return array_get($this->original, $key, $default);
0 ignored issues
show
Bug introduced by
The property original does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
    
34
    public function isFillable($key)
35
    {
36
        return in_array($key, $this->getSerializableAttributes())
37
            ? true
38
            : parent::isFillable($key);
39
    }
40
41
    public function getAttribute($key)
42
    {
43
        return in_array($key, $this->getSerializableAttributes())
44
            ? $this->getValue($key)
45
            : parent::getAttribute($key);
46
    }
47
    
48
    public function setAttribute($key, $value)
49
    {
50
        in_array($key, $this->getSerializableAttributes())
51
            ? $this->setValue($key, $value)
52
            : parent::setAttribute($key, $value);
53
    }
54
55
    public function getValue($key)
56
    {
57
        return array_get($this->getAttribute('values'), $key);
58
    }
59
60
    public function setValue($key, $value)
61
    {
62
        $values = $this->values;
63
        $values[$key] = $value;
64
        $this->setValuesAttribute($values);
65
    }
66
67
    protected function serialize($array)
68
    {
69
        return base64_encode(serialize($array ?: []));
70
    }
71
72
    protected function unserialize($string)
73
    {
74
        return $string ? unserialize(base64_decode($string)) : [];
75
    }
76
}
77