/** * {@inheritdoc} */ public function apply(DataSourceInterface $dataSource, $name, $data, array $options) { $expressionBuilder = $dataSource->getExpressionBuilder(); if (is_array($data) && !isset($data['type'])) { $data['type'] = isset($options['type']) ? $options['type'] : self::TYPE_CONTAINS; } if (!is_array($data)) { $data = ['type' => self::TYPE_CONTAINS, 'value' => $data]; } $fields = array_key_exists('fields', $options) ? $options['fields'] : [$name]; $type = $data['type']; $value = array_key_exists('value', $data) ? $data['value'] : null; if (!in_array($type, [self::TYPE_NOT_EMPTY, self::TYPE_EMPTY], true) && '' === trim($value)) { return; } if (1 === count($fields)) { $dataSource->restrict($this->getExpression($expressionBuilder, $type, current($fields), $value)); return; } $expressions = []; foreach ($fields as $field) { $expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value); } $dataSource->restrict($expressionBuilder->orX(...$expressions)); }
function it_does_not_filters_when_data_id_is_not_defined(DataSourceInterface $dataSource, ExpressionBuilderInterface $expressionBuilder) { $dataSource->getExpressionBuilder()->willReturn($expressionBuilder); $expressionBuilder->equals('entity', Argument::any())->shouldNotBeCalled(); $dataSource->restrict(Argument::any())->shouldNotBeCalled(); $this->apply($dataSource, 'entity', '', []); }
function it_filters_false_boolean_values(DataSourceInterface $dataSource, ExpressionBuilderInterface $expressionBuilder) { $dataSource->getExpressionBuilder()->willReturn($expressionBuilder); $expressionBuilder->equals('enabled', false)->willReturn('EXPR'); $dataSource->restrict('EXPR')->shouldBeCalled(); $this->apply($dataSource, 'enabled', BooleanFilter::FALSE, []); }
/** * {@inheritdoc} */ public function apply(DataSourceInterface $dataSource, $name, $data, array $options) { if (empty($data)) { return; } $field = isset($options['field']) ? $options['field'] : $name; $dataSource->restrict($dataSource->getExpressionBuilder()->equals($field, $data)); }
/** * {@inheritdoc} */ public function apply(DataSourceInterface $dataSource, $name, $data, array $options) { if (empty($data)) { return; } $fields = isset($options['fields']) ? $options['fields'] : [$name]; $expressionBuilder = $dataSource->getExpressionBuilder(); $expressions = []; foreach ($fields as $field) { $expressions[] = $expressionBuilder->equals($field, $data); } $dataSource->restrict($expressionBuilder->orX(...$expressions)); }
/** * {@inheritdoc} */ public function apply(DataSourceInterface $dataSource, $name, $data, array $options) { $expressionBuilder = $dataSource->getExpressionBuilder(); if (!is_array($data)) { $data = ['type' => self::TYPE_CONTAINS, 'value' => $data]; } $fields = array_key_exists('fields', $options) ? $options['fields'] : [$name]; $type = $data['type']; $value = array_key_exists('value', $data) ? $data['value'] : null; if (1 === count($fields)) { $expression = $this->getExpression($expressionBuilder, $type, $name, $value); } else { $expressions = []; foreach ($fields as $field) { $expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value); } $expression = $expressionBuilder->orX($expressions); } $dataSource->restrict($expression); }
function it_does_not_ignore_filter_if_its_value_is_zero(DataSourceInterface $dataSource, ExpressionBuilderInterface $expressionBuilder) { $dataSource->getExpressionBuilder()->willReturn($expressionBuilder); $expressionBuilder->like('firstName', '%0%')->willReturn('EXPR'); $dataSource->restrict('EXPR')->shouldBeCalled(); $this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_CONTAINS, 'value' => '0'], []); }
function it_filters_translation_fields(DataSourceInterface $dataSource, ExpressionBuilderInterface $expressionBuilder) { $dataSource->getExpressionBuilder()->willReturn($expressionBuilder); $expressionBuilder->like('translation.name', '%John%')->willReturn('EXPR'); $dataSource->restrict('EXPR')->shouldBeCalled(); $this->apply($dataSource, 'name', 'John', ['fields' => ['translation.name']]); }
function it_filters_in_multiple_fields(DataSourceInterface $dataSource, ExpressionBuilderInterface $expressionBuilder) { $dataSource->getExpressionBuilder()->willReturn($expressionBuilder); $expressionBuilder->like('firstName', '%John%')->willReturn('EXPR1'); $expressionBuilder->like('lastName', '%John%')->willReturn('EXPR2'); $expressionBuilder->orX(['EXPR1', 'EXPR2'])->willReturn('EXPR'); $dataSource->restrict('EXPR')->shouldBeCalled(); $this->apply($dataSource, 'name', 'John', ['fields' => ['firstName', 'lastName']]); }