Last active
August 29, 2015 14:15
-
-
Save muratsplat/dd1efb3735caf7f8c6c0 to your computer and use it in GitHub Desktop.
A abstract class for repository
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 Boruu\Root\Mvc\Repository; | |
/** | |
* Core Repository for Eloquent | |
* | |
* @author Murat Ödünç <[email protected]> | |
* @copyright (c) 2015, Murat Ödünç | |
* @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3 | |
* @package Repository | |
*/ | |
abstract class EloquentRepository { | |
/** | |
* Eloquent Model | |
* | |
* @var \Illuminate\Database\Eloquent\Model | |
*/ | |
protected $model; | |
/** | |
* current model | |
* | |
* @var \Illuminate\Database\Eloquent\Model | |
*/ | |
protected $current; | |
/** | |
* Constructer | |
*/ | |
public function __construct($model = null) { | |
$this->model = $model; | |
} | |
/** | |
* to get all users in a collection object | |
* | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
public function all() { | |
return $this->model->all(); | |
} | |
/** | |
* to get model by id | |
* | |
* @param int $id | |
* @return Illuminate\Database\Eloquent\Model|null | |
*/ | |
public function getById($id) { | |
return $this->model->find($id); | |
} | |
/** | |
* To create model | |
* | |
* @param array $attributes | |
* @return bool | |
*/ | |
abstract public function create(array $attributes); | |
/** | |
* To update model | |
* | |
* @param int $id | |
* @param array $attributes | |
* @return boolean | |
*/ | |
abstract public function update($id, array $attributes); | |
/** | |
* to detete model | |
* | |
* @param int $id | |
* @return bool | |
*/ | |
public function delete($id) { | |
$this->current = $this->getById($id); | |
return is_null($this->current) ? false : $this->current->delete(); | |
} | |
/** | |
* To get all users in Collection object | |
* | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
public function getModels() { | |
return $this->all(); | |
} | |
/** | |
* to get Instance | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function getCurrent() { | |
return $this->current; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment