Skip to content

Instantly share code, notes, and snippets.

@mibk
Last active December 14, 2018 10:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mibk/9410266 to your computer and use it in GitHub Desktop.
Save mibk/9410266 to your computer and use it in GitHub Desktop.
LeanMapperQuery suggested base classes.
<?php
namespace Model\Entity;
use LeanMapperQuery\Entity;
use LeanMapperQuery\IQuery;
use Model\Query\EntityQuery;
class BaseEntity extends Entity
{
protected static $magicMethodsPrefixes = array('findOne', 'findCount', 'find');
protected function find($field, IQuery $query)
{
$entities = $this->queryProperty($field, $query);
return $this->entityFactory->createCollection($entities);
}
protected function findOne($field, IQuery $query)
{
$query->limit(1);
$entities = $this->queryProperty($field, $query);
if ($entities) {
return $entities[0];
}
return NULL;
}
protected function findCount($field, IQuery $query)
{
return count($this->queryProperty($field, $query));
}
protected function createQueryObject($field)
{
return new EntityQuery($this, $field);
}
public function __call($name, array $arguments)
{
if (preg_match('#^query(.+)$#', $name, $matches)) {
return $this->createQueryObject($matches[1]);
} else {
return parent::__call($name, $arguments);
}
}
}
<?php
namespace Model\Repository;
use LeanMapper\Repository;
use LeanMapperQuery\IQuery;
use Model\Query\RepositoryQuery;
class BaseRepository extends Repository implements IQueryable
{
private function apply(IQuery $query)
{
$fluent = $this->createFluent();
$query->applyQuery($fluent, $this->mapper);
return $fluent;
}
public function query()
{
return new RepositoryQuery($this);
}
/**
* @param IQuery $query
* @return Entity[]
*/
public function find(IQuery $query)
{
return $this->createEntities($this->apply($query)->fetchAll());
}
/**
* @param IQuery $query
* @return Entity|NULL
*/
public function findOne(IQuery $query)
{
$row = $this->apply($query)
->removeClause('limit')
->removeClause('offset')
->fetch();
return $row ? $this->createEntity($row) : NULL;
}
/**
* @param IQuery $query
* @return int
*/
public function findCount(IQuery $query)
{
return count($this->apply($query));
}
}
<?php
namespace Model\Query;
use LeanMapperQuery\Query;
use LeanMapperQuery\Entity;
class EntityQuery extends Query implements \Countable
{
/** @var Entity */
private $entity;
/** @var string */
private $field;
public function __construct(Entity $entity, $field)
{
$this->entity = $entity;
$this->field = $field;
}
public function find()
{
return $this->entity->{'find' . ucfirst($this->field)}($this);
}
public function findOne()
{
return $this->entity->{'findOne' . ucfirst($this->field)}($this);
}
public function count()
{
return $this->entity->{'findCount' . ucfirst($this->field)}($this);
}
}
<?php
namespace Model\Repository;
use LeanMapperQuery\IQuery;
use LeanMapper\Entity;
interface IQueryable
{
/**
* @param IQuery $query
* @return Entity[]
*/
public function find(IQuery $query);
/**
* @param IQuery $query
* @return Entity
*/
public function findOne(IQuery $query);
/**
* @param IQuery $query
* @return int
*/
public function findCount(IQuery $query);
}
<?php
namespace Model\Query;
use LeanMapperQuery\Query;
use Model\Repository\IQueryable;
class RepositoryQuery extends Query implements \Countable
{
/** @var IQueryable */
private $queryable;
public function __construct(IQueryable $queryable)
{
$this->queryable = $queryable;
}
public function find()
{
return $this->queryable->find($this);
}
public function findOne()
{
return $this->queryable->findOne($this);
}
public function count()
{
return $this->queryable->findCount($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment