Skip to content

Instantly share code, notes, and snippets.

@thomasbellio
Created November 19, 2014 22:33
Show Gist options
  • Save thomasbellio/8bb4e5120d3857468db4 to your computer and use it in GitHub Desktop.
Save thomasbellio/8bb4e5120d3857468db4 to your computer and use it in GitHub Desktop.
<?hh namespace Repositories\Generics;
class BaseRepository<T>
{
private $model;
public function __construct(T $model)
{
$this->model = $model;
}
/**
* Saves the model
* @param T $model
* @return bool true if the model was saved
*/
public function save(T $model)
{
return $model->save();
}
public function all()
{
return $this->model->all();
}
}
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
<?hh use Repositories\Generics\BaseRepository;
class UserControllerGeneric {
private BaseRepository<User> $baseRepository;
public function __construct(BaseRepository<User> $baseRepository)
{
$this->baseRepository = $baseRepository;
}
public function save()
{
$user = new User;
$user->email = uniqid().'@indatus.com';
$user->password = 'password';
$this->baseRepository->save($user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment