-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:BrandEmbassy/query-language-parser
- Loading branch information
Showing
5 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\''], | ||
]; | ||
} | ||
} |