Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Undoctrine doctrine specification #82

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Comments
drymek committed Dec 29, 2014
commit 6f1790d01d031ad7ff2fe42f075fab434e28f1b7
12 changes: 12 additions & 0 deletions src/DoctrineTransformer.php
Original file line number Diff line number Diff line change
@@ -36,6 +36,12 @@ public function setQueryBuilder($queryBuilder)
$this->queryBuilder = $queryBuilder;
}

/**
* Modify query builder according to specification
*
* @param Specification $specification
* @return QueryBuilder modified query builder
*/
public function transform(Specification $specification)
{
$queryBuilder = clone $this->queryBuilder;
@@ -47,14 +53,20 @@ private function getTransformers()
return $this->transformers;
}

/**
* Add transformer to collection to use to transform specifications
* @param FilterTransformerInterface $transformer
*/
public function addTransformer(FilterTransformerInterface $transformer)
{
$this->transformers[] = $transformer;
}

/**
* Build where part
* @param Specification $specification
* @param $queryBuilder
*
* @return QueryBuilder
*/
private function addWhere(Specification $specification, $queryBuilder)
23 changes: 23 additions & 0 deletions src/ParametersBag.php
Original file line number Diff line number Diff line change
@@ -4,18 +4,36 @@

class ParametersBag
{
/**
* @var array DQL Parameters array
*/
private $parameters = [];

/**
* Clears parameters array
*/
public function clear()
{
$this->parameters = [];
}

/**
* Returns parameters array
*
* @return array
*/
public function get()
{
return $this->parameters;
}

/**
* Add new parameter to collection and returns it's name
*
* @param $value mixed parameter value
*
* @return string DQL Parameter name
*/
public function add($value)
{
$key = count($this->parameters) + 1;
@@ -24,6 +42,11 @@ public function add($value)
return '?' . $key;
}

/**
* Checks whether the parameters array is non empty
*
* @return bool
*/
public function has()
{
return (boolean)count($this->parameters);
9 changes: 9 additions & 0 deletions src/Transformer/ComparisonTransformer.php
Original file line number Diff line number Diff line change
@@ -23,11 +23,17 @@ class ComparisonTransformer implements FilterTransformerInterface
*/
private $queryBuilder;

/**
* @inheritdoc
*/
public function setQueryBuilder(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}

/**
* @inheritdoc
*/
public function supports(FilterInterface $object)
{
if ($object instanceof Comparison) {
@@ -44,6 +50,9 @@ public function supports(FilterInterface $object)
return false;
}

/**
* @inheritdoc
*/
public function transform(FilterInterface $filter, ParametersBag $parameters)
{
if ($filter instanceof Equals) {
9 changes: 9 additions & 0 deletions src/Transformer/FilterTransformer.php
Original file line number Diff line number Diff line change
@@ -16,16 +16,25 @@ class FilterTransformer implements FilterTransformerInterface
*/
private $queryBuilder;

/**
* @inheritdoc
*/
public function setQueryBuilder(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}

/**
* @inheritdoc
*/
public function supports(FilterInterface $filter)
{
return $filter instanceof IsNotNull || $filter instanceof IsNull;
}

/**
* @inheritdoc
*/
public function transform(FilterInterface $filter, ParametersBag $parameters)
{
if ($filter instanceof IsNull) {
16 changes: 16 additions & 0 deletions src/Transformer/FilterTransformerInterface.php
Original file line number Diff line number Diff line change
@@ -8,9 +8,25 @@

interface FilterTransformerInterface
{
/**
* Return true when transformer supports class
*
* @param FilterInterface $filter
* @return boolean
*/
public function supports(FilterInterface $filter);

/**
* Transform filter to DQL part
* @param FilterInterface $filter
* @param ParametersBag $parameters
*
* @return string
*/
public function transform(FilterInterface $filter, ParametersBag $parameters);

/**
* @param QueryBuilder $queryBuilder
*/
public function setQueryBuilder(QueryBuilder $queryBuilder);
}