From 2ab1a43d1ec9c1e2140219daad85daf3a7534311 Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Mon, 14 Sep 2020 10:04:18 +0700 Subject: [PATCH] added more comment --- src/Contracts/CriteriaInterface.php | 8 +- src/Contracts/CriterionInterface.php | 2 +- src/Contracts/RepositoryInterface.php | 8 +- src/Contracts/ScopesInterface.php | 8 +- src/Criteria/FindWhere.php | 14 ++- src/Eloquent/BaseRepository.php | 2 +- src/Eloquent/RepositoryAbstract.php | 37 +++++++- src/Exceptions/RepositoryException.php | 2 +- src/Helper.php | 96 ++++++++++---------- src/Scopes/Clauses/OrWhereLikeScope.php | 10 +- src/Scopes/Clauses/OrWhereScope.php | 10 +- src/Scopes/Clauses/OrderByScope.php | 19 ++-- src/Scopes/Clauses/WhereDateGreaterScope.php | 10 +- src/Scopes/Clauses/WhereDateLessScope.php | 10 +- src/Scopes/Clauses/WhereLikeScope.php | 10 +- src/Scopes/Clauses/WhereScope.php | 10 +- src/Scopes/ScopeAbstract.php | 2 +- src/Scopes/Scopes.php | 21 ++++- src/Scopes/ScopesAbstract.php | 29 +++++- 19 files changed, 196 insertions(+), 112 deletions(-) diff --git a/src/Contracts/CriteriaInterface.php b/src/Contracts/CriteriaInterface.php index 72c2111..620ebe2 100644 --- a/src/Contracts/CriteriaInterface.php +++ b/src/Contracts/CriteriaInterface.php @@ -4,5 +4,11 @@ interface CriteriaInterface { + /** + * Criteria are a way to build up specific query conditions. + * + * @param array $criteria + * @return $this + */ public function withCriteria(array $criteria); -} \ No newline at end of file +} diff --git a/src/Contracts/CriterionInterface.php b/src/Contracts/CriterionInterface.php index 2dac9c8..9bf9015 100644 --- a/src/Contracts/CriterionInterface.php +++ b/src/Contracts/CriterionInterface.php @@ -11,4 +11,4 @@ interface CriterionInterface * @return mixed */ public function apply($entity); -} \ No newline at end of file +} diff --git a/src/Contracts/RepositoryInterface.php b/src/Contracts/RepositoryInterface.php index 4efa9b2..c400847 100644 --- a/src/Contracts/RepositoryInterface.php +++ b/src/Contracts/RepositoryInterface.php @@ -53,7 +53,7 @@ public function paginate($perPage = null, $columns = ['*']); * * @param array $attributes * @return \CodeIgniter\Database\BaseResult|false|int|string - * + * * @throws \ReflectionException */ public function create(array $attributes); @@ -72,14 +72,14 @@ public function createBatch(array $attributes); * @param array|object $values * @param array|int|string $id * @return bool - * + * * @throws \ReflectionException */ public function update(array $values, $id); /** * Update a batch record. - * + * * @param array $attributes * @param string $id * @return mixed @@ -90,7 +90,7 @@ public function updateBatch(array $attributes, $id); /** * Delete a record by id. - * + * * @param int $id * @return mixed */ diff --git a/src/Contracts/ScopesInterface.php b/src/Contracts/ScopesInterface.php index f9e9ea3..275f722 100644 --- a/src/Contracts/ScopesInterface.php +++ b/src/Contracts/ScopesInterface.php @@ -4,5 +4,11 @@ interface ScopesInterface { + /** + * In your repository define which fields can be used to scope your queries. + * + * @param \CodeIgniter\HTTP\IncomingRequest $request + * @return $this + */ public function scope($request); -} \ No newline at end of file +} diff --git a/src/Criteria/FindWhere.php b/src/Criteria/FindWhere.php index a39a7f7..7a596a8 100644 --- a/src/Criteria/FindWhere.php +++ b/src/Criteria/FindWhere.php @@ -6,18 +6,22 @@ class FindWhere implements CriterionInterface { + /** @var array $conditions */ protected $conditions; + /** + * Constructor FindWhare. + * + * @param array $conditions + * @return void + */ public function __construct(array $conditions) { $this->conditions = $conditions; } /** - * Apply model entity. - * - * @param \CodeIgniter\Model $entity - * @return mixed + * @inheritdoc */ public function apply($entity) { @@ -32,4 +36,4 @@ public function apply($entity) return $entity; } -} \ No newline at end of file +} diff --git a/src/Eloquent/BaseRepository.php b/src/Eloquent/BaseRepository.php index 9869e8d..1ef4a5a 100644 --- a/src/Eloquent/BaseRepository.php +++ b/src/Eloquent/BaseRepository.php @@ -135,4 +135,4 @@ public function orderBy($column, $direction = 'asc') return $this; } -} \ No newline at end of file +} diff --git a/src/Eloquent/RepositoryAbstract.php b/src/Eloquent/RepositoryAbstract.php index 1699377..03c69b3 100644 --- a/src/Eloquent/RepositoryAbstract.php +++ b/src/Eloquent/RepositoryAbstract.php @@ -10,20 +10,30 @@ abstract class RepositoryAbstract implements CriteriaInterface, ScopesInterface { + /** @var \CodeIgniter\Model $entity */ protected $entity; + /** @var array $searchable */ protected $searchable = []; + /** + * Constructor Repository abstract. + */ public function __construct() { $this->entity = $this->resolveEntity(); } /** + * Abstact method to difine instance model. + * * @return \CodeIgniter\Model; */ abstract public function entity(); + /** + * @inheritdoc + */ public function withCriteria(array $criteria) { foreach ($criteria as $criterion) { @@ -33,6 +43,9 @@ public function withCriteria(array $criteria) return $this; } + /** + * @inheritdoc + */ public function scope($request) { $this->entity = (new Scopes($request, $this->searchable))->scope($this->entity); @@ -40,16 +53,25 @@ public function scope($request) return $this; } + /** + * Reset model to new instance. + * + * @return void + */ public function reset() { $this->entity = $this->resolveEntity(); - } + /** + * Provides direct access to method in the builder (if available) + * and the database connection. + * + * @return $this + */ public function __call($method, $parameters) { if (method_exists($this->entity, 'scope' . ucfirst($method))) { - $this->entity = $this->entity->{$method}(...$parameters); return $this; @@ -60,6 +82,11 @@ public function __call($method, $parameters) return $this; } + /** + * Provides/instantiates the from entity model. + * + * @return mixed + */ public function __get($name) { return $this->entity->{$name}; @@ -67,8 +94,10 @@ public function __get($name) /** * Resolve entity. - * + * * @return \CodeIgniter\Model + * + * @throws RepositoryException */ protected function resolveEntity() { @@ -80,4 +109,4 @@ protected function resolveEntity() return $this->entity(); } -} \ No newline at end of file +} diff --git a/src/Exceptions/RepositoryException.php b/src/Exceptions/RepositoryException.php index a7c635f..507d2e6 100644 --- a/src/Exceptions/RepositoryException.php +++ b/src/Exceptions/RepositoryException.php @@ -5,4 +5,4 @@ class RepositoryException extends \Exception { // -} \ No newline at end of file +} diff --git a/src/Helper.php b/src/Helper.php index 05c8eef..5641da2 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -1,47 +1,49 @@ -orLike($scope, $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/OrWhereScope.php b/src/Scopes/Clauses/OrWhereScope.php index 5b12a43..f2b83fd 100644 --- a/src/Scopes/Clauses/OrWhereScope.php +++ b/src/Scopes/Clauses/OrWhereScope.php @@ -9,13 +9,13 @@ class OrWhereScope extends ScopeAbstract /** * Or where scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { return $builder->orWhere($scope, $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/OrderByScope.php b/src/Scopes/Clauses/OrderByScope.php index 72e0a2e..f42d401 100644 --- a/src/Scopes/Clauses/OrderByScope.php +++ b/src/Scopes/Clauses/OrderByScope.php @@ -10,10 +10,10 @@ class OrderByScope extends ScopeAbstract /** * Order by scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { @@ -21,16 +21,15 @@ public function scope($builder, $value, $scope) $orderable = $builder->orderable ?? []; - if (array_pop($arr) == 'desc' - && in_array($field = implode('_', $arr), $orderable)) { - + if ( + array_pop($arr) == 'desc' + && in_array($field = implode('_', $arr), $orderable) + ) { return $builder->orderBy($field, 'desc'); - } elseif (in_array($value, $orderable)) { - return $builder->orderBy($value, 'asc'); } return $builder; } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/WhereDateGreaterScope.php b/src/Scopes/Clauses/WhereDateGreaterScope.php index a06e0db..886b36c 100644 --- a/src/Scopes/Clauses/WhereDateGreaterScope.php +++ b/src/Scopes/Clauses/WhereDateGreaterScope.php @@ -9,13 +9,13 @@ class WhereDateGreaterScope extends ScopeAbstract /** * Where date greeter scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { return $builder->orWhere('created_at' . '>', $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/WhereDateLessScope.php b/src/Scopes/Clauses/WhereDateLessScope.php index c5e2fff..aa3883d 100644 --- a/src/Scopes/Clauses/WhereDateLessScope.php +++ b/src/Scopes/Clauses/WhereDateLessScope.php @@ -9,13 +9,13 @@ class WhereDateLessScope extends ScopeAbstract /** * Where date less scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { return $builder->orWhere('created_at' . '<', $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/WhereLikeScope.php b/src/Scopes/Clauses/WhereLikeScope.php index beebf8c..3735a4d 100644 --- a/src/Scopes/Clauses/WhereLikeScope.php +++ b/src/Scopes/Clauses/WhereLikeScope.php @@ -9,13 +9,13 @@ class WhereLikeScope extends ScopeAbstract /** * Where like scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { return $builder->like($scope, $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/Clauses/WhereScope.php b/src/Scopes/Clauses/WhereScope.php index 06e96da..eb3113f 100644 --- a/src/Scopes/Clauses/WhereScope.php +++ b/src/Scopes/Clauses/WhereScope.php @@ -9,13 +9,13 @@ class WhereScope extends ScopeAbstract /** * Where scope. * - * @param \CodeIgniter\Model $builder - * @param $value - * @param $scope - * @return mixed + * @param \CodeIgniter\Database\BaseBuilder $builder + * @param string $value + * @param string $scope + * @return \CodeIgniter\Database\BaseBuilder */ public function scope($builder, $value, $scope) { return $builder->where($scope, $value); } -} \ No newline at end of file +} diff --git a/src/Scopes/ScopeAbstract.php b/src/Scopes/ScopeAbstract.php index ef38743..387e910 100644 --- a/src/Scopes/ScopeAbstract.php +++ b/src/Scopes/ScopeAbstract.php @@ -15,4 +15,4 @@ protected function resolveScopeValue($key) { return array_get($this->mappings(), $key); } -} \ No newline at end of file +} diff --git a/src/Scopes/Scopes.php b/src/Scopes/Scopes.php index a54b7db..cf9a4eb 100644 --- a/src/Scopes/Scopes.php +++ b/src/Scopes/Scopes.php @@ -12,18 +12,25 @@ class Scopes extends ScopesAbstract { + /** @var array $scopes */ protected $scopes = [ 'orderBy' => OrderByScope::class, 'begin' => WhereDateGreaterScope::class, 'end' => WhereDateLessScope::class, ]; - + + /** + * Constructor scopes. + * + * @param \CodeIgniter\HTTP\IncomingRequest $request + * @param array $searchable + * @return void + */ public function __construct($request, $searchable) { parent::__construct($request); foreach ($searchable as $key => $value) { - if (is_string($key)) { $this->scopes[$key] = $this->mappings($value); } else { @@ -31,7 +38,13 @@ public function __construct($request, $searchable) } } } - + + /** + * Mapping by scope. + * + * @param string $key + * @return string + */ protected function mappings($key) { $mappings = [ @@ -42,4 +55,4 @@ protected function mappings($key) return $mappings[$key] ?? WhereScope::class; } -} \ No newline at end of file +} diff --git a/src/Scopes/ScopesAbstract.php b/src/Scopes/ScopesAbstract.php index 4feb92b..bffbdc0 100644 --- a/src/Scopes/ScopesAbstract.php +++ b/src/Scopes/ScopesAbstract.php @@ -4,8 +4,10 @@ class ScopesAbstract { + /** @var \CodeIgniter\HTTP\IncomingRequest $request */ protected $request; + /** @var array $scopes */ protected $scopes = []; /** @@ -18,6 +20,12 @@ public function __construct($request) $this->request = $request; } + /** + * In your repository define which fields can be used to scope your queries. + * + * @param \CodeIgniter\Database\BaseBuilder $builder + * @return \CodeIgniter\Database\BaseBuilder $builder + */ public function scope($builder) { $scopes = $this->getScopes(); @@ -29,11 +37,22 @@ public function scope($builder) return $builder; } + /** + * Resolve scope mapping. + * + * @param string $scope + * @return object + */ protected function resolveScope($scope) { - return new $this->scopes[$scope]; + return new $this->scopes[$scope](); } + /** + * Get scope mapping by request. + * + * @return object + */ protected function getScopes() { return $this->filterScopes( @@ -41,10 +60,16 @@ protected function getScopes() ); } + /** + * Filter scopes. + * + * @param array $scopes + * @return array + */ protected function filterScopes($scopes) { return array_filter($scopes, function ($scope) { return isset($scope); }); } -} \ No newline at end of file +}