Last active
August 29, 2015 14:20
-
-
Save vinicius73/8f1c0d456cb4f3d9621f to your computer and use it in GitHub Desktop.
Repo Base
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
<?php namespace App\Contracts\Repositories; | |
interface Institutions extends Segregated\Common, Segregated\CommonCreate, Segregated\FractalAvailable | |
{ | |
} |
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
<?php namespace App\Contracts\Repositories\Segregated; | |
use Illuminate\Database\Eloquent\Model; | |
interface Common | |
{ | |
/** | |
* @param bool $paginate | |
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query | |
* @param int $take | |
* @param bool $cache | |
* | |
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\AbstractPaginator | |
*/ | |
public function getAll($paginate = true, $query = null, $take = 15, $cache = false); | |
/** | |
* @param int $id | |
* @param boolean $fail | |
* | |
* @return Model | |
*/ | |
public function findByID($id, $fail = true); | |
/** | |
* @param Model $model | |
* @param array $data | |
* | |
* @return bool | |
*/ | |
public function update(Model &$model, array $data = array()); | |
/** | |
* @param Model $model | |
* | |
* @return bool | |
*/ | |
public function save(Model &$model); | |
/** | |
* @param Model $model | |
* | |
* @return bool|null | |
* | |
* @throws \Exception | |
*/ | |
public function delete(Model &$model); | |
} |
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
<?php namespace App\Repositories; | |
use App\Contracts\Repositories\Segregated\Common; | |
use App\Contracts\Repositories\Segregated\CommonCreate; | |
use Illuminate\Database\Eloquent\Builder as QueryBuilder; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Http\Request; | |
use League\Fractal\TransformerAbstract; | |
abstract class CommonRepository implements Common | |
{ | |
/** | |
* @var int | |
*/ | |
protected $cacheSqlTime = 15; | |
/** | |
* @var Request | |
*/ | |
protected $request; | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* @param bool $paginate | |
* @param QueryBuilder|\Illuminate\Database\Query\Builder $query | |
* @param int $take | |
* @param bool $cache | |
* | |
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\Paginator|static[] | |
*/ | |
public function doQuery($paginate = true, $query = null, $take = 15, $cache = false) | |
{ | |
if (is_null($query)): | |
$query = $this->newQuery(); | |
endif; | |
return ($paginate) ? return $query->paginate($take) : $query->get(); | |
} | |
/** | |
* @param bool $paginate | |
* @param QueryBuilder|\Illuminate\Database\Query\Builder $query | |
* @param int $take | |
* @param bool $cache | |
* | |
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\AbstractPaginator | |
*/ | |
public function getAll($paginate = true, $query = null, $take = 15, $cache = false) | |
{ | |
return $this->doQuery($paginate, $query, $take, $cache); | |
} | |
/** | |
* @param int $id | |
* @param boolean $fail | |
* | |
* @return Model | |
*/ | |
public function findByID($id, $fail = true) | |
{ | |
if ($fail): | |
return $this->newQuery()->findOrFail($id); | |
endif; | |
return $this->newQuery()->find($id); | |
} | |
/** | |
* @param Model $model | |
* @param array $data | |
* | |
* @return bool | |
*/ | |
public function update(Model &$model, array $data = array()) | |
{ | |
if (empty($data)) $data = $this->request->all(); | |
$model->fill($data); | |
return $this->save($model); | |
} | |
/** | |
* @param Model $model | |
* | |
* @return bool | |
*/ | |
public function save(Model &$model) | |
{ | |
return $model->save(); | |
} | |
/** | |
* @param Model $model | |
* | |
* @return bool|null | |
* | |
* @throws \Exception | |
*/ | |
public function delete(Model &$model) | |
{ | |
return $model->delete(); | |
} | |
/** | |
* Create model object, but not persist | |
* | |
* @param array $data | |
* | |
* @return Model | |
*/ | |
public function create(array $data = []) | |
{ | |
$data = (empty($data)) ? $this->request->all() : $data; | |
return $this->newQuery()->getModel()->newInstance($data); | |
} | |
/** | |
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder | |
*/ | |
abstract protected function newQuery(); | |
} |
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
<?php namespace Aprimore\Repositories; | |
use App\Models\Institution; | |
use App\Contracts\Repositories\Institutions as InstitutionsContract; | |
class Institutions extends CommonRepository implements InstitutionsContract | |
{ | |
/** | |
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder | |
*/ | |
protected function newQuery() | |
{ | |
return Institution::query(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment