Skip to content

Commit

Permalink
added syntax to syntax string rules
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Sep 8, 2017
1 parent b397749 commit fd3f8a2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectSyntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/SyntaxSyntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()),
Expand Down
5 changes: 5 additions & 0 deletions tests/SyntaxSyntaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit fd3f8a2

Please sign in to comment.