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

FormAccessible::getAttributeFromArray()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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
     * Get form value from the eloquent model.
31
     *
32
     * @param string $key
33
     *
34
     * @return mixed
35
     */
36 8
    public function getFormValue($key)
37
    {
38 8
        $value = $this->getAttributeFromArray($key);
39
40 8
        if (in_array($key, $this->getDates()) && ! is_null($value)) {
41 8
            $value = $this->asDateTime($value);
42 6
        }
43
44 8
        if ($this->hasFormMutator($key)) {
45 8
            $value = $this->mutateFormAttribute($key, $value);
46 6
        }
47
48 8
        return $value;
49
    }
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Eloquent Functions
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Get an attribute from the $attributes array.
57
     *
58
     * @param  string  $key
59
     *
60
     * @return mixed
61
     */
62
    abstract protected function getAttributeFromArray($key);
63
64
    /**
65
     * Get the attributes that should be converted to dates.
66
     *
67
     * @return array
68
     */
69
    abstract public function getDates();
70
71
    /**
72
     * Return a timestamp as DateTime object.
73
     *
74
     * @param  mixed  $value
75
     *
76
     * @return \Carbon\Carbon
77
     */
78
    abstract protected function asDateTime($value);
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Other Functions
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Check if has a form mutator.
86
     *
87
     * @param  string  $key
88
     *
89
     * @return bool
90
     */
91 8
    protected function hasFormMutator($key)
92
    {
93 8
        $methods = $this->getReflection()->getMethods(
94 2
            ReflectionMethod::IS_PUBLIC
95 6
        );
96
97 8
        $mutator = collect($methods)
98 8
            ->first(function ($index, ReflectionMethod $method) use ($key) {
99 8
                return $method->name === $this->getMutateFromMethodName($key);
100 8
            });
101
102 8
        return (bool) $mutator;
103
    }
104
105
    /**
106
     * Mutate the form attribute.
107
     *
108
     * @param  string  $key
109
     * @param  mixed   $value
110
     *
111
     * @return mixed
112
     */
113 8
    private function mutateFormAttribute($key, $value)
114
    {
115 8
        return $this->{$this->getMutateFromMethodName($key)}($value);
116
    }
117
118
    /**
119
     * Get the mutate form method name.
120
     *
121
     * @param  string  $key
122
     *
123
     * @return string
124
     */
125 8
    private function getMutateFromMethodName($key)
126
    {
127 8
        return 'form' . str_studly($key) . 'Attribute';
128
    }
129
130
    /**
131
     * Get a ReflectionClass Instance.
132
     *
133
     * @return ReflectionClass
134
     */
135 8
    protected function getReflection()
136
    {
137 8
        if ( ! $this->reflection) {
138 8
            $this->reflection = new ReflectionClass($this);
139 6
        }
140
141 8
        return $this->reflection;
142
    }
143
}
144