Skip to content

Commit

Permalink
Merge branch 'master' of github.com:BrandEmbassy/query-language-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
janveselynet committed May 15, 2021
2 parents 2e03713 + d5920b2 commit 1cecb7f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/Car/QueryLanguage/CarColorQueryLanguageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use BrandEmbassy\QueryLanguageParser\Operator\EqualTo\QueryLanguageFieldSupportingEqualToOperator;
use BrandEmbassy\QueryLanguageParser\Operator\In\QueryLanguageFieldSupportingInOperator;
use BrandEmbassy\QueryLanguageParser\Operator\IsNotNull\QueryLanguageFieldSupportingIsNotNullOperator;
use BrandEmbassy\QueryLanguageParser\Operator\IsNull\QueryLanguageFieldSupportIsNullOperator;
use BrandEmbassy\QueryLanguageParser\Operator\IsNull\QueryLanguageFieldSupportingIsNullOperator;
use BrandEmbassy\QueryLanguageParser\Operator\NotEqualTo\QueryLanguageFieldSupportingNotEqualToOperator;
use BrandEmbassy\QueryLanguageParser\Operator\NotIn\QueryLanguageFieldSupportingNotInOperator;
use BrandEmbassy\QueryLanguageParser\Value\MultipleValuesExpressionParserCreator;
Expand All @@ -22,7 +22,7 @@ final class CarColorQueryLanguageField
QueryLanguageFieldSupportingNotEqualToOperator,
QueryLanguageFieldSupportingInOperator,
QueryLanguageFieldSupportingNotInOperator,
QueryLanguageFieldSupportIsNullOperator,
QueryLanguageFieldSupportingIsNullOperator,
QueryLanguageFieldSupportingIsNotNullOperator
{
public function getFieldIdentifier(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Operator/IsNull/IsNullQueryLanguageOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function createOperatorParser(): MonoParser

public function isFieldSupported(QueryLanguageField $field): bool
{
return $field instanceof QueryLanguageFieldSupportIsNullOperator;
return $field instanceof QueryLanguageFieldSupportingIsNullOperator;
}


public function createFieldExpressionParser(QueryLanguageField $field): MonoParser
{
assert($field instanceof QueryLanguageFieldSupportIsNullOperator);
assert($field instanceof QueryLanguageFieldSupportingIsNullOperator);

return new ConcParser(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use BrandEmbassy\QueryLanguageParser\Field\QueryLanguageField;

interface QueryLanguageFieldSupportIsNullOperator extends QueryLanguageField
interface QueryLanguageFieldSupportingIsNullOperator extends QueryLanguageField
{
/**
* @param mixed $fieldName output of field name parser
Expand Down
30 changes: 30 additions & 0 deletions src/Value/TextValueParserCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace BrandEmbassy\QueryLanguageParser\Value;

use Ferno\Loco\GrammarException;
use Ferno\Loco\MonoParser;
use Ferno\Loco\RegexParser;
use Nette\StaticClass;
use function str_replace;

final class TextValueParserCreator
{
use StaticClass;

public const TEXT_VALUE_PARSER = '/^(["]{1}[^"]+["]{1})|([\']{1}[^\']+[\']{1})/';


/**
* @throws GrammarException
*/
public static function create(): MonoParser
{
return new RegexParser(
self::TEXT_VALUE_PARSER,
static function ($value): string {
return str_replace(['"', '\''], '', (string)$value);
}
);
}
}
71 changes: 71 additions & 0 deletions src/Value/TextValueParserCreatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace BrandEmbassy\QueryLanguageParser\Value;

use Ferno\Loco\ParseFailureException;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

final class TextValueParserCreatorTest extends TestCase
{
/**
* @dataProvider validTextValueProvider
*/
public function testParsingValidTextValue(string $expectedParsedResult, string $valueToParse): void
{
$parser = TextValueParserCreator::create();

$actualValue = $parser->parse($valueToParse);

Assert::assertSame($expectedParsedResult, $actualValue);
}


/**
* @return string[][]
*/
public function validTextValueProvider(): array
{
return [
[
'expectedParsedResult' => 'hello world',
'valueToParse' => '"hello world"',
],
[
'expectedParsedResult' => 'foobar',
'valueToParse' => '\'foobar\'',
],
[
'expectedParsedResult' => '1234',
'valueToParse' => '"1234"',
],
];
}


/**
* @dataProvider invalidTextValueProvider
*/
public function testParsingInvalidTextValue(string $valueToParse): void
{
$parser = TextValueParserCreator::create();

$this->expectException(ParseFailureException::class);

$parser->parse($valueToParse);
}


/**
* @return string[][]
*/
public function invalidTextValueProvider(): array
{
return [
['valueToParse' => 'hello world'],
['valueToParse' => '"foo bar'],
['valueToParse' => 'foo bar\''],
['valueToParse' => '"foo bar\''],
];
}
}

0 comments on commit 1cecb7f

Please sign in to comment.