Skip to content

Commit

Permalink
feature: adds generics support to the ModelQueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTheAdams committed Sep 26, 2024
1 parent 6d0d833 commit 1de8121
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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 ) {
$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

0 comments on commit 1de8121

Please sign in to comment.