-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Custom cast string into Stringable (#39410)
* custom cast string to stringable * fix ci style * styleci
- Loading branch information
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\Castable; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Support\Str; | ||
|
||
class AsStringable implements Castable | ||
{ | ||
/** | ||
* Get the caster class to use when casting from / to this cast target. | ||
* | ||
* @param array $arguments | ||
* @return object|string | ||
*/ | ||
public static function castUsing(array $arguments) | ||
{ | ||
return new class implements CastsAttributes | ||
{ | ||
public function get($model, $key, $value, $attributes) | ||
{ | ||
return isset($value) ? Str::of($value) : null; | ||
} | ||
|
||
public function set($model, $key, $value, $attributes) | ||
{ | ||
return isset($value) ? (string) $value : null; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters