-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[6.x] Added creation of custom Cast classes for Eloquent #30958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4d4c098
[6.x] The `hasAttributes` trait is divided into two: `hasAttributes` …
1dfd194
[6.x] Added creation of custom Cast classes for Eloquent
3920b01
[6.x] Added make console command of custom Cast
67e2562
Updated codestyle
3914f73
Fixed docblocks
ec1ba01
Fixed docblock
f67cb97
Fixed transfer of null value to custom castes
38b62f9
Fixed getting values for custom castes
f7689b4
Added verification and storage of initialized instances of custom castes
0503111
Fixed null-casting test
99f4ded
Fixed procedure for checking NULL values
27eb546
Added test for checking NULL values
4b8a2d1
Fixed `return null` style-ci errors
0954ebf
Fixed cast.stub file for the custom Cast console command creating
ad8adbc
Changed the variable name from snake_case to camelCase.
6fa8bd1
Updated docblocks
febda92
Updated make:cast command description
d5ddc3b
Updated test for DateTime custom case testing
ce3985a
Added the ability to store the key and the original value in custom c…
e578d21
The `get` and `set` methods are renamed `fromDatabase` and `toDatabas…
92db2cd
The `hasAttributes` trait split has been canceled
eb397ef
Small fixed code in the `hasAttributes` trait
064353a
`array_key_exists` function replaced with faster `isset`
3e51364
Replaced class instance creation method
493e4d7
Added method call skipped when canceling the separation of traits
13ceace
The principle of creating a container has been changed
3446cbe
Added a docblock for the `registerCastMakeCommand` method
d62cd8d
Storage of custom Cast instances transferred to the global static arr…
14720d4
Updated docblock
b8adc68
Replaced `new $cast` by Container make method
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,24 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Contracts\Database\Eloquent; | ||
|
|
||
| interface Castable | ||
| { | ||
| /** | ||
| * Get a given attribute from the model. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
| * @return mixed | ||
| */ | ||
| public function fromDatabase($key, $value = null); | ||
|
|
||
| /** | ||
| * Set a given attribute on the model. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
| * @return mixed | ||
| */ | ||
| public function toDatabase($key, $value = null); | ||
| } | ||
This file contains hidden or 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 hidden or 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,51 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Foundation\Console; | ||
|
|
||
| use Illuminate\Console\GeneratorCommand; | ||
|
|
||
| class CastMakeCommand extends GeneratorCommand | ||
| { | ||
| /** | ||
| * The console command name. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $name = 'make:cast'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Create a new custom Eloquent attribute caster'; | ||
|
|
||
| /** | ||
| * The type of class being generated. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $type = 'Cast'; | ||
|
|
||
| /** | ||
| * Get the stub file for the generator. | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function getStub() | ||
| { | ||
| return __DIR__.'/stubs/cast.stub'; | ||
| } | ||
|
|
||
| /** | ||
| * Get the default namespace for the class. | ||
| * | ||
| * @param string $rootNamespace | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function getDefaultNamespace($rootNamespace) | ||
| { | ||
| return $rootNamespace.'\Casts'; | ||
| } | ||
| } |
This file contains hidden or 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 DummyNamespace; | ||
|
|
||
| use Illuminate\Contracts\Database\Eloquent\Castable; | ||
|
|
||
| class DummyClass implements Castable | ||
| { | ||
| /** | ||
| * Get a given attribute from the model. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
| * @return mixed | ||
| */ | ||
| public function fromDatabase($key, $value = null) | ||
| { | ||
| return $value; | ||
| } | ||
|
|
||
| /** | ||
| * Set a given attribute on the model. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
| * @return mixed | ||
| */ | ||
| public function toDatabase($key, $value = null) | ||
| { | ||
| return $value; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.