Skip to content
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
5 changes: 5 additions & 0 deletions lib/Core/Input/ConditionStructureBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public function __construct(
$this->maxGroups = $config->getMaxGroups();
$this->valuesGroupLevels[0] = new ValuesGroup();
$this->path[] = $path;

// Future compatible, this is not part of the current API.
if (method_exists($validator, 'initialize')) {
$validator->initialize($config);
}
}

public function getErrors(): ErrorList
Expand Down
5 changes: 5 additions & 0 deletions lib/Core/Input/OrderStructureBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function __construct(
) {
$this->fieldSet = $config->getFieldSet();
$this->valuesGroup = new ValuesGroup();

// Future compatible, this is not part of the current API.
if (method_exists($validator, 'initialize')) {
$validator->initialize($config);
}
}

public function getErrors(): ErrorList
Expand Down
4 changes: 2 additions & 2 deletions lib/Core/Input/StringInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ final protected function parse(ProcessorConfig $config, string $input, FieldSet
/** @var FieldConfig $field */
foreach ($fieldSet->all() as $name => $field) {
if (OrderField::isOrder($name) && null !== $direction = $field->getOption('default')) {
$this->orderStructureBuilder->field($name, '[order][%s]');
$this->orderStructureBuilder->simpleValue($direction, '');
$this->orderStructureBuilder->field($name, '');
$this->orderStructureBuilder->simpleValue($direction, '[order][{pos}]');
$this->orderStructureBuilder->endValues();
}
}
Expand Down
31 changes: 28 additions & 3 deletions lib/Core/Input/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,47 @@
use Rollerworks\Component\Search\Field\FieldConfig;

/**
* The Validator validates input values according to a set of
* rules (constraints).
* The Validator validates field values.
*
* The validator is first initialized with the FieldConfig using the
* `initializeConfig()` method (when present).
*
* For each field the validator is then initialized with `initializeContext()`.
* Then for each value in the current field the `validate()` method is called.
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*
* @method void initialize(ProcessorConfig $config)
*/
interface Validator
{
/**
* Initialize the validator context for the field.
*
* Whenever calling validate(), this context needs to be used.
* This method is called prior to the first call to validate().
* Errors need to be added to the ErrorList.
*/
public function initializeContext(FieldConfig $field, ErrorList $errorList): void;

/**
* Validates and returns whether the value is valid.
*
* Any error (or violation) should be added to the ErrorList with the
* corresponding path. Multiple errors can be added for the same path.
*
* Example:
* ```
* // ErrorList was initialized in initializeContext()
*
* $this->errorList->append(new \Rollerworks\Component\Search\ConditionErrorMessage(
* $path,
* $violation->getMessage(),
* $violation->getMessageTemplate(),
* $violation->getParameters(),
* $violation->getPlural(),
* $violation, // Cause of the error (can be anything), optional used for debugging and profiling
* ));
* ```
*/
public function validate($value, string $type, $originalValue, string $path): bool;
}
128 changes: 128 additions & 0 deletions lib/Core/Tests/Input/JsonInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
namespace Rollerworks\Component\Search\Tests\Input;

use Rollerworks\Component\Search\ConditionErrorMessage;
use Rollerworks\Component\Search\ErrorList;
use Rollerworks\Component\Search\Exception\InvalidSearchConditionException;
use Rollerworks\Component\Search\Field\FieldConfig;
use Rollerworks\Component\Search\Input\JsonInput;
use Rollerworks\Component\Search\Input\ProcessorConfig;
use Rollerworks\Component\Search\Input\Validator;
use Rollerworks\Component\Search\InputProcessor;
use Rollerworks\Component\Search\Value\Compare;
use Rollerworks\Component\Search\Value\PatternMatch;
Expand Down Expand Up @@ -179,6 +182,131 @@ public static function provide_invalid_structures(): iterable
];
}

/**
* @test
*/
public function it_validates_values(): void
{
$validator = new class implements Validator {
private ?string $currentField = null;

/** @var array<string, mixed> */
public array $calls = [];

public function initialize(ProcessorConfig $config): void
{
$this->calls = [];
$this->calls['initialize'] = $config;
}

public function initializeContext(FieldConfig $field, ErrorList $errorList): void
{
$this->currentField = $field->getName();
$this->calls[$this->currentField] = [];
}

public function validate($value, string $type, $originalValue, string $path): bool
{
if ($value instanceof \DateTimeImmutable) {
$value = '{DateTime}' . $value->format('Y-m-d');
}

$this->calls[$this->currentField][] = [$value, $type, $originalValue, $path];

return false;
}
};

$processor = new JsonInput($validator);
$config = new ProcessorConfig($this->getFieldSet(order: true));

$processor->process(
$config,
json_encode(
[
'fields' => [
'name' => ['simple-values' => ['value', 'value2']],
],
'groups' => [
[
'fields' => [
'date' => [
'simple-values' => ['2014-12-16'],
'ranges' => [
['lower' => '2014-12-16', 'upper' => '2015-12-16'],
],
],
],
],
],
'order' => [
'id' => 'asc',
],
],
\JSON_THROW_ON_ERROR,
),
);

self::assertSame([
'initialize' => $config,
'name' => [
['value', 'simple', 'value', '[fields][name][simple-values][0]'],
['value2', 'simple', 'value2', '[fields][name][simple-values][1]'],
],
'date' => [
['{DateTime}2014-12-16', 'simple', '2014-12-16', '[groups][0][fields][date][simple-values][0]'],
['{DateTime}2014-12-16', Range::class, '2014-12-16', '[groups][0][fields][date][ranges][0][lower]'],
['{DateTime}2015-12-16', Range::class, '2015-12-16', '[groups][0][fields][date][ranges][0][upper]'],
],
'@id' => [
['ASC', 'simple', 'asc', '[order][@id]'],
],
], $validator->calls);

$processor->process(
$config,
json_encode(
[
'fields' => [
'name' => ['simple-values' => ['value3', 'value4']],
],
'groups' => [
[
'fields' => [
'date' => [
'simple-values' => ['2014-12-14'],
'ranges' => [
['lower' => '2014-12-16', 'upper' => '2015-12-16'],
],
],
],
],
],
'order' => [
'id' => 'asc',
],
],
\JSON_THROW_ON_ERROR,
),
);

self::assertSame([
'initialize' => $config,
'name' => [
['value3', 'simple', 'value3', '[fields][name][simple-values][0]'],
['value4', 'simple', 'value4', '[fields][name][simple-values][1]'],
],
'date' => [
['{DateTime}2014-12-14', 'simple', '2014-12-14', '[groups][0][fields][date][simple-values][0]'],
['{DateTime}2014-12-16', Range::class, '2014-12-16', '[groups][0][fields][date][ranges][0][lower]'],
['{DateTime}2015-12-16', Range::class, '2015-12-16', '[groups][0][fields][date][ranges][0][upper]'],
],
'@id' => [
['ASC', 'simple', 'asc', '[order][@id]'],
],
], $validator->calls);
}

public static function provideEmptyInputTests(): iterable
{
return [
Expand Down
75 changes: 75 additions & 0 deletions lib/Core/Tests/Input/StringQueryInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Tests\Input;

use Rollerworks\Component\Search\ConditionErrorMessage;
use Rollerworks\Component\Search\ErrorList;
use Rollerworks\Component\Search\Exception\InputProcessorException;
use Rollerworks\Component\Search\Exception\OrderStructureException;
use Rollerworks\Component\Search\Exception\StringLexerException;
Expand All @@ -23,6 +24,7 @@
use Rollerworks\Component\Search\Input\ProcessorConfig;
use Rollerworks\Component\Search\Input\StringLexer;
use Rollerworks\Component\Search\Input\StringQueryInput;
use Rollerworks\Component\Search\Input\Validator;
use Rollerworks\Component\Search\InputProcessor;
use Rollerworks\Component\Search\SearchCondition;
use Rollerworks\Component\Search\SearchConditionBuilder;
Expand Down Expand Up @@ -85,6 +87,79 @@ public function isEqual($value, $nextValue, array $options): bool
return $build ? $fieldSet->getFieldSet() : $fieldSet;
}

/**
* @test
*/
public function it_validates_values(): void
{
$validator = new class implements Validator {
private ?string $currentField = null;

/** @var array<string, mixed> */
public array $calls = [];

public function initialize(ProcessorConfig $config): void
{
$this->calls = [];
$this->calls['initialize'] = $config;
}

public function initializeContext(FieldConfig $field, ErrorList $errorList): void
{
$this->currentField = $field->getName();
$this->calls[$this->currentField] = [];
}

public function validate($value, string $type, $originalValue, string $path): bool
{
if ($value instanceof \DateTimeImmutable) {
$value = '{DateTime}' . $value->format('Y-m-d');
}

$this->calls[$this->currentField][] = [$value, $type, $originalValue, $path];

return false;
}
};

$processor = new StringQueryInput($validator, null);
$config = new ProcessorConfig($this->getFieldSet(order: true));

$processor->process($config, 'name: value, value2; (date: "12-16-2014", "12-16-2014" ~ "12-16-2015"); @id: asc;');
self::assertSame([
'initialize' => $config,
'name' => [
['value', 'simple', 'value', '[name][0]'],
['value2', 'simple', 'value2', '[name][1]'],
],
'date' => [
['{DateTime}2014-12-16', 'simple', '12-16-2014', '[0][date][0]'],
['{DateTime}2014-12-16', Range::class, '12-16-2014', '[0][date][1][lower]'],
['{DateTime}2015-12-16', Range::class, '12-16-2015', '[0][date][1][upper]'],
],
'@id' => [
['ASC', 'simple', 'asc', '[@id]'],
],
], $validator->calls);

$processor->process($config, 'name: value4, value3; (date: "12-16-2014", "12-16-2014" ~ "12-16-2015"); @id: asc;');
self::assertSame([
'initialize' => $config,
'name' => [
['value4', 'simple', 'value4', '[name][0]'],
['value3', 'simple', 'value3', '[name][1]'],
],
'date' => [
['{DateTime}2014-12-16', 'simple', '12-16-2014', '[0][date][0]'],
['{DateTime}2014-12-16', Range::class, '12-16-2014', '[0][date][1][lower]'],
['{DateTime}2015-12-16', Range::class, '12-16-2015', '[0][date][1][upper]'],
],
'@id' => [
['ASC', 'simple', 'asc', '[@id]'],
],
], $validator->calls);
}

/**
* @test
*
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ parameters:
identifier: argument.type
path: lib/Core/Tests/SearchConditionSerializerTest.php

-
# Forward compatible
message: '#^Call to function method_exists\(\) with Rollerworks\\Component\\Search\\Input\\Validator and ''initialize'' will always evaluate to true\.$#'
path: lib/Core/Input/*.php

- '#Call to an undefined static method Carbon\\Translator\:\:get\(\)#'
Loading