MutatesAttributes::getMutatedAttribute()   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
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Support\Concerns;
5
6
use Illuminate\Support\Str;
7
8
9
/**
10
 * Trait MutatesAttributes
11
 *
12
 * @package SmartWeb\ModuleTesting\Support\Concerns
13
 */
14
trait MutatesAttributes
15
{
16
    
17
    /**
18
     * @var array
19
     */
20
    protected static $mutators = [];
21
    
22
    /**
23
     * @param string $attribute
24
     *
25
     * @return bool
26
     */
27
    protected function hasGetMutator(string $attribute) : bool
28
    {
29
        return method_exists($this, $this->attributeGetMutator($attribute));
30
    }
31
    
32
    /**
33
     * @param string $attribute
34
     *
35
     * @return mixed
36
     */
37
    protected function getMutatedAttribute(string $attribute)
38
    {
39
        return $this->{$this->attributeGetMutator($attribute)}($attribute);
40
    }
41
    
42
    /**
43
     * @param string $attribute
44
     *
45
     * @return string
46
     */
47
    private function attributeGetMutator(string $attribute) : string
48
    {
49
        return static::$mutators['get'][static::class][$attribute] = static::$mutators['get'][static::class][$attribute]
50
                                                                     ?? $this->resolveGetMutator($attribute);
51
    }
52
    
53
    /**
54
     * @param string $attribute
55
     *
56
     * @return string
57
     */
58
    private function resolveGetMutator(string $attribute) : string
59
    {
60
        return 'get' . Str::studly($attribute) . 'Attribute';
61
    }
62
}
63