Repeater::__get()   A
last analyzed

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
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
/**
9
 * @property string $slug
10
 * @property string $url
11
 * @property string $page
12
 * @property array  $json
13
 */
14
class Repeater extends Eloquent
15
{
16
    use SoftDeletes;
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = [
24
        'slug', 'url', 'page', 'json',
25
    ];
26
27
    protected $casts = [
28
        'json' => 'array',
29
    ];
30
31
    public function getJson(string $string)
32
    {
33
        $json = $this->resolveJson($string);
34
35
        return !is_null($json) ? $json : null;
36
    }
37
38
    private function resolveJson($var)
39
    {
40
        $json = $this->json;
41
        $split = explode('.', $var);
42
        foreach ($split as $value) {
43
            if (array_key_exists($value, $json)) {
44
                $json = $json[$value];
45
            } else {
46
                return null;
47
            }
48
        }
49
50
        return $json;
51
    }
52
53
    /**
54
     * Dynamically retrieve attributes on the model.
55
     *
56
     * @param string $key
57
     *
58
     * @return mixed
59
     */
60
    public function __get($key)
61
    {
62
        return $this->getAttribute($key) ?? $this->getJson($key);
63
    }
64
}
65