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

Repeater   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 45
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getJson() 0 4 2
A resolveJson() 0 13 3
A __get() 0 3 1
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