1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence; |
4
|
|
|
|
5
|
|
|
use Sofa\Eloquence\Mutable\Hooks; |
6
|
|
|
use Sofa\Eloquence\Mutator\Mutator; |
7
|
|
|
use Sofa\Eloquence\Contracts\Mutator as MutatorContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property array $setterMutators |
11
|
|
|
* @property array $getterMutators |
12
|
|
|
*/ |
13
|
|
|
trait Mutable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Attribute mutator instance. |
17
|
|
|
* |
18
|
|
|
* @var \Sofa\Eloquence\Contracts\Mutator |
19
|
|
|
*/ |
20
|
|
|
protected static $attributeMutator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Register hooks for the trait. |
24
|
|
|
* |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public static function bootMutable() |
30
|
|
|
{ |
31
|
|
|
if (!isset(static::$attributeMutator)) { |
32
|
|
|
if (function_exists('app') && app()->bound('eloquence.mutator')) { |
33
|
|
|
static::setAttributeMutator(app('eloquence.mutator')); |
34
|
|
|
} else { |
35
|
|
|
static::setAttributeMutator(new Mutator); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$hooks = new Hooks; |
40
|
|
|
|
41
|
|
|
foreach (['setAttribute', 'getAttribute', 'toArray'] as $method) { |
42
|
|
|
static::hook($method, $hooks->{$method}()); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Mutate mutable attributes for array conversion. |
48
|
|
|
* |
49
|
|
|
* @param array $attributes |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
protected function mutableAttributesToArray(array $attributes) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
foreach ($attributes as $key => $value) { |
55
|
|
|
if ($this->hasGetterMutator($key)) { |
56
|
|
|
$attributes[$key] = $this->mutableMutate($key, $value, 'getter'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $attributes; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine whether an attribute has getter mutators defined. |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public function hasGetterMutator($key) |
70
|
|
|
{ |
71
|
|
|
return array_key_exists($key, $this->getMutators('getter')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Determine whether an attribute has setter mutators defined. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function hasSetterMutator($key) |
81
|
|
|
{ |
82
|
|
|
return array_key_exists($key, $this->getMutators('setter')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Mutate the attribute. |
87
|
|
|
* |
88
|
|
|
* @param string $key |
89
|
|
|
* @param string $value |
90
|
|
|
* @param string $dir |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
protected function mutableMutate($key, $value, $dir) |
94
|
|
|
{ |
95
|
|
|
$mutators = $this->getMutatorsForAttribute($key, $dir); |
96
|
|
|
|
97
|
|
|
return static::$attributeMutator->mutate($value, $mutators); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the mutators for an attribute. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function getMutatorsForAttribute($key, $dir) |
107
|
|
|
{ |
108
|
|
|
return $this->getMutators($dir)[$key]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the array of attribute mutators. |
113
|
|
|
* |
114
|
|
|
* @param string $dir |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getMutators($dir) |
118
|
|
|
{ |
119
|
|
|
$property = ($dir === 'setter') ? 'setterMutators' : 'getterMutators'; |
120
|
|
|
|
121
|
|
|
return (property_exists($this, $property)) ? $this->{$property} : []; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set attribute mutator instance. |
126
|
|
|
* |
127
|
|
|
* @codeCoverageIgnore |
128
|
|
|
* |
129
|
|
|
* @param \Sofa\Eloquence\Contracts\Mutator $mutator |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public static function setAttributeMutator(MutatorContract $mutator) |
133
|
|
|
{ |
134
|
|
|
static::$attributeMutator = $mutator; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get attribute mutator instance. |
139
|
|
|
* |
140
|
|
|
* @codeCoverageIgnore |
141
|
|
|
* |
142
|
|
|
* @return \Sofa\Eloquence\Contracts\Mutator |
143
|
|
|
*/ |
144
|
|
|
public static function getAttributeMutator() |
145
|
|
|
{ |
146
|
|
|
return static::$attributeMutator; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.