Skip to content

Commit

Permalink
update and fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric G committed Jun 4, 2012
1 parent cbfba1e commit 7e5959f
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 135 deletions.
4 changes: 2 additions & 2 deletions Filter/Extension/Type/CheckboxFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value'])) {
$queryBuilder->andWhere($e->eq($field, $values['value']));
$queryBuilder->andWhere($expr->eq($field, $values['value']));
}
}
}
26 changes: 24 additions & 2 deletions Filter/Extension/Type/ChoiceFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Lexik\Bundle\FormFilterBundle\Filter\Extension\Type;

use Lexik\Bundle\FormFilterBundle\Filter\Expr;

use Doctrine\ORM\QueryBuilder;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Expand All @@ -10,14 +14,14 @@
*
* @author Cédric Girard <[email protected]>
*/
class ChoiceFilterType extends ChoiceType
class ChoiceFilterType extends ChoiceType implements FilterTypeInterface
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return isset($options['expanded']) && $options['expanded'] ? 'filter' : 'filter_field';
return 'filter';
}

/**
Expand All @@ -27,4 +31,22 @@ public function getName()
{
return 'filter_choice';
}

/**
* {@inheritdoc}
*/
public function getTransformerId()
{
return 'lexik_form_filter.transformer.default';
}

/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value'])) {
$queryBuilder->andWhere($expr->eq($field, $values['value']));
}
}
}
20 changes: 2 additions & 18 deletions Filter/Extension/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,11 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);

$compound = function (Options $options) {
return $options['widget'] != 'single_text';
};

$resolver->setDefaults(array(
'compound' => $compound,
));
}

/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if ($values['value'] instanceof \DateTime) {
$date = $values['value']->format(Expr::SQL_DATE);
$queryBuilder->andWhere($e->eq($field, $date));
$queryBuilder->andWhere($expr->eq($field, $date));
}
}
}
6 changes: 3 additions & 3 deletions Filter/Extension/Type/DateRangeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->setAttribute('filter_value_keys', array(
'left_date' => $options['left_date'],
'right_date' => $options['right_date']
'right_date' => $options['right_date'],
));
}

Expand Down Expand Up @@ -66,9 +66,9 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
$value = $values['value'];
$queryBuilder->andWhere($e->dateInRange($field, $value['left_date'][0], $value['right_date'][0]));
$queryBuilder->andWhere($expr->dateInRange($field, $value['left_date'][0], $value['right_date'][0]));
}
}
6 changes: 3 additions & 3 deletions Filter/Extension/Type/EntityFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (is_object($values['value'])) {
if ($values['value'] instanceof Collection) {
Expand All @@ -62,15 +62,15 @@ public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $
$joinAlias = 'a' . $alias;
$queryBuilder
->leftJoin($field, $joinAlias)
->andWhere($e->in($joinAlias, $ids));
->andWhere($expr->in($joinAlias, $ids));
}

} else {
if (!is_callable(array($values['value'], 'getId'))) {
throw new \Exception(sprintf('Can\'t call method "getId()" on an instance of "%s"', get_class($values['value'])));
}

$queryBuilder->andWhere($e->eq($field, $values['value']->getId()));
$queryBuilder->andWhere($expr->eq($field, $values['value']->getId()));
}
}
}
Expand Down
46 changes: 4 additions & 42 deletions Filter/Extension/Type/FieldFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lexik\Bundle\FormFilterBundle\Filter\Extension\Type;

use Symfony\Component\Form\AbstractType as FormFieldType;
use Symfony\Component\Form\Extension\Core\Type\FieldType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Expand All @@ -15,32 +15,14 @@
*
* @author Cédric Girard <[email protected]>
*/
class FieldFilterType extends FormFieldType implements FilterTypeInterface
class FieldFilterType extends FieldType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function getParent()
{
parent::buildForm($builder, $options);

if ($options['apply_filter'] instanceof \Closure || is_callable($options['apply_filter'])) {
$builder->setAttribute('apply_filter', $options['apply_filter']);
}
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);

$resolver->setDefaults(array(
'required' => false,
'apply_filter' => null,
'compound' => false,
));
return 'filter';
}

/**
Expand All @@ -50,24 +32,4 @@ public function getName()
{
return 'filter_field';
}

/**
* {@inheritdoc}
*/
public function getTransformerId()
{
return 'lexik_form_filter.transformer.default';
}

/**
* Default implementation of the applyFieldFilter() method.
* We just add a 'and where' clause.
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
{
if (!empty($values['value'])) {
$queryBuilder->andWhere($e->eq($field, $values['value']));
}
}

}
22 changes: 20 additions & 2 deletions Filter/Extension/Type/FilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\FormFilterBundle\Filter\Extension\Type;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Expand All @@ -15,9 +16,26 @@ class FilterType extends FormType
/**
* {@inheritdoc}
*/
public function getParent()
public function buildForm(FormBuilderInterface $builder, array $options)
{
return 'filter_field';
parent::buildForm($builder, $options);

if ($options['apply_filter'] instanceof \Closure || is_callable($options['apply_filter'])) {
$builder->setAttribute('apply_filter', $options['apply_filter']);
}
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);

$resolver->setDefaults(array(
'required' => false,
'apply_filter' => null,
));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Filter/Extension/Type/FilterTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface FilterTypeInterface
* @param string $field
* @param array $values
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values);
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values);

/**
* Return service id used to transforme values
Expand Down
8 changes: 4 additions & 4 deletions Filter/Extension/Type/NumberFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ($options['condition_operator'] == self::SELECT_OPERATOR) {
$this->transformerId = 'lexik_form_filter.transformer.text';

$numberOptions = array_intersect_key($options, parent::getDefaultOptions(array()));
$numberOptions = array_intersect_key($options, parent::getDefaultOptions());
$numberOptions['required'] = isset($options['required']) ? $options['required'] : false;
$numberOptions['trim'] = isset($options['trim']) ? $options['trim'] : true;

Expand All @@ -67,7 +67,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
parent::setDefaultOptions($resolver);

$compound = function (Options $options) {
return $options['condition_operator'] != NumberFilterType::SELECT_OPERATOR;
return $options['condition_operator'] == NumberFilterType::SELECT_OPERATOR;
};

$resolver->setDefaults(array(
Expand Down Expand Up @@ -103,11 +103,11 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value'])) {
$op = $values['condition_operator'];
$queryBuilder->andWhere($e->$op($field, $values['value']));
$queryBuilder->andWhere($expr->$op($field, $values['value']));
}
}

Expand Down
4 changes: 2 additions & 2 deletions Filter/Extension/Type/NumberRangeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
$value = $values['value'];
if (isset($value['left_number'][0], $value['right_number'][0])) {
$leftCond = $value['left_number']['condition_operator'];
$leftValue = $value['left_number'][0];
$rightCond = $value['right_number']['condition_operator'];
$rightValue = $value['right_number'][0];
$queryBuilder->andWhere($e->andX($e->$leftCond($field, $leftValue), $e->$rightCond($field, $rightValue)));
$queryBuilder->andWhere($expr->andX($expr->$leftCond($field, $leftValue), $expr->$rightCond($field, $rightValue)));
}
}
}
16 changes: 11 additions & 5 deletions Filter/Extension/Type/TextFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ class TextFilterType extends TextType implements FilterTypeInterface

const SELECT_PATTERN = 'select_pattern';

/**
* @var string
*/
protected $transformerId;

protected $parent;

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);

$attributes = array();
$this->transformerId = 'lexik_form_filter.transformer.default';

Expand All @@ -46,8 +49,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'choices' => self::getConditionChoices(),
));
$builder->add('text', 'text', $textOptions);

$this->transformerId = 'lexik_form_filter.transformer.text';
} else {
parent::buildForm($builder, $options);

$attributes['condition_pattern'] = $options['condition_pattern'];
}

Expand Down Expand Up @@ -76,7 +82,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function getParent()
{
return 'filter_field';
return 'filter';
}

/**
Expand All @@ -98,10 +104,10 @@ public function getTransformerId()
/**
* {@inheritdoc}
*/
public function applyFilter(QueryBuilder $queryBuilder, Expr $e, $field, array $values)
public function applyFilter(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value'])) {
$queryBuilder->andWhere($e->stringLike($field, $values['value'], $values['condition_pattern']));
$queryBuilder->andWhere($expr->stringLike($field, $values['value'], $values['condition_pattern']));
}
}

Expand Down
Loading

0 comments on commit 7e5959f

Please sign in to comment.