| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 | 
							- <?php
 
- namespace App\Repositories\Base;
 
- use Illuminate\Support\Collection;
 
- use Illuminate\Database\Eloquent\Model;
 
- use Illuminate\Container\Container as App;
 
- use App\Repositories\Contracts\RepositoryInterface;
 
- use App\Repositories\Exceptions\RepositoryException;
 
- use App\Repositories\Contracts\CriteriaInterface;
 
- use App\Repositories\Base\Criteria;
 
- /**
 
-  * Class Repository
 
-  * @package Bosnadev\Repositories\Eloquent
 
-  */
 
- abstract class Repository implements RepositoryInterface, CriteriaInterface
 
- {
 
-     /**
 
-      * @var App
 
-      */
 
-     private $app;
 
-     /**
 
-      * @var Model
 
-      */
 
-     protected $model;
 
-     protected $newModel;
 
-     /**
 
-      * @var Collection
 
-      */
 
-     protected $criteria;
 
-     /**
 
-      * @var bool
 
-      */
 
-     protected $skipCriteria = false;
 
-     /**
 
-      * Prevents from overwriting same criteria in chain usage
 
-      * @var bool
 
-      */
 
-     protected $preventCriteriaOverwriting = true;
 
-     /**
 
-      * @param App $app
 
-      * @param Collection $collection
 
-      * @throws Exceptions\RepositoryException
 
-      */
 
-     public function __construct(App $app, Collection $collection)
 
-     {
 
-         $this->app = $app;
 
-         $this->criteria = $collection;
 
-         $this->resetScope();
 
-         $this->makeModel();
 
-     }
 
-     /**
 
-      * Specify Model class name
 
-      *
 
-      * @return mixed
 
-      */
 
-     public abstract function model();
 
-     /**
 
-      * @param array $columns
 
-      * @return mixed
 
-      */
 
-     public function all($columns = array('*'))
 
-     {
 
-         $this->applyCriteria();
 
-         return $this->model->get($columns);
 
-     }
 
-     /**
 
-      * @param array $relations
 
-      * @return $this
 
-      */
 
-     public function with(array $relations)
 
-     {
 
-         $this->model = $this->model->with($relations);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param  string $value
 
-      * @param  string $key
 
-      * @return array
 
-      */
 
-     public function lists($value, $key = null)
 
-     {
 
-         $this->applyCriteria();
 
-         $lists = $this->model->lists($value, $key);
 
-         if (is_array($lists)) {
 
-             return $lists;
 
-         }
 
-         return $lists->all();
 
-     }
 
-     /**
 
-      * @param int $perPage
 
-      * @param array $columns
 
-      * @return mixed
 
-      */
 
-     public function paginate($perPage = 25, $columns = array('*'))
 
-     {
 
-         $this->applyCriteria();
 
-         return $this->model->paginate($perPage, $columns);
 
-     }
 
-     /**
 
-      * @param array $data
 
-      * @return mixed
 
-      */
 
-     public function create(array $data)
 
-     {
 
-         return $this->model->create($data);
 
-     }
 
-     /**
 
-      * save a model without massive assignment
 
-      *
 
-      * @param array $data
 
-      * @return bool
 
-      */
 
-     public function saveModel(array $data)
 
-     {
 
-         foreach ($data as $k => $v) {
 
-             $this->model->$k = $v;
 
-         }
 
-         return $this->model->save();
 
-     }
 
-     /**
 
-      * @param array $data
 
-      * @param $id
 
-      * @param string $attribute
 
-      * @return mixed
 
-      */
 
-     public function update($id,array $data, $attribute = "id")
 
-     {
 
-         return $this->model->where($attribute, '=', $id)->update($data);
 
-     }
 
-     /**
 
-      * @param  array $data
 
-      * @param  $id
 
-      * @return mixed
 
-      */
 
-     public function updateRich( $id,array $data)
 
-     {
 
-         if (!($model = $this->model->find($id))) {
 
-             return false;
 
-         }
 
-         return $model->fill($data)->save();
 
-     }
 
-     /**
 
-      * @param $id
 
-      * @return mixed
 
-      */
 
-     public function delete($id)
 
-     {
 
-         return $this->model->destroy($id);
 
-     }
 
-     /**
 
-      * @param $id
 
-      * @return mixed
 
-      */
 
-     public function destroy($id)
 
-     {
 
-         $data = $this->model->find($id);
 
-         if ( $data->delete()) {
 
-             return true;
 
-         }else
 
-             return false;
 
-     }
 
-     /**
 
-      * @param $id
 
-      * @param array $columns
 
-      * @return mixed
 
-      */
 
-     public function find($id, $columns = array('*'))
 
-     {
 
-         $this->applyCriteria();
 
-         return $this->model->find($id, $columns);
 
-     }
 
-     /**
 
-      * @param $attribute
 
-      * @param $value
 
-      * @param array $columns
 
-      * @return mixed
 
-      */
 
-     public function findBy($attribute, $value, $columns = array('*'))
 
-     {
 
-         $this->applyCriteria();
 
-         return $this->model->where($attribute, '=', $value)->first($columns);
 
-     }
 
-     /**
 
-      * @param $attribute
 
-      * @param $value
 
-      * @param array $columns
 
-      * @return mixed
 
-      */
 
-     public function findAllBy($attribute, $value, $columns = array('*'))
 
-     {
 
-         $this->applyCriteria();
 
-         return $this->model->where($attribute, '=', $value)->get($columns);
 
-     }
 
-     /**
 
-      * Find a collection of models by the given query conditions.
 
-      *
 
-      * @param array $where
 
-      * @param array $columns
 
-      * @param bool $or
 
-      *
 
-      * @return \Illuminate\Database\Eloquent\Collection|null
 
-      */
 
-     public function findWhere($where, $columns = ['*'], $or = false)
 
-     {
 
-         $this->applyCriteria();
 
-         $model = $this->model;
 
-         foreach ($where as $field => $value) {
 
-             if ($value instanceof \Closure) {
 
-                 $model = (!$or)
 
-                     ? $model->where($value)
 
-                     : $model->orWhere($value);
 
-             } elseif (is_array($value)) {
 
-                 if (count($value) === 3) {
 
-                     list($field, $operator, $search) = $value;
 
-                     $model = (!$or)
 
-                         ? $model->where($field, $operator, $search)
 
-                         : $model->orWhere($field, $operator, $search);
 
-                 } elseif (count($value) === 2) {
 
-                     list($field, $search) = $value;
 
-                     $model = (!$or)
 
-                         ? $model->where($field, '=', $search)
 
-                         : $model->orWhere($field, '=', $search);
 
-                 }
 
-             } else {
 
-                 $model = (!$or)
 
-                     ? $model->where($field, '=', $value)
 
-                     : $model->orWhere($field, '=', $value);
 
-             }
 
-         }
 
-         return $model->get($columns);
 
-     }
 
-     /**
 
-      * @return \Illuminate\Database\Eloquent\Builder
 
-      * @throws RepositoryException
 
-      */
 
-     public function makeModel()
 
-     {
 
-         return $this->setModel($this->model());
 
-     }
 
-     /**
 
-      * Set Eloquent Model to instantiate
 
-      *
 
-      * @param $eloquentModel
 
-      * @return Model
 
-      * @throws RepositoryException
 
-      */
 
-     public function setModel($eloquentModel)
 
-     {
 
-         $this->newModel = $this->app->make($eloquentModel);
 
-         if (!$this->newModel instanceof Model)
 
-             throw new RepositoryException("Class {$this->newModel} must be an instance of Illuminate\\Database\\Eloquent\\Model");
 
-         return $this->model = $this->newModel;
 
-     }
 
-     /**
 
-      * @return $this
 
-      */
 
-     public function resetScope()
 
-     {
 
-         $this->skipCriteria(false);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param bool $status
 
-      * @return $this
 
-      */
 
-     public function skipCriteria($status = true)
 
-     {
 
-         $this->skipCriteria = $status;
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return mixed
 
-      */
 
-     public function getCriteria()
 
-     {
 
-         return $this->criteria;
 
-     }
 
-     /**
 
-      * @param Criteria $criteria
 
-      * @return $this
 
-      */
 
-     public function getByCriteria(Criteria $criteria)
 
-     {
 
-         $this->model = $criteria->apply($this->model, $this);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param Criteria $criteria
 
-      * @return $this
 
-      */
 
-     public function pushCriteria(Criteria $criteria)
 
-     {
 
-         if ($this->preventCriteriaOverwriting) {
 
-             // Find existing criteria
 
-             $key = $this->criteria->search(function ($item) use ($criteria) {
 
-                 return (is_object($item) && (get_class($item) == get_class($criteria)));
 
-             });
 
-             // Remove old criteria
 
-             if (is_int($key)) {
 
-                 $this->criteria->offsetUnset($key);
 
-             }
 
-         }
 
-         $this->criteria->push($criteria);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return $this
 
-      */
 
-     public function applyCriteria()
 
-     {
 
-         if ($this->skipCriteria === true)
 
-             return $this;
 
-         foreach ($this->getCriteria() as $criteria) {
 
-             if ($criteria instanceof Criteria)
 
-                 $this->model = $criteria->apply($this->model, $this);
 
-         }
 
-         return $this;
 
-     }
 
- }
 
 
  |