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:
<?php
namespace Inlm\SchemaGenerator\Utils;
use CzProject\SqlSchema;
use Inlm\SchemaGenerator\DataType;
use Inlm\SchemaGenerator\DuplicatedException;
use Inlm\SchemaGenerator\InvalidArgumentException;
use Inlm\SchemaGenerator\MissingException;
use Inlm\SchemaGenerator\StaticClassException;
class DataTypeProcessor
{
public function __construct()
{
throw new StaticClassException('This is static class.');
}
public static function process($inputType, DataType $dbType = NULL, $isPrimaryColumn = FALSE, array $customTypes = array(), $databaseType = NULL)
{
$type = NULL;
$parameters = array();
$options = array();
if ($inputType !== NULL) {
$inputType = strtolower($inputType);
if (isset($customTypes[$inputType])) {
$columnType = $customTypes[$inputType];
$type = $columnType->getType();
$parameters = $columnType->getParameters();
$options = $columnType->getOptions();
} elseif ($inputType === 'integer' || $inputType === 'int') {
$type = 'INT';
if ($isPrimaryColumn) {
$options[] = SqlSchema\Column::OPTION_UNSIGNED;
}
} elseif ($inputType === 'boolean' || $inputType === 'bool') {
$type = 'TINYINT';
$parameters = array(1);
$options[] = SqlSchema\Column::OPTION_UNSIGNED;
} elseif ($inputType === 'float' || $inputType === 'double') {
$type = 'DOUBLE';
} elseif ($inputType === 'string') {
$type = 'TEXT';
} elseif ($inputType === 'datetime' || $inputType === 'datetimeinterface') {
$type = 'DATETIME';
}
}
if ($dbType !== NULL) {
if ($dbType->getType() !== NULL) {
$type = $dbType->getType();
$lowerType = strtolower($type);
if (isset($customTypes[$lowerType])) {
$columnType = $customTypes[$lowerType];
$type = $columnType->getType();
$parameters = $columnType->getParameters();
$options = $columnType->getOptions();
}
}
if ($dbType->getParameters() !== NULL) {
$parameters = $dbType->getParameters();
}
foreach ($dbType->getOptions() as $k => $v) {
if (is_int($k)) {
$options[$v] = NULL;
} else {
$options[$k] = $v;
}
}
}
if ($type === NULL) {
throw new MissingException('Missing type.');
}
return new DataType($type, $parameters, $options);
}
}