Skip to content

Instantly share code, notes, and snippets.

@favrik
Created July 21, 2009 02:25
Show Gist options
  • Save favrik/151061 to your computer and use it in GitHub Desktop.
Save favrik/151061 to your computer and use it in GitHub Desktop.
<?php
class MorpheusParser {
private $Form = array();
private $group_cursor = 0;
public function __construct() {
}
private function getFile($input) {
if (!empty($input) and file_exists($input)) {
return file($input);
}
}
private function isComment ($line) {
if (!$line) {
return false;
}
if ($line[0] == '#') {
return true;
}
return false;
}
private function isBlankLine($line) {
return $line === '';
}
private function parseAttributes($line) {
$result = array();
$attribs = explode(' ', $line);
foreach ($attribs as $attrib) {
$current = explode('=', $attrib);
$result[array_shift($current)] = array_shift($current);
}
return $result;
}
private function addForm($lines, $start) {
$params = explode(' ', $lines[$start]);
$params_number = count($params);
if ($params_number < 2) {
throw new Exception('"form:" directive requires, at least, one line
with two parameters separated by a space.');
}
$this->Form['layout'] = array_shift($params);
$this->Form['method'] = array_shift($params);
if ($params_number === 3) {
$this->Form['action'] = array_shift($params);
}
$position = $start + 1;
$attrib_line = $lines[$start + 1];
if (!$this->isBlankLine($attrib_line)) {
$position = $start + 2;
$this->Form['attrib'] = $this->parseAttributes($attrib_line);
}
return $position;
}
private function addLegend($legend) {
$this->Form['groups'] = array();
$this->Form['groups'][$this->group_cursor]['legend'] = $legend;
$this->Form['groups'][$this->group_cursor]['fields'] = array();
}
private function isAttributeLine($line) {
return strpos($line, '=') !== false;
}
private function addGroupAttributes($attribs) {
$this->Form['groups'][$this->group_cursor]['attrib'] =
$this->parseAttributes($attribs);
}
private function isFieldStart($line) {
$pos = strpos($line, ':');
if ($pos !== false and $line !== 'group:' and $line !== 'form:') {
return $pos;
}
return false;
}
private function addFields($lines, $start) {
$fields = array();
for ($i = $start; $i < count($lines); $i++) {
if ($pos = $this->isFieldStart($lines[$i])) {
$name = substr($lines[$i], 0, $pos);
$fields[$name]['name'] = $name;
$fields[$name]['type'] =
$fields[$name]['label'] =
$fields[$name
}
}
return $i;
}
private function addGroup($lines, $start) {
$legend = $lines[$start];
if (empty($legend)) {
throw new Exception('"legend" cannot be empty when adding a group.');
}
$this->addLegend($legend);
if ($this->isAttributeLine($lines[$start + 1])) {
$this->addGroupAttributes($lines[$start + 1]);
}
return $this->addFields($lines, $start + 2);
}
private function parse($file) {
$lines = $this->getFile($file);
$lines = array_map('trim', $lines);
for ($i = 0; $i < count($lines); $i++) {
$line = $lines[$i];
// Fail sooner
if ($this->isBlankLine($line) or $this->isComment($line)) {
continue;
}
if ($line == 'form:') {
$i = $this->addForm($lines, $i + 1);
}
if ($line == 'group:') {
$i = $this->addGroup($lines, $i + 1);
}
continue;
}
var_dump($this->Form);
return $this->Form;
}
public static function load($file) {
$parser = new MorpheusParser();
return $parser->parse($file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment