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: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
<?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 DataTypeParser
{
const SYNTAX_DEFAULT = 0;
const SYNTAX_ALTERNATIVE = 1;
const PARSER_CONTEXT = 0;
const PARSER_TOKEN = 1;
const CONTEXT_DEFAULT = 0;
const CONTEXT_ARGS = 1;
const CONTEXT_ALT_ARGS = 2;
const CONTEXT_FINISH = 3;
const TOKEN_DEFAULT = 0;
const TOKEN_ARGUMENT = 1;
public function __construct()
{
throw new StaticClassException('This is static class.');
}
public static function parse($s, $syntax = self::SYNTAX_DEFAULT)
{
$type = NULL;
$parameters = array();
$options = array();
$delimiters = array(
self::CONTEXT_DEFAULT => array(
' ' => array(self::TOKEN_DEFAULT, self::CONTEXT_DEFAULT),
"\t" => array(self::TOKEN_DEFAULT, self::CONTEXT_DEFAULT),
"\n" => array(self::TOKEN_DEFAULT, self::CONTEXT_DEFAULT),
"\r" => array(self::TOKEN_DEFAULT, self::CONTEXT_DEFAULT),
'(' => array(self::TOKEN_DEFAULT, self::CONTEXT_ARGS),
),
self::CONTEXT_ARGS => array(
',' => array(self::TOKEN_ARGUMENT, self::CONTEXT_ARGS),
')' => array(self::TOKEN_ARGUMENT, self::CONTEXT_DEFAULT),
),
self::CONTEXT_FINISH => array(
self::CONTEXT_DEFAULT => self::TOKEN_DEFAULT,
self::CONTEXT_ARGS => self::TOKEN_ARGUMENT,
),
);
if ($syntax & self::SYNTAX_ALTERNATIVE) {
$delimiters[self::CONTEXT_DEFAULT][':'] = array(self::TOKEN_DEFAULT, self::CONTEXT_ALT_ARGS);
$delimiters[self::CONTEXT_ALT_ARGS] = array(
',' => array(self::TOKEN_ARGUMENT, self::CONTEXT_ALT_ARGS),
' ' => array(self::TOKEN_ARGUMENT, self::CONTEXT_DEFAULT),
"\t" => array(self::TOKEN_ARGUMENT, self::CONTEXT_DEFAULT),
"\n" => array(self::TOKEN_ARGUMENT, self::CONTEXT_DEFAULT),
"\r" => array(self::TOKEN_ARGUMENT, self::CONTEXT_DEFAULT),
);
$delimiters[self::CONTEXT_FINISH][self::CONTEXT_ALT_ARGS] = self::TOKEN_ARGUMENT;
}
$findParameters = TRUE;
$inParameters = FALSE;
$tokens = self::process($s, self::CONTEXT_DEFAULT, $delimiters);
$knownOptions = array(
SqlSchema\Column::OPTION_UNSIGNED => TRUE,
SqlSchema\Column::OPTION_ZEROFILL => TRUE,
);
foreach ($tokens as $token) {
if ($token[0] === self::TOKEN_ARGUMENT) {
if (!$findParameters) {
throw new InvalidArgumentException('Malformed parameters definition.');
}
$inParameters = TRUE;
$value = trim($token[1]);
$resValue = (int) $value;
if ($value !== (string) $resValue) {
$resValue = $value;
}
if (self::isQuoted($resValue)) {
$resValue = substr($resValue, 1, -1);
}
$parameters[] = $resValue;
} else {
if ($inParameters) {
$inParameters = FALSE;
$findParameters = FALSE;
}
$upperToken = strtoupper($token[1]);
if (isset($knownOptions[$upperToken])) {
$options[] = $upperToken;
continue;
}
if ($type === NULL) {
$type = $upperToken;
} else {
$options[] = $upperToken;
}
}
}
return new DataType(
$type,
!empty($parameters) ? $parameters : NULL,
$options
);
}
private static function process($s, $context, array $delimiters)
{
$result = array();
$pos = 0;
$length = strlen($s);
for ($i = 0; $i < $length; $i++) {
$char = $s[$i];
if (isset($delimiters[$context][$char])) {
$token = substr($s, $pos, $i - $pos);
$pos = $i + 1;
$action = $delimiters[$context][$char];
if (is_array($action)) {
if ($token !== '') {
$result[] = array($action[0], $token);
}
$context = $action[1];
}
}
}
if ($pos !== $i && isset($delimiters[self::CONTEXT_FINISH][$context])) {
$token = substr($s, $pos, $i - $pos);
if ($token !== '') {
$result[] = array($delimiters[self::CONTEXT_FINISH][$context], $token);
}
}
return $result;
}
private static function isQuoted($s)
{
if ($s === '') {
return FALSE;
}
if ($s[0] !== '\'' && $s[0] !== '"') {
return FALSE;
}
return $s[0] === substr($s, -1);
}
}