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

Feature: adds generics support to the ModelQueryBuilder #13

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin/strauss.phar
files/
repo/
vendor/
.idea
17 changes: 10 additions & 7 deletions src/Models/ModelQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@

/**
* @since 1.0.0
* @unreleased improve model generic
*
* @template M of Model
*/
class ModelQueryBuilder extends QueryBuilder {
/**
* @var class-string<Model>
* @var class-string<M>
*/
protected $model;

/**
* @param class-string<Model> $modelClass
* @param class-string<M> $modelClass
*/
public function __construct( string $modelClass ) {
if ( ! is_subclass_of( $modelClass, Model::class ) ) {
Expand Down Expand Up @@ -53,9 +56,9 @@ public function count( $column = null ) : int {
*
* @param string $output
*
* @return Model|null
* @return M|null
*/
public function get( $output = OBJECT ) : ?Model {
public function get( $output = OBJECT ): ?Model {
$row = DB::get_row( $this->getSQL(), OBJECT );

if ( ! $row ) {
Expand All @@ -70,7 +73,7 @@ public function get( $output = OBJECT ) : ?Model {
*
* @since 1.0.0
*
* @return Model[]|null
* @return M[]|null
*/
public function getAll( $output = OBJECT ) : ?array {
$results = DB::get_results( $this->getSQL(), OBJECT );
Expand All @@ -93,7 +96,7 @@ public function getAll( $output = OBJECT ) : ?array {
*
* @param object|null $row
*
* @return Model|null
* @return M|null
*/
protected function getRowAsModel( $row ) {
$model = $this->model;
Expand All @@ -112,7 +115,7 @@ protected function getRowAsModel( $row ) {
*
* @param object[] $results
*
* @return Model[]|null
* @return M[]|null
*/
protected function getAllAsModel( array $results ) {
/** @var Contracts\ModelCrud $model */
Expand Down
Loading