public function test_dump() { $syntax = S::arr(); $this->assertEquals('foo,bar,baz', $syntax->dump(['foo', 'bar', 'baz'])); $syntax = S::arr(S::string(), ':'); $this->assertEquals('foo,bar:baz', $syntax->dump(['foo,bar', 'baz'])); }
/** * Creates a new instance of ArraySyntax. * * @param Tarsana\Syntax\Syntax $syntax The syntax of each item of the array. * @param string $separator The string that separates items of the array. * @param string $default The default value. */ public function __construct($syntax = null, $separator = null, $default = null, $description = '') { if ($syntax === null) { $syntax = Factory::string(); } if ($separator === null || $separator == '') { $separator = ','; } $this->itemSyntax = $syntax; $this->separator = $separator; parent::__construct($default, $description); }
<?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);
protected function getComplexTestCase() { $text = "Student Agent Smart,Stupid\n" . "name:string:public age:int year:int:protected,get:1 " . "count:int:static,protected,get:0\n" . 'speak:void canLearn:bool:s|Subject|"math":protected'; $fullText = "Student Agent Smart,Stupid\n" . "name:string:public: age:int:private,get,set: year:int:protected,get:1 " . "count:int:static,protected,get:0\n" . 'speak:void::public canLearn:bool:s|Subject|"math":protected'; $syntax = S::obj(['names' => S::obj(['class_name' => S::string(), 'parents' => S::arr(S::string(), ',', []), 'interfaces' => S::arr(S::string(), ',', [])], ' '), 'attrs' => S::arr(S::obj(['name' => S::string(), 'type' => S::string(), 'flags' => S::arr(S::string(), ',', ['private', 'get', 'set']), 'default' => S::string('')]), ' '), 'methods' => S::arr(S::obj(['name' => S::string(), 'return' => S::string(), 'args' => S::arr(S::obj(['name' => S::string(), 'type' => S::string(), 'default' => S::string('')], '|'), ',', []), 'flags' => S::arr(S::string(), ',', ['public'])]), ' ')], "\n"); $object = (object) ['names' => (object) ['class_name' => 'Student', 'parents' => ['Agent'], 'interfaces' => ['Smart', 'Stupid']], 'attrs' => [(object) ['name' => 'name', 'type' => 'string', 'flags' => ['public'], 'default' => ''], (object) ['name' => 'age', 'type' => 'int', 'flags' => ['private', 'get', 'set'], 'default' => ''], (object) ['name' => 'year', 'type' => 'int', 'flags' => ['protected', 'get'], 'default' => '1'], (object) ['name' => 'count', 'type' => 'int', 'flags' => ['static', 'protected', 'get'], 'default' => '0']], 'methods' => [(object) ['name' => 'speak', 'return' => 'void', 'args' => [], 'flags' => ['public']], (object) ['name' => 'canLearn', 'return' => 'bool', 'args' => [(object) ['name' => 's', 'type' => 'Subject', 'default' => '"math"']], 'flags' => ['protected']]]]; return ['syntax' => $syntax, 'object' => $object, 'text' => $text, 'full_text' => $fullText]; }
public function test_dump_object_containing_objects() { $this->checkDump('user{ ,name,accounts{:,site,login,[pass]}[,]}', S::obj(['name' => S::string(), 'accounts' => S::arr(S::obj(['site' => S::string(), 'login' => S::string(), 'pass' => S::string('')], ':'), ',')], ' ')->description('user')); }
protected function parseString($text) { $default = null; if (F\head($text) == '[' && F\last($text) == ']') { $text = F\tail(F\init($text)); $default = ''; } return S::string($default, $text); }