-
-
Save jaketoolson/399308f861a8c81efc371f03fe023ea4 to your computer and use it in GitHub Desktop.
PHP Repository pattern
This file contains 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 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