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

[Core] Fix wrong exported StringQuery with order #315

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 11 additions & 6 deletions lib/Core/Exporter/StringExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ public function exportCondition(SearchCondition $condition): string
{
$this->fields = $this->resolveLabels($fieldSet = $condition->getFieldSet());

return $this->exportOrder($condition, $fieldSet) . $this->exportGroup($condition->getValuesGroup(), $fieldSet, true);
$valuesGroup = $condition->getValuesGroup();
$result = '';

if ($valuesGroup->countValues() > 0 && $valuesGroup->getGroupLogical() === ValuesGroup::GROUP_LOGICAL_OR) {
$result .= '* ';
}

$result .= $this->exportOrder($condition, $fieldSet);

return trim($result . $this->exportGroup($valuesGroup, $fieldSet, true));
}

abstract protected function resolveLabels(FieldSet $fieldSet): array;
Expand All @@ -59,18 +68,14 @@ protected function exportOrder(SearchCondition $condition, FieldSet $fieldSet):
$result .= ': ' . $this->modelToExported($direction, $fieldSet->get($name)) . '; ';
}

return trim($result);
return ltrim($result);
}

protected function exportGroup(ValuesGroup $valuesGroup, FieldSet $fieldSet, bool $isRoot = false): string
{
$result = '';
$exportedGroups = '';

if ($isRoot && $valuesGroup->countValues() > 0 && $valuesGroup->getGroupLogical() === ValuesGroup::GROUP_LOGICAL_OR) {
$result .= '*';
}

foreach ($valuesGroup->getFields() as $name => $values) {
if ($fieldSet->isPrivate($name) || $values->count() === 0) {
continue;
Expand Down
1 change: 0 additions & 1 deletion lib/Core/Exporter/StringQueryExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Rollerworks\Component\Search\Field\FieldConfig;
use Rollerworks\Component\Search\FieldSet;
use Rollerworks\Component\Search\SearchCondition;

/**
* Exports the SearchCondition as StringQuery string.
Expand Down
2 changes: 2 additions & 0 deletions lib/Core/Test/SearchConditionExporterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

/**
* @author Sebastiaan Stok <[email protected]>
*
* @internal This class is not covered by the BC promise.
*/
abstract class SearchConditionExporterTestCase extends SearchIntegrationTestCase
{
Expand Down
27 changes: 27 additions & 0 deletions lib/Core/Tests/Exporter/StringQueryExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ public function it_exports_with_ordering_aliases(): void
$this->assertConditionEquals('@id: desc; @status: asc;', $condition, $processor, $config);
}

/** @test */
public function it_exports_with_ordering_and_root_group_logical(): void
{
$exporter = $this->getExporter();
$fieldSet = $this->getFieldSet(false)
->add('@id', OrderFieldType::class)
->add('@status', OrderFieldType::class)
->getFieldSet()
;

$config = new ProcessorConfig($fieldSet);

$condition = new SearchCondition(
$config->getFieldSet(),
(new ValuesGroup(ValuesGroup::GROUP_LOGICAL_OR))->addField('name', (new ValuesBag())->addSimpleValue('value '))
);

$orderGroup = (new ValuesGroup())
->addField('@id', (new ValuesBag())->addSimpleValue('DESC'))
->addField('@status', (new ValuesBag())->addSimpleValue('ASC'))
;
$condition->setOrder(new SearchOrder($orderGroup));

$this->assertExportEquals('* @id: desc; @status: asc; name: "value ";', $exporter->exportCondition($condition));
$this->assertConditionEquals('* @id: desc; @status: asc; name: "value ";', $condition, $this->getInputProcessor(), $config);
}

public function provideSingleValuePairTest()
{
return 'name: "value ", -value2, value2-, 10.00, "10,00", hÌ, ٤٤٤٦٥٤٦٠٠, "doctor""who""""", !value3; price: € 12.00, "12,00 $", $ 12.00;';
Expand Down
Loading