Skip to content

Commit

Permalink
Now values can be null on Repo = [] parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleangioni committed Jan 27, 2016
1 parent d5a4f62 commit e9c24ce
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions src/MicheleAngioni/Support/Repos/AbstractEloquentRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace MicheleAngioni\Support\Repos;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class AbstractEloquentRepository implements RepositoryCacheableQueriesInterface
Expand Down Expand Up @@ -58,9 +59,7 @@ public function firstBy(array $where = array(), array $with = array())
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->first();
}
Expand All @@ -69,9 +68,7 @@ public function firstOrFailBy(array $where = array(), array $with = array())
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->firstOrFail();
}
Expand All @@ -80,9 +77,7 @@ public function getBy(array $where = array(), array $with = array())
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->get();
}
Expand All @@ -91,9 +86,7 @@ public function getByLimit($limit, array $where = array(), array $with = array()
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->take($limit)->get();
}
Expand All @@ -102,9 +95,7 @@ public function getByOrder($orderBy, array $where = array(), array $with = array
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

$query = $query->orderBy($orderBy, $order);

Expand Down Expand Up @@ -180,9 +171,7 @@ public function getHas($relation, array $where = array(), array $with = array(),
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->has($relation, '>=', $hasAtLeast)->get();
}
Expand All @@ -201,9 +190,7 @@ public function hasFirst($relation, array $where = array(), array $with = array(
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->has($relation, '>=', $hasAtLeast)->first();
}
Expand All @@ -222,9 +209,7 @@ public function hasFirstOrFail($relation, array $where = array(), array $with =
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->has($relation, '>=', $hasAtLeast)->firstOrFail();
}
Expand All @@ -243,15 +228,11 @@ public function whereHas($relation, array $where = array(), array $whereHas = ar
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

$query = $query->whereHas($relation, function($q) use($whereHas)
{
foreach($whereHas as $key => $value) {
$q = $q->where($key, '=', $value);
}
$this->applyWhere($q, $whereHas);
});

return $query->get();
Expand All @@ -273,17 +254,15 @@ public function getByPage($page = 1, $limit = 10, array $where = array(), $with
{
$query = $this->make($with);

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

if($orderBy) {
$query = $query->orderBy($orderBy, $order);
}

return $query->skip($limit * ($page - 1))
->take($limit)
->get();
->take($limit)
->get();
}


Expand Down Expand Up @@ -328,9 +307,7 @@ public function updateBy(array $where, array $inputs)

$query = $this->make();

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->update($inputs);
}
Expand All @@ -341,9 +318,7 @@ public function updateOrCreateBy(array $where, array $inputs = [])

$query = $this->make();

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

$model = $query->first();

Expand Down Expand Up @@ -375,9 +350,7 @@ public function destroyBy(array $where)
{
$query = $this->make();

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->delete();
}
Expand All @@ -400,9 +373,7 @@ public function countBy(array $where = array())
{
$query = $this->make();

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

return $query->count();
}
Expand All @@ -420,15 +391,11 @@ public function countWhereHas($relation, array $where = array(), array $whereHas
{
$query = $this->make();

foreach($where as $key => $value) {
$query = $query->where($key, '=', $value);
}
$query = $this->applyWhere($query, $where);

$query = $query->whereHas($relation, function($q) use($whereHas)
{
foreach($whereHas as $key => $value) {
$q = $q->where($key, '=', $value);
}
$this->applyWhere($q, $whereHas);
});

return $query->count();
Expand All @@ -437,6 +404,28 @@ public function countWhereHas($relation, array $where = array(), array $whereHas

// <--- INTERNALLY USED METHODS --->

/**
* Apply the where clauses to input query.
*
* @param Builder $query
* @param array $where
*
* @return Builder
*/
protected function applyWhere(Builder $query, array $where)
{
foreach($where as $key => $value) {
if(is_null($value)) {
$query = $query->whereNull($key);
}
else {
$query = $query->where($key, '=', $value);
}
}

return $query;
}

/**
* Remove keys from the $inputs array beginning with '_' .
*
Expand Down

0 comments on commit e9c24ce

Please sign in to comment.