1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134:
<?php
namespace Inlm\SchemaGenerator;
class ConfigurationSerializer
{
public function __construct()
{
throw new StaticClassException('This is static class.');
}
public static function serialize(Configuration $configuration)
{
$res = array();
$options = $configuration->getOptions();
if (!empty($options)) {
ksort($options, SORT_STRING);
$res['options'] = $options;
}
$schema = $configuration->getSchema();
$tables = array();
$_foreignKeys = array();
foreach ($schema->getTables() as $table) {
$table->validate();
$tableName = $table->getName();
if (isset($tables[$tableName])) {
throw new DuplicatedException("Duplicated table name '$tableName'.");
}
$definition = self::export(array(
'comment' => $table->getComment(),
), array('comment' => NULL));
foreach ($table->getColumns() as $column) {
$columnName = $column->getName();
$definition['columns'][$columnName] = self::export(array(
'type' => $column->getType(),
'parameters' => $column->getParameters(),
'options' => $column->getOptions(),
'nullable' => $column->isNullable(),
'autoIncrement' => $column->isAutoIncrement(),
'defaultValue' => $column->getDefaultValue(),
'comment' => $column->getComment(),
), array(
'parameters' => array(),
'options' => array(),
'nullable' => FALSE,
'autoIncrement' => FALSE,
'defaultValue' => NULL,
'comment' => NULL,
));
}
foreach ($table->getIndexes() as $index) {
$indexName = $index->getName();
$indexColumns = array();
foreach ($index->getColumns() as $indexColumn) {
$indexColumns[] = self::export(array(
'name' => $indexColumn->getName(),
'order' => $indexColumn->getOrder(),
'length' => $indexColumn->getLength(),
), array(
'order' => 'ASC',
'length' => NULL,
));
}
$definition['indexes'][$indexName] = array(
'type' => $index->getType(),
'columns' => $indexColumns,
);
}
foreach ($table->getForeignKeys() as $foreignKey) {
$foreignKeyName = $foreignKey->getName();
if (isset($_foreignKeys[$foreignKeyName])) {
throw new DuplicatedException("Duplicated foreign key '$foreignKeyName' in table '$tableName'.");
}
$definition['foreignKeys'][$foreignKeyName] = array(
'columns' => $foreignKey->getColumns(),
'targetTable' => $foreignKey->getTargetTable(),
'targetColumns' => $foreignKey->getTargetColumns(),
'onUpdateAction' => $foreignKey->getOnUpdateAction(),
'onDeleteAction' => $foreignKey->getOnDeleteAction(),
);
$_foreignKeys[$foreignKeyName] = TRUE;
}
foreach ($table->getOptions() as $optionName => $optionValue) {
$definition['options'][$optionName] = $optionValue;
}
isset($definition['indexes']) && ksort($definition['indexes'], SORT_STRING);
isset($definition['foreignKeys']) && ksort($definition['foreignKeys'], SORT_STRING);
isset($definition['options']) && ksort($definition['options'], SORT_STRING);
$tables[$tableName] = $definition;
}
if (!empty($tables)) {
ksort($tables, SORT_STRING);
$res['schema'] = $tables;
}
return $res;
}
private static function export(array $data, array $defaults = array())
{
foreach ($defaults as $key => $value) {
if ($data[$key] === $value) {
unset($data[$key]);
}
}
return $data;
}
}