Accessors::__get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace N7olkachev\LaravelAccessors;
4
5
trait Accessors
6
{
7
    public function __get($key)
8
    {
9
        return $this->callMutator('get', $key);
10
    }
11
12
    public function __set($key, $value)
13
    {
14
        return $this->callMutator('set', $key, $value);
15
    }
16
17
    protected function callMutator($type, $key, $value = null)
18
    {
19
        if (method_exists($this, $method = $this->getAccessorName($type, $key))) {
20
            return $this->$method($value);
21
        } else {
22
            throw new \Exception("Undefined property: $key");
23
        }
24
    }
25
26
    protected function getAccessorName($type, $key)
27
    {
28
        $key = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
29
30
        return $type . $key . 'Attribute';
31
    }
32
}