Skip to content
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

feat: add UserModel::getReturnType() and RegisterController::getUserEntity() uses it #1172

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/customization/user_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ class UserModel extends ShieldUserModel
}
}
```

## Using a Custom User Entity

If you have set a custom `$returnType` in your custom `UserModel`, you may
retrieve the return type using the `UserModel::getReturnType()` method and
easily create a new User Entity using it:

```php
$userEntityClass = $userModel->getReturnType();
$newUser = new $userEntityClass();
```
5 changes: 4 additions & 1 deletion src/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ protected function getUserProvider(): UserModel
*/
protected function getUserEntity(): User
{
return new User();
$userProvider = $this->getUserProvider();
$userEntityClass = $userProvider->getReturnType();

return new $userEntityClass();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,14 @@ private function checkReturnType(): void
throw new LogicException('Return type must be a subclass of ' . User::class);
}
}

/**
* Returns User Entity Type
*
* @return class-string<User>
*/
public function getReturnType(): string
{
return $this->returnType;
}
}
Loading