Completed
Pull Request — master (#5)
by ARCANEDEV
04:54
created

FormAccessible   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 122
ccs 24
cts 25
cp 0.96
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormValue() 0 19 4
getAttributeFromArray() 0 1 ?
getDates() 0 1 ?
asDateTime() 0 1 ?
A hasFormMutator() 0 12 1
A mutateFormAttribute() 0 4 1
A getReflection() 0 8 2
1
<?php namespace Arcanedev\LaravelHtml\Traits;
2
3
use ReflectionClass;
4
use ReflectionMethod;
5
6
/**
7
 * Class     FormAccessible
8
 *
9
 * @package  Arcanedev\LaravelHtml\Traits
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait FormAccessible
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * A cached ReflectionClass instance for $this
20
     *
21
     * @var ReflectionClass
22
     */
23
    protected $reflection;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * @param string $key
31
     *
32
     * @return mixed
33
     */
34 8
    public function getFormValue($key)
35
    {
36 8
        $value = $this->getAttributeFromArray($key);
37
38
        // If the attribute is listed as a date, we will convert it to a DateTime
39
        // instance on retrieval, which makes it quite convenient to work with
40
        // date fields without having to create a mutator for each property.
41 8
        if (in_array($key, $this->getDates())) {
42 8
            if ( ! is_null($value)) {
43 8
                $value = $this->asDateTime($value);
44 6
            }
45 6
        }
46
47 8
        if ($this->hasFormMutator($key)) {
48 8
            return $this->mutateFormAttribute($key, $value);
49
        }
50
51
        return $value;
52
    }
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Eloquent Functions
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Get an attribute from the $attributes array.
60
     *
61
     * @param  string  $key
62
     *
63
     * @return mixed
64
     */
65
    abstract protected function getAttributeFromArray($key);
66
67
    /**
68
     * Get the attributes that should be converted to dates.
69
     *
70
     * @return array
71
     */
72
    abstract public function getDates();
73
74
    /**
75
     * Return a timestamp as DateTime object.
76
     *
77
     * @param  mixed  $value
78
     *
79
     * @return \Carbon\Carbon
80
     */
81
    abstract protected function asDateTime($value);
82
83
    /* ------------------------------------------------------------------------------------------------
84
     |  Other Functions
85
     | ------------------------------------------------------------------------------------------------
86
     */
87
    /**
88
     * Check if has a form mutator.
89
     *
90
     * @param  string  $key
91
     *
92
     * @return bool
93
     */
94 8
    protected function hasFormMutator($key)
95
    {
96 8
        $methods = $this->getReflection()
97 8
            ->getMethods(ReflectionMethod::IS_PUBLIC);
98
99 8
        $mutator = collect($methods)
100 8
            ->first(function ($index, ReflectionMethod $method) use ($key) {
101 8
                return $method->name === 'form' . str_studly($key) . 'Attribute';
102 8
            });
103
104 8
        return (bool) $mutator;
105
    }
106
107
    /**
108
     * Mutate the form attribute.
109
     *
110
     * @param  string  $key
111
     * @param  mixed   $value
112
     *
113
     * @return mixed
114
     */
115 8
    private function mutateFormAttribute($key, $value)
116
    {
117 8
        return $this->{'form' . str_studly($key) . 'Attribute'}($value);
118
    }
119
120
    /**
121
     * Get a ReflectionClass Instance
122
     *
123
     * @return ReflectionClass
124
     */
125 8
    protected function getReflection()
126
    {
127 8
        if ( ! $this->reflection) {
128 8
            $this->reflection = new ReflectionClass($this);
129 6
        }
130
131 8
        return $this->reflection;
132
    }
133
}
134