feat: add UserModel::getReturnType() and RegisterController::getUserEntity() uses it #1172
feat: add UserModel::getReturnType() and RegisterController::getUserEntity() uses it #1172christianberkman wants to merge 5 commits intocodeigniter4:developfrom christianberkman:user-entity
UserModel::getReturnType() and RegisterController::getUserEntity() uses it #1172Conversation
src/Models/UserModel.php
Outdated
| } | ||
| } | ||
|
|
||
| public function getUserEntity(): User |
There was a problem hiding this comment.
| public function getUserEntity(): User | |
| /** | |
| * Returns User Entity Type | |
| * | |
| * @return class-string<User> | |
| */ | |
| public function getReturnType(): string |
There was a problem hiding this comment.
Is it better to add createNewUser(array $data): User or something instead of getReturnType(): string?
There was a problem hiding this comment.
That would omit the UserEntity entirely so we would still not be able to use UserEntity during the RegistrationAction?
There was a problem hiding this comment.
If we add the following method to UserModel:
/**
* Returns a new User Entity.
*
* @param array<string, mixed> $data The data passed to the constructor
*/
public function createNewUser(array $data = []): User
{
return new $this->returnType($data);
}RegisterController would be like this:
protected function getUserEntity(): User
{
$userProvider = $this->getUserProvider();
return $userProvider->createNewUser();
}There was a problem hiding this comment.
@christianberkman
I think adding a method to create a new user is better than a method that just returns the user type string.
Would you like to make such an update?
There was a problem hiding this comment.
Sorry typo. I meant
So do you propose the following? (My last code snippet)
I also read your snippet with createNewUser wrong and actually we are on the same line indeed.
Is it now better to open a fresh pull request since this differs from the original suggestion?
There was a problem hiding this comment.
Yes, it would be better to open a fresh PR.
And I propose to add the first parameter:
/**
* Returns a new User Entity.
*
* @param array<string, mixed> $data (Optional) user data
*/
public function createNewUser(array $data = []): User
{
return new $this->returnType($data);
}Then, a dev can create a complete new User entity.
If a dev want to use Complete Constructor to the custom User entity, s/he needs complete data to construct a new entity.
Also, the CI4 Entity permits an empty instance creation, so we can just call it without arguments.
There was a problem hiding this comment.
I personally don't like using mixed, and I prefer to explicitly define the possible types as much as we can. For any other unexpected values, we can wait for bug reports from users.
There was a problem hiding this comment.
If we don't use mixed, something like this:
array<string, array|bool|int|float|null|object|string>
But it contains array, and it is not acceptable. Therefore,
array<string, array<array-key, array|bool|int|float|null|object|string>|bool|int|float|null|object|string>
But still contains array... Give up and use mixed.
array<string, array<array-key, mixed>|bool|int|float|null|object|string>
Which is better?
@param array<string, mixed> $data (Optional) user data@param array<string, array<array-key, mixed>|bool|int|float|null|object|string> $data (Optional) user data
or something else?
There was a problem hiding this comment.
My choice is the second option because it strikes a balance between overly vague type definitions and overwhelming complexity. While it's not ideal, it offers a reasonable level of flexibility and clarity.
@param array<string, array<array-key, mixed>|bool|int|float|null|object|string> $data (Optional) user dataUserModel::getReturnType() and RegisterController::getUserEntity() uses it
Co-authored-by: kenjis <kenji.uui@gmail.com>
|
I sent #1196 |
Description
See #1170
Refactor
RegisterController::getUserEntityto useUserModel::returnTypeinstead of the hard codednew User(). This allows for easy use of a custom User Entity without needing to extend theRegisterControllerand add the routes.Checklist: