Passed
Push — master ( 54aa16...02143c )
by Karel
16:12
created

Repeater::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Chuckbe\Chuckcms\Models;
4
5
use Eloquent;
6
7
/**
8
 * @property string $slug
9
 * @property string $url
10
 * @property string $page
11
 * @property array $json
12
 */
13
class Repeater extends Eloquent
14
{
15
    /**
16
     * The attributes that are mass assignable.
17
     *
18
     * @var array
19
     */
20
    protected $fillable = [
21
        'slug', 'url', 'page', 'json'
22
    ];
23
24
    protected $casts = [
25
        'json' => 'array',
26
    ];
27
28
    public function getJson(string $string)
29
    {
30
        $json = $this->resolveJson($string);
31
        return $json ? $json : null;
32
    }
33
34
    private function resolveJson($var)
35
    {
36
        $json = $this->json;
37
        $split = explode('.', $var);
38
        foreach ($split as $value) {
39
            if (array_key_exists($value, $json)) {
40
                $json = $json[$value];
41
            } else {
42
                return null;
43
            }
44
        }
45
46
        return $json;
47
    }
48
49
    /**
50
     * Dynamically retrieve attributes on the model.
51
     *
52
     * @param  string  $key
53
     * @return mixed
54
     */
55
    public function __get($key)
56
    {
57
        return $this->getAttribute($key) ?? $this->getJson($key);
58
    }
59
}
60