1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence; |
4
|
|
|
|
5
|
|
|
use Sofa\Eloquence\Mutable\Hooks; |
6
|
|
|
use Sofa\Eloquence\Mutator\Mutator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property array $setterMutators |
10
|
|
|
* @property array $getterMutators |
11
|
|
|
*/ |
12
|
|
|
trait Mutable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Register hooks for the trait. |
16
|
|
|
* |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public static function bootMutable() |
22
|
|
|
{ |
23
|
|
|
$hooks = new Hooks; |
24
|
|
|
|
25
|
|
|
foreach (['setAttribute', 'getAttribute', 'toArray'] as $method) { |
26
|
|
|
static::hook($method, $hooks->{$method}()); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Mutate mutable attributes for array conversion. |
32
|
|
|
* |
33
|
|
|
* @param array $attributes |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
protected function mutableAttributesToArray(array $attributes) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
foreach ($attributes as $key => $value) { |
39
|
|
|
if ($this->hasGetterMutator($key)) { |
40
|
|
|
$attributes[$key] = $this->mutableMutate($key, $value, 'getter'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $attributes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Determine whether an attribute has getter mutators defined. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @return boolean |
52
|
|
|
*/ |
53
|
|
|
public function hasGetterMutator($key) |
54
|
|
|
{ |
55
|
|
|
return array_key_exists($key, $this->getMutators('getter')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Determine whether an attribute has setter mutators defined. |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
|
|
public function hasSetterMutator($key) |
65
|
|
|
{ |
66
|
|
|
return array_key_exists($key, $this->getMutators('setter')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Mutate the attribute. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @param string $value |
74
|
|
|
* @param string $dir |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function mutableMutate($key, $value, $dir) |
78
|
|
|
{ |
79
|
|
|
$mutators = $this->getMutatorsForAttribute($key, $dir); |
80
|
|
|
|
81
|
|
|
return static::$attributeMutator->mutate($value, $mutators); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the mutators for an attribute. |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function getMutatorsForAttribute($key, $dir) |
91
|
|
|
{ |
92
|
|
|
return $this->getMutators($dir)[$key]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the array of attribute mutators. |
97
|
|
|
* |
98
|
|
|
* @param string $dir |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function getMutators($dir) |
102
|
|
|
{ |
103
|
|
|
$property = ($dir === 'setter') ? 'setterMutators' : 'getterMutators'; |
104
|
|
|
|
105
|
|
|
return (property_exists($this, $property)) ? $this->{$property} : []; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.