-
-
Save sabid/8fe2d3436e007a4c27fb676bb5c08b26 to your computer and use it in GitHub Desktop.
This file contains hidden or 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\Shop\Base; | |
use App\Shop\Base\Interfaces\BaseRepositoryInterface; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
use Illuminate\Pagination\LengthAwarePaginator; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
abstract class BaseRepository implements BaseRepositoryInterface | |
{ | |
protected $model; | |
/** | |
* BaseRepository constructor. | |
* @param Model $model | |
*/ | |
public function __construct(Model $model) | |
{ | |
$this->model = $model; | |
} | |
/** | |
* @param array $attributes | |
* @return mixed | |
*/ | |
public function create(array $attributes) | |
{ | |
return $this->model->create($attributes); | |
} | |
/** | |
* @param array $attributes | |
* @param int $id | |
* @return bool | |
*/ | |
public function update(array $attributes, int $id) : bool | |
{ | |
return $this->find($id)->update($attributes); | |
} | |
/** | |
* @param array $columns | |
* @param string $orderBy | |
* @param string $sortBy | |
* @return mixed | |
*/ | |
public function all($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') | |
{ | |
return $this->model->orderBy($orderBy, $sortBy)->get($columns); | |
} | |
/** | |
* @param int $id | |
* @return mixed | |
*/ | |
public function find(int $id) | |
{ | |
return $this->model->find($id); | |
} | |
/** | |
* @param int $id | |
* @return mixed | |
* @throws ModelNotFoundException | |
*/ | |
public function findOneOrFail(int $id) | |
{ | |
return $this->model->findOrFail($id); | |
} | |
/** | |
* @param array $data | |
* @return mixed | |
*/ | |
public function findBy(array $data) | |
{ | |
return $this->model->where($data)->all(); | |
} | |
/** | |
* @param array $data | |
* @return mixed | |
*/ | |
public function findOneBy(array $data) | |
{ | |
return $this->model->where($data)->first(); | |
} | |
/** | |
* @param array $data | |
* @return mixed | |
* @throws ModelNotFoundException | |
*/ | |
public function findOneByOrFail(array $data) | |
{ | |
return $this->model->where($data)->firstOrFail(); | |
} | |
/** | |
* @param array $data | |
* @param int $perPage | |
* @return LengthAwarePaginator | |
*/ | |
public function paginateArrayResults(array $data, int $perPage = 50) | |
{ | |
$page = request()->get('page', 1); | |
$offset = ($page * $perPage) - $perPage; | |
return new LengthAwarePaginator( | |
array_slice($data, $offset, $perPage, false), | |
count($data), | |
$perPage, | |
$page, | |
[ | |
'path' => request()->url(), | |
'query' => request()->query() | |
] | |
); | |
} | |
/** | |
* @param int $id | |
* @return bool | |
*/ | |
public function delete(int $id) : bool | |
{ | |
return $this->model->find($id)->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment