From fd3f8a25e3be0e391c8fd486ccfcffe913ade534 Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Fri, 8 Sep 2017 06:21:25 +0200 Subject: [PATCH] added `syntax` to syntax string rules --- README.md | 6 +++++- src/ObjectSyntax.php | 2 +- src/SyntaxSyntax.php | 6 ++++++ tests/SyntaxSyntaxTest.php | 5 +++++ tests/TestCase.php | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 936a67e..cf8c5db 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,7 @@ $personSyntax = S::syntax()->parse('{name, age:number, vip:boolean, friends:[]}' - `S::string()` is `string`. - `S::number()` is `number`. - `S::boolean()` is `boolean`. +- `S::syntax()` is `syntax`. - `S::optional($type, $default)` is `(type:default)` where `type` is the string corresponding to `$type` and `default` is `json_encode($default)`. - `S::array($type, $separator)` is `[type|separator]` where`type` is the string corresponding to `$type` and `separator` is the same as `$separator`. If the separator is omitted (ie. `[type]`); the default value is `,`. t)`. @@ -388,7 +389,10 @@ S::object([ # Development Notes & Next Steps -- **version 2.0** +- **version 2.1.0** + - `syntax` added to the string representation of a syntax and corresponds to the `S::syntax()` instance. + +- **version 2.0.0** - Separators and default values can be specified when creating syntax from string. - Escaping separators is now possible. diff --git a/src/ObjectSyntax.php b/src/ObjectSyntax.php index bc42e23..4f31714 100644 --- a/src/ObjectSyntax.php +++ b/src/ObjectSyntax.php @@ -233,7 +233,7 @@ public function parse(string $text) : \stdClass "Additional items with no corresponding fields", $extra); } - return (object) array_map(function($field) { + return (object) array_map(function(\stdClass $field) { return $field->value; }, $this->values); } diff --git a/src/SyntaxSyntax.php b/src/SyntaxSyntax.php index ca55ac9..91c4995 100644 --- a/src/SyntaxSyntax.php +++ b/src/SyntaxSyntax.php @@ -68,6 +68,9 @@ public function parse(string $text) : Syntax if ($text === 'boolean') return S::boolean(); + if ($text === 'syntax') + return S::syntax(); + $length = strlen($text); if ($length >= 2) { $wrappers = substr($text, 0, 1) . substr($text, -1); @@ -142,6 +145,9 @@ public function dump($value) : string if ($value instanceof BooleanSyntax) return 'boolean'; + if ($value instanceof SyntaxSyntax) + return 'syntax'; + if ($value instanceof ArraySyntax) { $array = [ 'type' => $this->dump($value->syntax()), diff --git a/tests/SyntaxSyntaxTest.php b/tests/SyntaxSyntaxTest.php index ef21efb..1b52cca 100644 --- a/tests/SyntaxSyntaxTest.php +++ b/tests/SyntaxSyntaxTest.php @@ -55,6 +55,11 @@ public function test_optional_number() { $this->dump($syntax, '(number:false)'); } + public function test_syntax() { + $this->parse('syntax', S::syntax()); + $this->dump(S::syntax(), 'syntax'); + } + public function test_array() { $syntax = S::array(); $this->parse('[]', $syntax); diff --git a/tests/TestCase.php b/tests/TestCase.php index cf5c15a..d5fe0e1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,7 +16,7 @@ public static function assertEquals($expected, $actual, $message = '', $delta = if ($expected instanceof Syntax && $actual instanceof Syntax) { if (get_class($expected) != get_class($actual)) throw new \Exception("'{$expected}' and '{$actual}' are not equal"); - if ($expected instanceof StringSyntax || $expected instanceof BooleanSyntax || $expected instanceof NumberSyntax) + if ($expected instanceof StringSyntax || $expected instanceof BooleanSyntax || $expected instanceof NumberSyntax || $expected instanceof SyntaxSyntax) return; if ($expected instanceof OptionalSyntax) return $this->assertEquals($expected->syntax(), $actual->syntax())