Skip to content

Commit

Permalink
added more comment
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Sep 14, 2020
1 parent 2fed28d commit 2ab1a43
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 112 deletions.
8 changes: 7 additions & 1 deletion src/Contracts/CriteriaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Contracts/CriterionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ interface CriterionInterface
* @return mixed
*/
public function apply($entity);
}
}
8 changes: 4 additions & 4 deletions src/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -90,7 +90,7 @@ public function updateBatch(array $attributes, $id);

/**
* Delete a record by id.
*
*
* @param int $id
* @return mixed
*/
Expand Down
8 changes: 7 additions & 1 deletion src/Contracts/ScopesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 9 additions & 5 deletions src/Criteria/FindWhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -32,4 +36,4 @@ public function apply($entity)

return $entity;
}
}
}
2 changes: 1 addition & 1 deletion src/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ public function orderBy($column, $direction = 'asc')

return $this;
}
}
}
37 changes: 33 additions & 4 deletions src/Eloquent/RepositoryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,23 +43,35 @@ public function withCriteria(array $criteria)
return $this;
}

/**
* @inheritdoc
*/
public function scope($request)
{
$this->entity = (new Scopes($request, $this->searchable))->scope($this->entity);

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;
Expand All @@ -60,15 +82,22 @@ public function __call($method, $parameters)
return $this;
}

/**
* Provides/instantiates the from entity model.
*
* @return mixed
*/
public function __get($name)
{
return $this->entity->{$name};
}

/**
* Resolve entity.
*
*
* @return \CodeIgniter\Model
*
* @throws RepositoryException
*/
protected function resolveEntity()
{
Expand All @@ -80,4 +109,4 @@ protected function resolveEntity()

return $this->entity();
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/RepositoryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class RepositoryException extends \Exception
{
//
}
}
96 changes: 49 additions & 47 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
<?php

if (! function_exists('array_get')) {
/**
* Get an item from an array using "dot" notation.
*
* @see https://github.com/rappasoft/laravel-helpers/blob/c8dfa1e979437528262725ebe99c2e6383b24c16/src/helpers.php#L236
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
function array_get(array $array, string $key, $default = null)
{
if (is_null($key)) {
return $array;
}

if (isset($array[$key])) {
return $array[$key];
}

foreach (explode('.', $key) as $segment) {
if (! is_array($array) || ! array_key_exists($segment, $array)) {
return value($default);
}

$array = $array[$segment];
}

return $array;
}
}

if (! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @see https://github.com/rappasoft/laravel-helpers/blob/c8dfa1e979437528262725ebe99c2e6383b24c16/src/helpers.php#L1424
* @param mixed $value
* @return mmixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
<?php

if (! function_exists('array_get')) {
/**
* Get an item from an array using "dot" notation.
*
* @see https://github.com/rappasoft/laravel-helpers/blob/c8dfa1e979437528262725ebe99c2e6383b24c16/src/helpers.php#L236
*
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
function array_get(array $array, string $key, $default = null)
{
if (is_null($key)) {
return $array;
}

if (isset($array[$key])) {
return $array[$key];
}

foreach (explode('.', $key) as $segment) {
if (! is_array($array) || ! array_key_exists($segment, $array)) {
return value($default);
}

$array = $array[$segment];
}

return $array;
}
}

if (! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @see https://github.com/rappasoft/laravel-helpers/blob/c8dfa1e979437528262725ebe99c2e6383b24c16/src/helpers.php#L1424
*
* @param mixed $value
* @return mmixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
10 changes: 5 additions & 5 deletions src/Scopes/Clauses/OrWhereLikeScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class OrWhereLikeScope extends ScopeAbstract
/**
* Or 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->orLike($scope, $value);
}
}
}
10 changes: 5 additions & 5 deletions src/Scopes/Clauses/OrWhereScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
19 changes: 9 additions & 10 deletions src/Scopes/Clauses/OrderByScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@ 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)
{
$arr = explode('_', $value);

$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;
}
}
}
Loading

0 comments on commit 2ab1a43

Please sign in to comment.