Skip to content

Instantly share code, notes, and snippets.

@andrewmclagan
Created March 8, 2018 21:50
Show Gist options
  • Save andrewmclagan/aaf49eaf067d8d2a6a88f08f1db23cc9 to your computer and use it in GitHub Desktop.
Save andrewmclagan/aaf49eaf067d8d2a6a88f08f1db23cc9 to your computer and use it in GitHub Desktop.
PHP Repository pattern
<?php
namespace EthicalJobs\Foundation\Storage;
use Illuminate\Support\Collection;
interface Repository
{
/**
* Find a model by its id
*
* @param string|int $id
* @return Illuminate\Database\Eloquent\Model
*/
public function findById($id): Model;
/**
* Find a model by a field
*
* @param string $field
* @param mixed $value
* @return Illuminate\Database\Eloquent\Model
*/
public function findBy(string $field, $value): Model;
/**
* Executes a where query on a field.
* - As a shortcut $operator can be $value for an assumed = operator
* - Valid operators [>=, <=, >, <, !=, like]
*
* @param string $field
* @param string|array $terms
* @return $this
*/
public function where(string $field, $operator, $value = null): Repository;
/**
* Executes a whereIn query matching an array of values.
*
* @param string $field
* @param array $terms
* @return $this
*/
public function whereIn(string $field, array $values): Repository;
/**
* Execute an order by query
*
* @param string $orderBy
* @return $this
*/
public function orderBy(string $orderBy): Repository;
/**
* Set the order direction [ASC, DESC]
*
* @param string $direction
* @return $this
*/
public function sortBy(string $direction): Repository;
/**
* Limit the current query
*
* @param int $limit
* @return $this
*/
public function limit(int $limit): Repository;
/**
* Hydrate results as Eloquent models
*
* @return $self
*/
public function asModels(): Repository;
/**
* Hydrate results as ArrayObjects
*
* @return $self
*/
public function asObjects(): Repository;
/**
* Hydrate results as associative arrays
*
* @return $self
*/
public function asArrays(): Repository;
/**
* Return the result of the query
*
* @return \Illuminate\Support\Collection
*/
public function find(): Collection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment