Exemple #1
0
 /**
  * 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);
 }
Exemple #2
0
 /**
  * @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]);
 }
Exemple #3
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Tarsana\Syntax\Factory as S;
// a repo is an object having a name (string) and stars (number), separated by ':'
$repo = "{:,name,#stars}";
// a line consists of a first and last names, optional number of followers, and repos, separated by space. The repos are separated by ","
$line = "{ ,first_name,last_name,[#followers],[repos{$repo}[,]]}";
// a document is a list of lines separated by PHP_EOL
$document = "{$line}[" . PHP_EOL . "]";
// Now we make the syntax object
$documentSyntax = S::fromString($document);
// 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);
Exemple #4
0
 /**
  * @expectedException Tarsana\Syntax\Exceptions\DumpException
  */
 public function test_dump_wrong_boolean()
 {
     $syntax = S::boolean();
     $this->assertFalse($syntax->canDump('boolean'));
     $syntax->dump('boolean');
 }
Exemple #5
0
<?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);
Exemple #6
0
 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];
 }
Exemple #7
0
 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'));
 }
Exemple #8
0
 protected function parseObject($text)
 {
     $default = null;
     if (F\head($text) == '[' && F\last($text) == ']') {
         $text = F\init(F\tail($text));
         $default = '';
     }
     $results = [];
     $count = preg_match_all('/^([a-zA-Z_-]*)\\{([^,a-zA-Z0-9\\[]+)?,?(.*)\\}$/', $text, $results);
     if ($count < 1) {
         return null;
     }
     $fields = [];
     if (trim($results[3][0]) != '') {
         $fields = F\chunks('(){}[]""', $this->fieldsSeparator, $results[3][0]);
         $fields = F\reduce(function ($results, $item) {
             $item = $this->doParse(trim($item));
             $results[$item->description()] = $item;
             return $results;
         }, [], $fields);
     }
     $separator = $results[2][0];
     if (empty($separator)) {
         $separator = $this->objectSeparator;
     }
     return S::obj($fields, $separator, $default, $results[1][0]);
 }