Skip to content

Commit f5ec47d

Browse files
author
daniel
committed
allow decrypt before cast
1 parent 1f41065 commit f5ec47d

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

src/Traits/EncryptableDbAttribute.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,43 @@
1212
trait EncryptableDbAttribute
1313
{
1414
/**
15-
* @param string $key
15+
* Get a plain attribute (not a relationship).
1616
*
17+
* @param string $key
1718
* @return mixed
1819
*/
19-
public function getAttribute($key)
20+
public function getAttributeValue($key)
2021
{
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+
}
2242

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);
2549
}
2650

27-
return $this->decrypt($value);
51+
return $value;
2852
}
2953

3054
/**
@@ -49,12 +73,43 @@ public function setAttribute($key, $value)
4973
*/
5074
public function attributesToArray(): array
5175
{
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);
56102
}
57103

104+
return $attributes;
105+
}
106+
107+
/**
108+
* @param array $attributes
109+
* @return array
110+
*/
111+
private function decryptAttributes(array $attributes): array
112+
{
58113
foreach ($attributes as $key => $value) {
59114
if (!in_array($key, $this->encryptable) || is_null($value) || $value === '') {
60115
continue;

0 commit comments

Comments
 (0)