Example #1
0
 public function parse($str, $callback)
 {
     if (!$str) {
         return false;
     }
     $pattern = new Pattern();
     call_user_func_array($callback, [$pattern]);
     preg_match_all($pattern->build(), $str, $m);
     $keys = $pattern->getKeys();
     array_shift($m);
     $rows = [];
     foreach ($m as $param => $values) {
         $key = $keys[$param];
         foreach ($values as $num => $value) {
             if (!isset($rows[$num])) {
                 $rows[$num] = new \stdClass();
             }
             $rows[$num]->{$key} = $value;
         }
     }
     return count($rows) === 1 ? reset($rows) : $rows;
 }