-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from cycle/active-query
feat: ActiveQuery PoC
- Loading branch information
Showing
12 changed files
with
170 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ActiveRecord\Query; | ||
|
||
use Cycle\ActiveRecord\Facade; | ||
use Cycle\ORM\Select; | ||
|
||
/** | ||
* @template-covariant TEntity of object | ||
* | ||
* @extends Select<TEntity> | ||
*/ | ||
class ActiveQuery extends Select | ||
Check failure on line 15 in src/Query/ActiveQuery.php GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)
|
||
{ | ||
/** | ||
* @param class-string<TEntity> $class | ||
*/ | ||
final public function __construct(string $class) | ||
{ | ||
parent::__construct(Facade::getOrm(), $class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ActiveRecord\Repository; | ||
|
||
use Cycle\ActiveRecord\Facade; | ||
use Cycle\ORM\Select; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template-covariant TEntity of object | ||
*/ | ||
class ActiveRepository | ||
{ | ||
/** @var Select<TEntity> */ | ||
private Select $select; | ||
|
||
/** | ||
* @param class-string<TEntity> $class | ||
*/ | ||
final public function __construct(string $class) | ||
{ | ||
$orm = Facade::getOrm(); | ||
$this->select = new Select($orm, $class); | ||
} | ||
|
||
/** | ||
* ActiveQuery is always immutable by default. | ||
*/ | ||
public function __clone() | ||
{ | ||
$this->select = clone $this->select; | ||
} | ||
|
||
/** | ||
* Get selector associated with the repository. | ||
* | ||
* @return Select<TEntity> | ||
*/ | ||
public function select(): Select | ||
Check failure on line 42 in src/Repository/ActiveRepository.php GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)
|
||
{ | ||
return clone $this->select; | ||
} | ||
|
||
public function find(mixed $id): ?object | ||
{ | ||
return $this->select()->wherePK($id)->fetchOne(); | ||
} | ||
|
||
public function findOne(array $scope = []): ?object | ||
{ | ||
return $this->select()->fetchOne($scope); | ||
} | ||
|
||
public function findAll(array $scope = [], array $orderBy = []): iterable | ||
{ | ||
return $this->select()->where($scope)->orderBy($orderBy)->fetchAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\App\Query; | ||
|
||
use Cycle\ActiveRecord\Query\ActiveQuery; | ||
|
||
/** | ||
* @template-covariant TEntity of object | ||
* | ||
* @extends ActiveQuery<TEntity> | ||
*/ | ||
class UserQuery extends ActiveQuery | ||
{ | ||
public function active(bool $state = true): UserQuery | ||
{ | ||
return $this->where(['active' => $state]); | ||
} | ||
|
||
public function orderByCreatedAt(string $direction = 'ASC'): UserQuery | ||
{ | ||
return $this->orderBy(['created_at' => $direction]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters