-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
121 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace Prettus\Repository\Eloquent; | ||
|
||
use Closure; | ||
|
@@ -26,8 +27,9 @@ | |
|
||
/** | ||
* Class BaseRepository | ||
* | ||
* @package Prettus\Repository\Eloquent | ||
* @author Anderson Andrade <[email protected]> | ||
* @author Anderson Andrade <[email protected]> | ||
*/ | ||
abstract class BaseRepository implements RepositoryInterface, RepositoryCriteriaInterface | ||
{ | ||
|
@@ -269,7 +271,7 @@ public function scopeQuery(\Closure $scope) | |
/** | ||
* Retrieve data array for populate field select | ||
* | ||
* @param string $column | ||
* @param string $column | ||
* @param string|null $key | ||
* | ||
* @return \Illuminate\Support\Collection|array | ||
|
@@ -284,7 +286,8 @@ public function lists($column, $key = null) | |
/** | ||
* Retrieve data array for populate field select | ||
* Compatible with Laravel 5.3 | ||
* @param string $column | ||
* | ||
* @param string $column | ||
* @param string|null $key | ||
* | ||
* @return \Illuminate\Support\Collection|array | ||
|
@@ -299,10 +302,11 @@ public function pluck($column, $key = null) | |
/** | ||
* Sync relations | ||
* | ||
* @param $id | ||
* @param $relation | ||
* @param $attributes | ||
* @param $id | ||
* @param $relation | ||
* @param $attributes | ||
* @param bool $detaching | ||
* | ||
* @return mixed | ||
*/ | ||
public function sync($id, $relation, $attributes, $detaching = true) | ||
|
@@ -316,6 +320,7 @@ public function sync($id, $relation, $attributes, $detaching = true) | |
* @param $id | ||
* @param $relation | ||
* @param $attributes | ||
* | ||
* @return mixed | ||
*/ | ||
public function syncWithoutDetaching($id, $relation, $attributes) | ||
|
@@ -350,7 +355,7 @@ public function all($columns = ['*']) | |
/** | ||
* Count results of repository | ||
* | ||
* @param array $where | ||
* @param array $where | ||
* @param string $columns | ||
* | ||
* @return int | ||
|
@@ -452,7 +457,7 @@ public function firstOrCreate(array $attributes = []) | |
/** | ||
* Retrieve data of repository with limit applied | ||
* | ||
* @param int $limit | ||
* @param int $limit | ||
* @param array $columns | ||
* | ||
* @return mixed | ||
|
@@ -469,8 +474,8 @@ public function limit($limit, $columns = ['*']) | |
* Retrieve all data of repository, paginated | ||
* | ||
* @param null|int $limit | ||
* @param array $columns | ||
* @param string $method | ||
* @param array $columns | ||
* @param string $method | ||
* | ||
* @return mixed | ||
*/ | ||
|
@@ -490,7 +495,7 @@ public function paginate($limit = null, $columns = ['*'], $method = "paginate") | |
* Retrieve all data of repository, simple paginated | ||
* | ||
* @param null|int $limit | ||
* @param array $columns | ||
* @param array $columns | ||
* | ||
* @return mixed | ||
*/ | ||
|
@@ -617,11 +622,11 @@ public function findWhereBetween($field, array $values, $columns = ['*']) | |
/** | ||
* Save a new entity in repository | ||
* | ||
* @throws ValidatorException | ||
* | ||
* @param array $attributes | ||
* | ||
* @return mixed | ||
* @throws ValidatorException | ||
* | ||
*/ | ||
public function create(array $attributes) | ||
{ | ||
|
@@ -654,12 +659,12 @@ public function create(array $attributes) | |
/** | ||
* Update a entity in repository by id | ||
* | ||
* @throws ValidatorException | ||
* | ||
* @param array $attributes | ||
* @param $id | ||
* | ||
* @return mixed | ||
* @throws ValidatorException | ||
* | ||
*/ | ||
public function update(array $attributes, $id) | ||
{ | ||
|
@@ -705,12 +710,12 @@ public function update(array $attributes, $id) | |
/** | ||
* Update or Create an entity in repository | ||
* | ||
* @throws ValidatorException | ||
* | ||
* @param array $attributes | ||
* @param array $values | ||
* | ||
* @return mixed | ||
* @throws ValidatorException | ||
* | ||
*/ | ||
public function updateOrCreate(array $attributes, array $values = []) | ||
{ | ||
|
@@ -824,7 +829,8 @@ public function with($relations) | |
/** | ||
* Add subselect queries to count the relations. | ||
* | ||
* @param mixed $relations | ||
* @param mixed $relations | ||
* | ||
* @return $this | ||
*/ | ||
public function withCount($relations) | ||
|
@@ -836,7 +842,7 @@ public function withCount($relations) | |
/** | ||
* Load relation with closure | ||
* | ||
* @param string $relation | ||
* @param string $relation | ||
* @param closure $closure | ||
* | ||
* @return $this | ||
|
@@ -865,7 +871,7 @@ public function hidden(array $fields) | |
/** | ||
* Set the "orderBy" value of the query. | ||
* | ||
* @param mixed $column | ||
* @param mixed $column | ||
* @param string $direction | ||
* | ||
* @return $this | ||
|
@@ -1058,14 +1064,90 @@ protected function applyCriteria() | |
* Applies the given where conditions to the model. | ||
* | ||
* @param array $where | ||
* | ||
* @return void | ||
*/ | ||
protected function applyConditions(array $where) | ||
{ | ||
foreach ($where as $field => $value) { | ||
if (is_array($value)) { | ||
list($field, $condition, $val) = $value; | ||
$this->model = $this->model->where($field, $condition, $val); | ||
//smooth input | ||
$condition = preg_replace('/\s\s+/', ' ', trim($condition)); | ||
|
||
//split to get operator, syntax: "DATE >", "DATE =", "DAY <" | ||
$operator = explode(' ', $condition); | ||
if (count($operator) > 1) { | ||
$condition = $operator[0]; | ||
$operator = $operator[1]; | ||
} else $operator = null; | ||
switch (strtoupper($condition)) { | ||
case 'IN': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereIn($field, $val); | ||
break; | ||
case 'NOTIN': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereNotIn($field, $val); | ||
break; | ||
case 'DATE': | ||
if (!$operator) $operator = '='; | ||
$this->model = $this->model->whereDate($field, $operator, $val); | ||
break; | ||
case 'DAY': | ||
if (!$operator) $operator = '='; | ||
$this->model = $this->model->whereDay($field, $operator, $val); | ||
break; | ||
case 'MONTH': | ||
if (!$operator) $operator = '='; | ||
$this->model = $this->model->whereMonth($field, $operator, $val); | ||
break; | ||
case 'YEAR': | ||
if (!$operator) $operator = '='; | ||
$this->model = $this->model->whereYear($field, $operator, $val); | ||
break; | ||
case 'EXISTS': | ||
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function"); | ||
$this->model = $this->model->whereExists($val); | ||
break; | ||
case 'HAS': | ||
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function"); | ||
$this->model = $this->model->whereHas($field, $val); | ||
break; | ||
case 'HASMORPH': | ||
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function"); | ||
$this->model = $this->model->whereHasMorph($field, $val); | ||
break; | ||
case 'DOESNTHAVE': | ||
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function"); | ||
$this->model = $this->model->whereDoesntHave($field, $val); | ||
break; | ||
case 'DOESNTHAVEMORPH': | ||
if (!($val instanceof Closure)) throw new RepositoryException("Input {$val} must be closure function"); | ||
$this->model = $this->model->whereDoesntHaveMorph($field, $val); | ||
break; | ||
case 'BETWEEN': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereBetween($field, $val); | ||
break; | ||
case 'BETWEENCOLUMNS': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereBetweenColumns($field, $val); | ||
break; | ||
case 'NOTBETWEEN': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereNotBetween($field, $val); | ||
break; | ||
case 'NOTBETWEENCOLUMNS': | ||
if (!is_array($val)) throw new RepositoryException("Input {$val} mus be an array"); | ||
$this->model = $this->model->whereNotBetweenColumns($field, $val); | ||
break; | ||
case 'RAW': | ||
$this->model = $this->model->whereRaw($val); | ||
break; | ||
default: | ||
$this->model = $this->model->where($field, $condition, $val); | ||
} | ||
} else { | ||
$this->model = $this->model->where($field, '=', $value); | ||
} | ||
|
@@ -1104,7 +1186,7 @@ public function parserResult($result) | |
|
||
return $model; | ||
}); | ||
} elseif ($result instanceof Presentable) { | ||
} else if ($result instanceof Presentable) { | ||
$result = $result->setPresenter($this->presenter); | ||
} | ||
|
||
|