OldAndAttribute   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 76
rs 10
ccs 30
cts 33
cp 0.9091
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttribute() 0 12 4
A hasOld() 0 3 1
A oldOrDefault() 0 16 4
A old() 0 10 3
A oldOrAttribute() 0 16 4
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields\Traits;
4
5
trait OldAndAttribute
6
{
7 87
    public function old($name = null)
8
    {
9 87
        if (is_null($name)) {
10 19
            $name = $this->name();
11 19
            if ($this->belongsToArray()) {
12 3
                $name = $this->getDotPatternName();
13
            }
14
        }
15
16 87
        return old($name);
17
    }
18
19 70
    public function hasOld($name = null)
20
    {
21 70
        return !is_null($this->old($name));
22
    }
23
24 17
    public function oldOrDefault($locale = null)
25
    {
26 17
        $name = $this->name();
27 17
        if ($locale) {
28
            $name .= '.'. $locale;
29
        }
30
31 17
        if ($this->belongsToArray()) {
32 1
            $name = $this->getDotPatternName();
33
        }
34
35 17
        if ($this->hasOld($name)) {
36 17
            return $this->old($name);
37
        }
38
39 17
        return $this->getDefault();
40
    }
41
42 19
    public function oldOrAttribute($model, $locale = null)
43
    {
44 19
        $name = $this->name();
45 19
        if ($locale) {
46
            $name .= '.'. $locale;
47
        }
48
49 19
        if ($this->belongsToArray()) {
50 2
            $name = $this->getDotPatternName();
51
        }
52
53 19
        if ($this->hasOld($name)) {
54 17
            return $this->old($name);
55
        }
56
57 2
        return $this->getAttribute($model, $locale);
58
    }
59
60 35
    public function getAttribute($model, $locale = null)
61
    {
62 35
        if ($locale && $this->isTranslatable()) {
63
            return $model->getTranslation($this->name(), $locale, false);
64
        }
65
66 35
        if ($this->belongsToArray()) {
67 2
            $values = $model->{$this->getAncestorName()};
68 2
            return $values[$this->getDescendantName()] ?? null;
69
        }
70
71 33
        return $model->{$this->name()};
72
    }
73
74
    abstract public function getDefault();
75
    abstract public function name(string $name = null);
76
    abstract public function isTranslatable();
77
    abstract public function belongsToArray(): bool;
78
    abstract public function getAncestorName(): string;
79
    abstract public function getDescendantName(): string;
80
    abstract public function getDotPatternName(): string;
81
}
82