Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from lexik/regroup_constants
Browse files Browse the repository at this point in the history
Regroup constants
  • Loading branch information
Cédric Girard committed Jan 21, 2013
2 parents c9456c2 + 73e8cbb commit 9c01c14
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 108 deletions.
38 changes: 7 additions & 31 deletions Filter/Extension/Type/NumberFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand All @@ -13,14 +15,6 @@
*/
class NumberFilterType extends AbstractFilterType
{
const OPERATOR_EQUAL = 'eq';
const OPERATOR_GREATER_THAN = 'gt';
const OPERATOR_GREATER_THAN_EQUAL = 'gte';
const OPERATOR_LOWER_THAN = 'lt';
const OPERATOR_LOWER_THAN_EQUAL = 'lte';

const SELECT_OPERATOR = 'select_operator';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -50,7 +44,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
parent::setDefaultOptions($resolver);

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

$transformerId = function (Options $options) {
Expand All @@ -59,19 +53,20 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

$resolver
->setDefaults(array(
'condition_operator' => self::OPERATOR_EQUAL,
'condition_operator' => FilterOperands::OPERATOR_EQUAL,
'compound' => $compound,
'number_options' => array(
'required' => false,
),
'choice_options' => array(
'choices' => self::getOperatorChoices(),
'choices' => FilterOperands::getNumberOperandsChoices(),
'required' => false,
),
'transformer_id' => $transformerId,
))
->setAllowedValues(array(
'transformer_id' => array('lexik_form_filter.transformer.text','lexik_form_filter.transformer.default'),
'transformer_id' => array('lexik_form_filter.transformer.text','lexik_form_filter.transformer.default'),
'condition_operator' => FilterOperands::getNumberOperands(true),
))
;
}
Expand All @@ -91,23 +86,4 @@ public function getName()
{
return 'filter_number';
}

/**
* Retruns an array of available conditions operator.
*
* @return array
*/
static public function getOperatorChoices()
{
$choices = array();

$reflection = new \ReflectionClass(__CLASS__);
foreach ($reflection->getConstants() as $name => $value) {
if ('OPERATOR_' === substr($name, 0, 9)) {
$choices[$value] = strtolower(str_replace(array('OPERATOR_', '_'), array('', ' '), $name));
}
}

return $choices;
}
}
6 changes: 4 additions & 2 deletions Filter/Extension/Type/NumberRangeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand Down Expand Up @@ -38,8 +40,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

$resolver
->setDefaults(array(
'left_number' => array('condition_operator' => NumberFilterType::OPERATOR_GREATER_THAN_EQUAL),
'right_number' => array('condition_operator' => NumberFilterType::OPERATOR_LOWER_THAN_EQUAL),
'left_number' => array('condition_operator' => FilterOperands::OPERATOR_GREATER_THAN_EQUAL),
'right_number' => array('condition_operator' => FilterOperands::OPERATOR_LOWER_THAN_EQUAL),
'transformer_id' => 'lexik_form_filter.transformer.value_keys',
))
->setAllowedValues(array(
Expand Down
41 changes: 7 additions & 34 deletions Filter/Extension/Type/TextFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@

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

use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\OptionsResolver\Options;

//@todo remove constants this namespace
use Lexik\Bundle\FormFilterBundle\Filter\ORM\Expr;

/**
* Filter type for strings.
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
class TextFilterType extends AbstractFilterType
{
const PATTERN_EQUALS = Expr::STRING_EQ;
const PATTERN_START_WITH = Expr::STRING_STARTS;
const PATTERN_END_WITH = Expr::STRING_ENDS;
const PATTERN_CONTAINS = Expr::STRING_BOTH;

const SELECT_PATTERN = 'select_pattern';

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


if (true === $options['compound']) {
$builder->add('condition_pattern', 'choice', $options['choice_options']);
$builder->add('text', 'text', $options['text_options']);
Expand All @@ -50,7 +41,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
parent::setDefaultOptions($resolver);

$compound = function (Options $options) {
return $options['condition_pattern'] == TextFilterType::SELECT_PATTERN;
return $options['condition_pattern'] == FilterOperands::OPERAND_SELECTOR;
};

$transformerId = function (Options $options) {
Expand All @@ -59,20 +50,21 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

$resolver
->setDefaults(array(
'condition_pattern' => self::PATTERN_EQUALS,
'condition_pattern' => FilterOperands::STRING_EQUALS,
'compound' => $compound,
'text_options' => array(
'required' => false,
'trim' => true,
),
'choice_options' => array(
'choices' => self::getConditionChoices(),
'choices' => FilterOperands::getStringOperandsChoices(),
'required' => false,
),
'transformer_id' => $transformerId,
))
->setAllowedValues(array(
'transformer_id' => array('lexik_form_filter.transformer.text','lexik_form_filter.transformer.default'),
'transformer_id' => array('lexik_form_filter.transformer.text','lexik_form_filter.transformer.default'),
'condition_pattern' => FilterOperands::getStringOperands(true),
))
;
}
Expand All @@ -92,23 +84,4 @@ public function getName()
{
return 'filter_text';
}

/**
* Retruns an array of available conditions patterns.
*
* @return array
*/
static public function getConditionChoices()
{
$choices = array();

$reflection = new \ReflectionClass(__CLASS__);
foreach ($reflection->getConstants() as $name => $value) {
if ('PATTERN_' === substr($name, 0, 8)) {
$choices[$value] = strtolower(str_replace(array('PATTERN_', '_'), array('', ' '), $name));
}
}

return $choices;
}
}
111 changes: 111 additions & 0 deletions Filter/FilterOperands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Lexik\Bundle\FormFilterBundle\Filter;

/**
* This class aim to regroup constants used in form filter types and in expression classes.
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
final class FilterOperands
{
const OPERATOR_EQUAL = 'eq';
const OPERATOR_GREATER_THAN = 'gt';
const OPERATOR_GREATER_THAN_EQUAL = 'gte';
const OPERATOR_LOWER_THAN = 'lt';
const OPERATOR_LOWER_THAN_EQUAL = 'lte';

const STRING_STARTS = 1;
const STRING_ENDS = 2;
const STRING_EQUALS = 3;
const STRING_BOTH = 4;

const OPERAND_SELECTOR = 'selection';

private function __construct()
{
}

/**
* Returns all available number operands.
*
* @param boolean $includeSelector
* @return array
*/
public static function getNumberOperands($includeSelector = false)
{
$values = array(
self::OPERATOR_EQUAL,
self::OPERATOR_GREATER_THAN,
self::OPERATOR_GREATER_THAN_EQUAL,
self::OPERATOR_LOWER_THAN,
self::OPERATOR_LOWER_THAN_EQUAL,
);

if ($includeSelector) {
$values[] = self::OPERAND_SELECTOR;
}

return $values;
}

/**
* Returns all available string operands.
*
* @param boolean $includeSelector
* @return array
*/
public static function getStringOperands($includeSelector = false)
{
$values = array(
self::STRING_STARTS,
self::STRING_ENDS,
self::STRING_EQUALS,
self::STRING_BOTH,
);

if ($includeSelector) {
$values[] = self::OPERAND_SELECTOR;
}

return $values;
}

/**
* Retruns an array of available conditions operator for numbers.
*
* @return array
*/
public static function getNumberOperandsChoices()
{
$choices = array();

$reflection = new \ReflectionClass(__CLASS__);
foreach ($reflection->getConstants() as $name => $value) {
if ('OPERATOR_' === substr($name, 0, 9)) {
$choices[$value] = strtolower(str_replace(array('OPERATOR_', '_'), array('', ' '), $name));
}
}

return $choices;
}

/**
* Retruns an array of available conditions patterns for string.
*
* @return array
*/
public static function getStringOperandsChoices()
{
$choices = array();

$reflection = new \ReflectionClass(__CLASS__);
foreach ($reflection->getConstants() as $name => $value) {
if ('STRING_' === substr($name, 0, 7)) {
$choices[$value] = strtolower(str_replace(array('STRING_', '_'), array('', ' '), $name));
}
}

return $choices;
}
}
Loading

0 comments on commit 9c01c14

Please sign in to comment.