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
37 changes: 34 additions & 3 deletions lib/Core/Input/StringLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Rollerworks\Component\Search\Input;

use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\Exception\StringLexerException;

/**
Expand All @@ -26,7 +27,7 @@ final class StringLexer
public const COMPARE = 'compare';
public const RANGE = 'range';

/** @var array <string, \Closure> */
/** @var array<string, \Closure(self, string): string> */
private array $valueLexers;

private string $data;
Expand All @@ -39,11 +40,12 @@ final class StringLexer
private ?int $colSnapshot;
private ?int $cursorSnapshot;
private ?int $charSnapshot;
private ?string $inValue = null;

/**
* @internal
*
* @param array<string, \Closure> $fieldLexers
* @param array<string, \Closure(self, string): string> $fieldLexers
*/
public function parse(string $data, array $fieldLexers = []): void
{
Expand Down Expand Up @@ -327,9 +329,17 @@ public function fieldIdentification(): string
*/
public function valuePart(string $fieldName, string $allowedNext = ',;)'): string
{
if ($this->inValue) {
$this->throwForbiddenInternalOperation(__METHOD__);
}

// matches value syntax (with custom lexer) or string
if (isset($this->valueLexers[$fieldName])) {
return $this->valueLexers[$fieldName]($this, $allowedNext);
$this->inValue = $fieldName;
$value = $this->valueLexers[$fieldName]($this, $allowedNext);
$this->inValue = null;

return $value;
}

return $this->stringValue($allowedNext);
Expand All @@ -342,6 +352,10 @@ public function valuePart(string $fieldName, string $allowedNext = ',;)'): strin
*/
public function rangeValue(string $name): array
{
if ($this->inValue) {
$this->throwForbiddenInternalOperation(__METHOD__);
}

$lowerInclusive = ($this->matchOptional('/[[\]]/A') ?? '[') === '[';

$this->skipWhitespace();
Expand All @@ -367,6 +381,10 @@ public function rangeValue(string $name): array
*/
public function comparisonValue(string $name): array
{
if ($this->inValue) {
$this->throwForbiddenInternalOperation(__METHOD__);
}

$operator = $this->expects('/<>|(?:[<>]=?)/A', 'CompareOperator');

$this->skipWhitespace();
Expand All @@ -383,6 +401,10 @@ public function comparisonValue(string $name): array
*/
public function patternMatchValue(): array
{
if ($this->inValue) {
$this->throwForbiddenInternalOperation(__METHOD__);
}

$this->expects('~');

if ($this->cursor === $this->end) {
Expand Down Expand Up @@ -436,6 +458,10 @@ public function patternMatchValue(): array
*/
public function detectValueType(string $name): string
{
if ($this->inValue) {
$this->throwForbiddenInternalOperation(__METHOD__);
}

if ($this->cursor === $this->end) {
return '';
}
Expand Down Expand Up @@ -495,4 +521,9 @@ private function regexOrSingleChar(string $data): ?string

return null;
}

private function throwForbiddenInternalOperation(string $method): never
{
throw new BadMethodCallException(\sprintf('Cannot call "%s" inside a custom value-lexer for field "%s".', mb_substr($method, mb_strrpos($method, '::') + 2), $this->inValue));
}
}
31 changes: 31 additions & 0 deletions lib/Core/Tests/Input/StringLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Tests\Input;

use PHPUnit\Framework\TestCase;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\Exception\StringLexerException;
use Rollerworks\Component\Search\Input\StringLexer;

Expand Down Expand Up @@ -97,4 +98,34 @@ public function it_reports_the_correct_col_when_start_at_newline(): void

$this->lexer->stringValue();
}

/**
* @dataProvider provideInternalLexerMethods
*
* @test
*/
public function it_forbids_internal_lexer_methods_in_custom_value_lexer(string $method): void
{
$customLexer = static function (StringLexer $lexer, string $expectedNext) use ($method): string {
$lexer->{$method}('custom');

return 'error';
};

$this->lexer->parse('id: 12;', ['id' => $customLexer]);
$this->lexer->fieldIdentification();

$this->expectExceptionObject(new BadMethodCallException(\sprintf('Cannot call "%s" inside a custom value-lexer for field "%s".', $method, 'id')));

$this->lexer->valuePart('id');
}

public static function provideInternalLexerMethods(): iterable
{
yield ['valuePart'];
yield ['rangeValue'];
yield ['comparisonValue'];
yield ['patternMatchValue'];
yield ['detectValueType'];
}
}
Loading