From bc0b1a4c2abda31cda8be2e8b02f13881cca1204 Mon Sep 17 00:00:00 2001 From: Jason Adams Date: Thu, 26 Sep 2024 11:59:37 -0600 Subject: [PATCH] Feature: adds generics support to the ModelQueryBuilder (#13) --- .gitignore | 1 + src/Models/ModelQueryBuilder.php | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 8af118f..740b442 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bin/strauss.phar files/ repo/ vendor/ +.idea diff --git a/src/Models/ModelQueryBuilder.php b/src/Models/ModelQueryBuilder.php index fb8ce78..0468fe0 100644 --- a/src/Models/ModelQueryBuilder.php +++ b/src/Models/ModelQueryBuilder.php @@ -10,15 +10,18 @@ /** * @since 1.0.0 + * @unreleased improve model generic + * + * @template M of Model */ class ModelQueryBuilder extends QueryBuilder { /** - * @var class-string + * @var class-string */ protected $model; /** - * @param class-string $modelClass + * @param class-string $modelClass */ public function __construct( string $modelClass ) { if ( ! is_subclass_of( $modelClass, Model::class ) ) { @@ -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 ) { @@ -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 ); @@ -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; @@ -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 */