Skip to content

Instantly share code, notes, and snippets.

@charlycoste
Created September 22, 2013 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlycoste/6658634 to your computer and use it in GitHub Desktop.
Save charlycoste/6658634 to your computer and use it in GitHub Desktop.
<?php
class Table
{
private $name;
public function __construct( $name )
{
$this->name = $name;
}
public function __toString()
{
return empty($this->name) ? "vide" : "\"{$this->name}\"";
}
}
class Database
{
private $tables = array();
private $references = array();
public function addTable( Table $t )
{
return in_array($t, $this->tables ) ? false : $this->tables[] = $t;
}
public function addReference( Reference $r )
{
return in_array($r, $this->references ) ? false : $this->references[] = $r;
}
public function __toString()
{
$result = "digraph a{\n";
foreach( $this->tables as $key=>$table )
{
$result .= "table{$key}[label=\"$table\"];\n";
//$result .= "$table;\n";
}
foreach( $this->references as $key=>$reference )
{
//$result = "table{$key}[label=\"$table\"];\n";
$result .= "$reference;\n";
}
return $result."}";
}
}
class Reference
{
private $table1;
private $table2;
public function __construct( Table $t1, Table $t2 )
{
$this->table1 = $t1;
$this->table2 = $t2;
}
public function __toString()
{
return "{$this->table1} -> {$this->table2}";
}
}
$database = new Database();
foreach( $schema as $key=>$table )
{
if($key == "_info") {
continue;
}
$currentTable = new Table( $table['name']);
$database->addTable( $currentTable );
if(is_array($table['fields']))
{
foreach( $table['fields'] as $name=>$field )
{
$pos = strrpos( $name, '_id');
if( $pos !== false )
{
$foreignTable = new Table( substr($name,0, -3) );
$database->addTable( $foreignTable );
$database->addReference( new Reference( $currentTable, $foreignTable) );
}
}
}
}
echo $database;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment