anlutro/laravel-repository — pull request #2 by anlutro (2014-06-19 09:43:30+00:00)
Title: Criteria

Core idea: Class representations of criteria that a query must match. Criteria can contain state and other various information that affect how it affects the query.

```php
setUser($user);
$criteria->setProject($project);
$repo->getForCriteria($criteria);

class MyRepository extends DatabaseRepository
{
public function getForProject(Project $project)
{
$criteria = new MyCriteria();
$criteria->setProject($project);
$this->pushCriteria($criteria);
return $this->getAll();
}

public function getForUser(User $user)
{
$criteria = new MyCriteria();
$criteria->setUser($user);
$this->pushCriteria($criteria);
return $this->getAll();
}

public function getForProjectUser(Project $project, User $user)
{
$criteria = new MyCriteria();
$criteria->setUser($user);
$criteria->setProject($project);
return $this->getAll();
}

public function getFilterInstance()
{
return new MyFilter($this);
}
}

class MyCriteria implements CriteriaInterface
{
protected $project;
protected $user;

public function setProject(Project $project)
{
$this->project = $project;
}

public function setUser(User $user)
{
$this->user = $user;
}

public function apply($query)
{
if ($this->project) {
// lots of intricate joins here maybe?
$query->where('project_id', '=', $this->project->getKey());
}

if ($this->user) {
// lots of intricate joins here maybe?
$query->where('user_id', '=', $this->user->getKey());
}
}
}
```