foreach ($items as $item) { if (!is_int($item)) { foreach (array("TYPE", "INPUT") as $p) { $C = "XBDATA_{$p}" . "_" . strtoupper($item); if (defined($C)) { $ret |= constant($C); continue 2; } } } else { $ret |= intval($item); } } return $ret; } /* LIBRARY:FUNCTION @name : bool @description : Получение булева значения @param : $v | | value | | Value @return : boolean | Булево представление значения */ public static function bool($v) { return strval($v) == 'true' || $v === true || intval($v) > 0; } } /* LIBRARY ~END */ define("XBDATA_TYPE_INTKEY", xbDataTypes::type('integer,notnull,autoincrement,primary')); /* INFO @copyright: Xander Bass, 2015 */
public static function correct($data, $name = '') { if (!is_array($data)) { return false; } $ret = $data; $ret['alias'] = $name; foreach (array('id', 'type', 'flags', 'length', 'default', 'elements', 'input', 'regexp', 'replace', 'strip', 'access', 'external', 'key') as $k) { if (!array_key_exists($k, $ret)) { $ret[$k] = null; } } // Коррекция типа $ret['type'] = is_null($ret['type']) ? 0 : xbDataTypes::type($ret['type']); $ret['default'] = xbDataTypes::value($ret['type'], $ret['default']); // Кеширование флагов типа $ret['primary'] = ($ret['type'] & XBDATA_TYPE_PRIMARY) != 0; $ret['null'] = ($ret['type'] & XBDATA_TYPE_NOTNULL) == 0; $ret['autoincrement'] = ($ret['type'] & XBDATA_TYPE_AUTOINCREMENT) != 0; $ret['unsigned'] = ($ret['type'] & XBDATA_TYPE_UNSIGNED) != 0; if ($ret['primary']) { $ret['null'] = false; } // Остальные параметры if (!is_int($ret['flags'])) { $ret['flags'] = self::flags($ret['flags']); } if (!is_null($ret['length'])) { $ret['length'] = abs(intval($ret['length'])); } if (!is_null($ret['elements'])) { if (is_string($ret['elements'])) { $q = strpos($ret['elements'], '@: ') === 0; $ret['elements'] = $q ? substr($ret['elements'], 3) : unserialize($ret['elements']); } } // Внешний ключ if (!is_null($ret['key'])) { $_ = explode('.', $ret['key']); $ret['key'] = array('table' => null, 'field' => null, 'update' => null, 'delete' => null); if (count($_) == 0) { $ret['key'] = null; } if (count($_) > 1) { $ret['key']['table'] = $_[0]; $_ = explode(':', $_[1]); $ret['key']['field'] = $_[0]; if (count($_) > 1) { $_ = explode('/', $_[1]); $ret['key']['update'] = empty($_[0]) ? null : $_[0]; $ret['key']['delete'] = count($_) > 1 ? empty($_[1]) ? null : $_[1] : null; } } else { $ret['key']['field'] = $_[0]; } if (!is_null($ret['key'])) { foreach (array('update', 'delete') as $k) { if (is_null($ret['key'][$k])) { $ret['key'][$k] = 'set null'; } } } } if (is_null($ret['input'])) { $ret['input'] = self::input($ret['type'], is_null($ret['elements'])); } // Дополнительные параметры $t = $ret['type'] & XBDATA_TYPE_VARIABLE; switch ($t) { case XBDATA_TYPE_FLOAT: case XBDATA_TYPE_INTEGER: foreach (array('min', 'max') as $dk) { if (!array_key_exists($dk, $ret)) { $ret[$dk] = null; } if (!is_null($ret[$dk])) { $ret[$dk] = $t == XBDATA_TYPE_FLOAT ? floatval($ret[$dk]) : intval($ret[$dk]); if ($ret['unsigned'] && $ret[$dk] <= 0) { $ret[$dk] = null; } } } if ($ret['min'] > $ret['max']) { list($ret['min'], $ret['max']) = array($ret['max'], $ret['min']); } $l = self::intLength($t, $ret['length']); $max = pow(256, $l) / ($ret['unsigned'] ? 2 : 1) - 1; $min = 0 - ($ret['unsigned'] ? 0 : $max + 1); if ($ret['min'] <= $min) { $ret['min'] = null; } if ($ret['max'] >= $max) { $ret['max'] = null; } break; } return $ret; }