Skip to content

Commit d298df7

Browse files
nunomaduroStyleCIBottaylorotwellinnocenzi
authored
[11.x] Adds Model::casts() method and named static methods for built-in casters (#47237)
* Adds `Model::casts()` method * Apply fixes from StyleCI * Adds `AsCollection::using` * Adds `AsEncryptedCollection::using` * Adds `AsEnumArrayObject::using` * Adjusts tests * Fixes docs * Adds `AsEnumCollection::using` * Adds array format support * Tests priority * Tests serialization * Method name * Fixes single element in array * Fixes single element in array * formatting * Update src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Co-authored-by: Enzo Innocenzi <[email protected]> * Adjusts changes after rebase --------- Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]> Co-authored-by: Enzo Innocenzi <[email protected]>
1 parent 7df3c90 commit d298df7

File tree

6 files changed

+294
-23
lines changed

6 files changed

+294
-23
lines changed

src/Illuminate/Database/Eloquent/Casts/AsCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ public function set($model, $key, $value, $attributes)
4646
}
4747
};
4848
}
49+
50+
/**
51+
* Specify the collection for the cast.
52+
*
53+
* @param class-string $class
54+
* @return string
55+
*/
56+
public static function using($class)
57+
{
58+
return static::class.':'.$class;
59+
}
4960
}

src/Illuminate/Database/Eloquent/Casts/AsEncryptedCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public function set($model, $key, $value, $attributes)
4949
}
5050
};
5151
}
52+
53+
/**
54+
* Specify the collection for the cast.
55+
*
56+
* @param class-string $class
57+
* @return string
58+
*/
59+
public static function using($class)
60+
{
61+
return static::class.':'.$class;
62+
}
5263
}

src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ protected function getStorableEnumValue($enum)
8181
}
8282
};
8383
}
84+
85+
/**
86+
* Specify the Enum for the cast.
87+
*
88+
* @param class-string $class
89+
* @return string
90+
*/
91+
public static function of($class)
92+
{
93+
return static::class.':'.$class;
94+
}
8495
}

src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,15 @@ protected function getStorableEnumValue($enum)
7777
}
7878
};
7979
}
80+
81+
/**
82+
* Specify the Enum for the cast.
83+
*
84+
* @param class-string $class
85+
* @return string
86+
*/
87+
public static function of($class)
88+
{
89+
return static::class.':'.$class;
90+
}
8091
}

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ trait HasAttributes
182182
*/
183183
public static $encrypter;
184184

185+
/**
186+
* Initialize the trait.
187+
*
188+
* @return void
189+
*/
190+
protected function initializeHasAttributes()
191+
{
192+
$this->casts = $this->ensureCastsAreStringValues(
193+
array_merge($this->casts, $this->casts()),
194+
);
195+
}
196+
185197
/**
186198
* Convert the model's attributes to an array.
187199
*
@@ -719,11 +731,39 @@ protected function mutateAttributeForArray($key, $value)
719731
*/
720732
public function mergeCasts($casts)
721733
{
734+
$casts = $this->ensureCastsAreStringValues($casts);
735+
722736
$this->casts = array_merge($this->casts, $casts);
723737

724738
return $this;
725739
}
726740

741+
/**
742+
* Ensure that the given casts are strings.
743+
*
744+
* @param array $casts
745+
* @return array
746+
*/
747+
protected function ensureCastsAreStringValues($casts)
748+
{
749+
foreach ($casts as $attribute => $cast) {
750+
$casts[$attribute] = match (true) {
751+
is_array($cast) => value(function () use ($cast) {
752+
if (count($cast) === 1) {
753+
return $cast[0];
754+
}
755+
756+
[$cast, $arguments] = [array_shift($cast), $cast];
757+
758+
return $cast.':'.implode(',', $arguments);
759+
}),
760+
default => $cast,
761+
};
762+
}
763+
764+
return $casts;
765+
}
766+
727767
/**
728768
* Cast an attribute to a native PHP type.
729769
*
@@ -1521,7 +1561,7 @@ public function hasCast($key, $types = null)
15211561
}
15221562

15231563
/**
1524-
* Get the casts array.
1564+
* Get the attributes that should be cast.
15251565
*
15261566
* @return array
15271567
*/
@@ -1534,6 +1574,16 @@ public function getCasts()
15341574
return $this->casts;
15351575
}
15361576

1577+
/**
1578+
* Get the attributes that should be cast.
1579+
*
1580+
* @return array
1581+
*/
1582+
protected function casts()
1583+
{
1584+
return [];
1585+
}
1586+
15371587
/**
15381588
* Determine whether a value is Date / DateTime castable for inbound manipulation.
15391589
*

0 commit comments

Comments
 (0)