/** * @expectedException Tarsana\Syntax\Exceptions\DumpException */ public function test_dump_wrong_item() { $syntax = S::arr(S::number()); $this->assertFalse($syntax->canDump([12, 32, 'wrong', 87])); $syntax->dump([12, 32, 'wrong', 87]); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Tarsana\Syntax\Factory as S; // Define the syntax of a repository part $repoSyntax = S::obj(['name' => S::string(), 'stars' => S::number()], ':'); // fields are separated by a ':' // Define the syntax of a line $lineSyntax = S::obj(['first_name' => S::string(), 'last_name' => S::string(), 'followers' => S::number(0), 'repos' => S::arr($repoSyntax, ',', [])], ' '); // fields are separated with ' ' // Now the syntax of the whole document $documentSyntax = S::arr($lineSyntax, PHP_EOL); // it's simply an array of lines separated by end-of-line characters. // Then we can use the defined syntax to parse the document: $developers = $documentSyntax->parse(trim(file_get_contents(__DIR__ . '/files/devs.txt'))); echo json_encode($developers);
/** * @expectedException Tarsana\Syntax\Exceptions\DumpException */ public function test_dump_wrong_field() { $syntax = S::obj(['name' => S::string(), 'age' => S::number(), 'is_programmer' => S::boolean(), 'friends' => S::arr()]); $object = (object) ['name' => 'Foo', 'age' => 'weird', 'is_programmer' => false, 'friends' => ['Bar', 'Baz']]; $this->assertFalse($syntax->canDump($object)); $syntax->dump($object); }
/** * @expectedException Tarsana\Syntax\Exceptions\DumpException */ public function test_dump_wrong_boolean() { $syntax = S::number(); $this->assertFalse($syntax->canDump('nan')); $syntax->dump('nan'); }
public function test_dump_object_with_optional_fields() { $this->checkDump('{ ,[name]}', S::obj(['name' => S::string('')], ' ')->description('')); $this->checkDump('person{:,name,[#age],friends[,]}', S::obj(['name' => S::string(), 'age' => S::number(''), 'friends' => S::arr()->description(',')], ':')->description('person')); }
protected function parseNumber($text) { $default = null; if (F\head($text) == '[' && F\last($text) == ']') { $text = F\init(F\tail($text)); $default = ''; } return S::number($default, F\tail($text)); }