Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Created March 21, 2018 13:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jsdecena/4b13a05c2c13f651b1dae95b17941221 to your computer and use it in GitHub Desktop.
Save jsdecena/4b13a05c2c13f651b1dae95b17941221 to your computer and use it in GitHub Desktop.
<?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();
}
}
@fellipegpbotelho
Copy link

Very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment