12
12
trait EncryptableDbAttribute
13
13
{
14
14
/**
15
- * @param string $key
15
+ * Get a plain attribute (not a relationship).
16
16
*
17
+ * @param string $key
17
18
* @return mixed
18
19
*/
19
- public function getAttribute ($ key )
20
+ public function getAttributeValue ($ key )
20
21
{
21
- $ value = parent ::getAttribute ($ key );
22
+ $ value = $ this ->getAttributeFromArray ($ key );
23
+
24
+ // If the attribute has a get mutator, we will call that then return what
25
+ // it returns as the value, which is useful for transforming values on
26
+ // retrieval from the model to a form that is more useful for usage.
27
+ if ($ this ->hasGetMutator ($ key )) {
28
+ return $ this ->mutateAttribute ($ key , $ value );
29
+ }
30
+
31
+ // decrypt value before casts
32
+ if (in_array ($ key , $ this ->encryptable )) {
33
+ $ value = $ this ->decrypt ($ value );
34
+ }
35
+
36
+ // If the attribute exists within the cast array, we will convert it to
37
+ // an appropriate native PHP type dependant upon the associated value
38
+ // given with the key in the pair. Dayle made this comment line up.
39
+ if ($ this ->hasCast ($ key )) {
40
+ return $ this ->castAttribute ($ key , $ value );
41
+ }
22
42
23
- if (!in_array ($ key , $ this ->encryptable ) || is_null ($ value ) || $ value === '' ) {
24
- return $ value ;
43
+ // If the attribute is listed as a date, we will convert it to a DateTime
44
+ // instance on retrieval, which makes it quite convenient to work with
45
+ // date fields without having to create a mutator for each property.
46
+ if (in_array ($ key , $ this ->getDates ()) &&
47
+ ! is_null ($ value )) {
48
+ return $ this ->asDateTime ($ value );
25
49
}
26
50
27
- return $ this -> decrypt ( $ value) ;
51
+ return $ value ;
28
52
}
29
53
30
54
/**
@@ -49,12 +73,43 @@ public function setAttribute($key, $value)
49
73
*/
50
74
public function attributesToArray (): array
51
75
{
52
- $ attributes = parent ::attributesToArray ();
53
-
54
- if (empty ($ attributes )) {
55
- return $ attributes ;
76
+ // If an attribute is a date, we will cast it to a string after converting it
77
+ // to a DateTime / Carbon instance. This is so we will get some consistent
78
+ // formatting while accessing attributes vs. arraying / JSONing a model.
79
+ $ attributes = $ this ->addDateAttributesToArray (
80
+ $ attributes = $ this ->getArrayableAttributes ()
81
+ );
82
+
83
+ $ attributes = $ this ->addMutatedAttributesToArray (
84
+ $ attributes , $ mutatedAttributes = $ this ->getMutatedAttributes ()
85
+ );
86
+
87
+ // decrypt attributes before casts
88
+ $ attributes = $ this ->decryptAttributes ($ attributes );
89
+
90
+ // Next we will handle any casts that have been setup for this model and cast
91
+ // the values to their appropriate type. If the attribute has a mutator we
92
+ // will not perform the cast on those attributes to avoid any confusion.
93
+ $ attributes = $ this ->addCastAttributesToArray (
94
+ $ attributes , $ mutatedAttributes
95
+ );
96
+
97
+ // Here we will grab all of the appended, calculated attributes to this model
98
+ // as these attributes are not really in the attributes array, but are run
99
+ // when we need to array or JSON the model for convenience to the coder.
100
+ foreach ($ this ->getArrayableAppends () as $ key ) {
101
+ $ attributes [$ key ] = $ this ->mutateAttributeForArray ($ key , null );
56
102
}
57
103
104
+ return $ attributes ;
105
+ }
106
+
107
+ /**
108
+ * @param array $attributes
109
+ * @return array
110
+ */
111
+ private function decryptAttributes (array $ attributes ): array
112
+ {
58
113
foreach ($ attributes as $ key => $ value ) {
59
114
if (!in_array ($ key , $ this ->encryptable ) || is_null ($ value ) || $ value === '' ) {
60
115
continue ;
0 commit comments