/** * Exports a search condition. * * @param SearchConditionInterface $condition The search condition to export * @param bool $useFieldAlias Use the localized field-alias * instead of the actual name (default false) * * @throws \RuntimeException * * @return mixed */ public function exportCondition(SearchConditionInterface $condition, $useFieldAlias = false) { $labelResolver = $this->labelResolver; if (!$useFieldAlias && $this->labelResolver instanceof NoopLabelResolver) { $this->labelResolver = new NoopLabelResolver(); } $result = $this->exportGroup($condition->getValuesGroup(), $condition->getFieldSet(), true); // Restore original resolver. $this->labelResolver = $labelResolver; return $result; }
/** * Serialize a search condition. * * The returned value is an array, you should serialize it yourself. * This is not done already because storing a serialized SearchCondition * in a php session would serialize the serialized result again. * * @param SearchConditionInterface $searchCondition * * @throws InvalidArgumentException when the FieldSet of the search condition * is not registered in the FieldSetRegistry * * @return array [FieldSet-name, serialized ValuesGroup object] */ public function serialize(SearchConditionInterface $searchCondition) { $setName = $searchCondition->getFieldSet()->getSetName(); if (!$this->fieldSetRegistry->has($setName)) { throw new InvalidArgumentException(sprintf('FieldSet "%s" is not registered in the FieldSetRegistry, ' . 'you should register the FieldSet before serializing the search condition.', $setName)); } return [$setName, serialize($searchCondition->getValuesGroup())]; }
/** * Constructor. * * @param SearchConditionInterface $searchCondition SearchCondition object * @param EntityManagerInterface $entityManager */ public function __construct(SearchConditionInterface $searchCondition, EntityManagerInterface $entityManager) { if ($searchCondition->getValuesGroup()->hasErrors(true)) { throw new BadMethodCallException('Unable to generate the where-clause, because the SearchCondition contains errors.'); } $this->searchCondition = $searchCondition; $this->fieldset = $searchCondition->getFieldSet(); $this->entityManager = $entityManager; $this->fieldsConfig = new FieldConfigBuilder($entityManager, $this->fieldset); }
/** * Validate the SearchCondition using the configured validation * constraints. * * If the condition has invalid values the constraintViolations * are converted to `Rollerworks\Component\Search\ValuesError` objects * and added to the related ValuesBag object. * * @param SearchConditionInterface $condition * * @return bool Returns true when the condition is valid * false otherwise */ public function validate(SearchConditionInterface $condition) { if ($condition->getValuesGroup()->hasErrors(true)) { return; } if ($condition->getValuesGroup()->isDataLocked()) { throw new \RuntimeException('Unable to validate locked ValuesGroup.'); } $group = $condition->getValuesGroup(); $this->validateValuesGroup($group, $condition->getFieldSet()); return !$group->hasErrors(true); }
/** * {@inheritdoc} */ public function process(SearchConditionInterface $condition) { if ($condition->getValuesGroup()->hasErrors(true)) { return; } krsort($this->optimizers, SORT_NUMERIC); foreach ($this->optimizers as $optimizers) { /** @var SearchConditionOptimizerInterface[] $optimizers */ foreach ($optimizers as $optimizer) { $optimizer->process($condition); } } }
/** * {@inheritdoc} */ public function process(SearchConditionInterface $condition) { $fieldSet = $condition->getFieldSet(); $supportsRanges = false; foreach ($fieldSet->all() as $field) { if ($field->supportValueType(ValuesBag::VALUE_TYPE_RANGE)) { $supportsRanges = true; break; } } // None of the fields supports ranges so don't optimize. if (!$supportsRanges) { return; } $this->normalizeRangesInGroup($condition->getValuesGroup(), $fieldSet); }
/** * Creates a new WhereBuilder for the SearchCondition. * * Conversions are applied using the 'doctrine_dbal_conversion' option (when present). * * @param Connection $connection Doctrine DBAL Connection object * @param SearchConditionInterface $searchCondition SearchCondition object * * @return WhereBuilder */ public function createWhereBuilder(Connection $connection, SearchConditionInterface $searchCondition) { $whereBuilder = new WhereBuilder($connection, $searchCondition); foreach ($searchCondition->getFieldSet()->all() as $name => $field) { if (!$field->hasOption('doctrine_dbal_conversion')) { continue; } $conversion = $field->getOption('doctrine_dbal_conversion'); // Lazy loaded if ($conversion instanceof \Closure) { $conversion = $conversion(); } $whereBuilder->setConverter($name, $conversion); } return $whereBuilder; }
/** * Exports the SearchCondition. * * @param SearchConditionInterface $condition The SearchCondition to export * @param bool $useFieldAlias Use the localized field-alias instead * of the actual name (default false) * @param bool $formatOutput Set whether to format the output (default true) * * @throws \RuntimeException * * @return string */ public function exportCondition(SearchConditionInterface $condition, $useFieldAlias = false, $formatOutput = true) { $labelResolver = $this->labelResolver; if (!$useFieldAlias && $this->labelResolver instanceof NoopLabelResolver) { $this->labelResolver = new NoopLabelResolver(); } $this->document = new \DOMDocument('1.0', 'utf-8'); $this->document->formatOutput = $formatOutput; $searchRoot = $this->document->createElement('search'); $searchRoot->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $searchRoot->setAttribute('xsi:schemaLocation', 'http://rollerworks.github.io/search/input/schema/search http://rollerworks.github.io/schema/search/xml-input-1.0.xsd'); $searchRoot->setAttribute('logical', $condition->getValuesGroup()->getGroupLogical()); $this->exportGroupNode($searchRoot, $condition->getValuesGroup(), $condition->getFieldSet()); $this->document->appendChild($searchRoot); $xml = $this->document->saveXML(); $this->document = null; // Restore original resolver. $this->labelResolver = $labelResolver; return $xml; }
/** * {@inheritdoc} */ public function process(SearchConditionInterface $condition) { $fieldSet = $condition->getFieldSet(); $valuesGroup = $condition->getValuesGroup(); $optimize = false; // Check if the optimization should be performed. // And builds the comparators. foreach ($fieldSet->all() as $name => $field) { $comparison = $field->getValueComparison(); if ($comparison instanceof ValueIncrementerInterface && $field->supportValueType(ValuesBag::VALUE_TYPE_RANGE)) { $this->comparators[$name] = new ValueSortCompare($comparison, $field->getOptions()); $optimize = true; } } // None of the fields supports ranges or value-increments so don't optimize. if (!$optimize) { return; } $this->optimizeValuesInGroup($valuesGroup, $fieldSet); }
/** * Returns the generated where-clause. * * The Where-clause is wrapped inside a group so it * can be safely used with other conditions. * * Values are embedded with in the Query. * * @param string $prependQuery Prepends this string to the where-clause * (" WHERE " or " AND " for example) * * @return string */ public function getWhereClause($prependQuery = '') { if (null === $this->whereClause) { $fields = $this->processFields(); $queryGenerator = new QueryGenerator($this->connection, $this->getQueryPlatform($fields), $fields); $this->whereClause = $queryGenerator->getGroupQuery($this->searchCondition->getValuesGroup()); } if ('' !== $this->whereClause) { return $prependQuery . $this->whereClause; } return ''; }
/** * Creates a new WhereBuilder for the SearchCondition. * * Conversions are applied using the 'doctrine_dbal_conversion' option (when present). * * @param NativeQuery|Query $query Doctrine ORM (Native)Query object * @param SearchConditionInterface $searchCondition SearchCondition object * * @return NativeWhereBuilder|WhereBuilder */ public function createWhereBuilder($query, SearchConditionInterface $searchCondition) { if ($query instanceof NativeQuery) { $whereBuilder = new NativeWhereBuilder($query, $searchCondition); } elseif ($query instanceof Query) { $whereBuilder = new WhereBuilder($query, $searchCondition); } else { throw new \InvalidArgumentException(sprintf('Query "%s" is not supported by the DoctrineOrmFactory.', get_class($query))); } foreach ($searchCondition->getFieldSet()->all() as $name => $field) { if (!$field->hasOption('doctrine_dbal_conversion')) { continue; } $conversion = $field->getOption('doctrine_dbal_conversion'); // Lazy loaded if ($conversion instanceof \Closure) { $conversion = $conversion(); } $whereBuilder->setConverter($name, $conversion); } return $whereBuilder; }
/** * {@inheritdoc} */ public function process(SearchConditionInterface $condition) { $this->removeDuplicatesInGroup($condition->getValuesGroup(), $condition->getFieldSet()); }