数据库安全字段.

@param string $field

@return string
2946
public static function cleanField($field) {
        if ('*' == $field) {
            return $field;
        }
        $strings = explode('.', $field);
        $fields  = [];
        foreach ($strings as $str) {
            $strs = preg_split('#\bas\b#i', $str);
            $fs   = [];
            foreach ($strs as $s) {
                if ($s == '*') {
                    $fs [] = '*';
                } else {
                    $fs [] = '`' . trim(trim(trim($s), '`')) . '`';
                }
            }
            $fields [] = implode(' AS ', $fs);
        }

        return implode('.', $fields);
    }
1287623
protected function get($field)
    {
        if (isset($this->raw[$field])) {
            return $this->raw[$field];
        }
        if (preg_match('/^is_/', $field)) {
            return false;
        }

        return null;
    }
725505
public static function quoteFieldNames($fields)
    {
        $fieldArr = is_string($fields) ? explode(',', trim($fields)) : (array)$fields;
        
        foreach ($fieldArr as $k => $field) {
            $field = preg_replace("/(\s+)/", ' ', trim($field));
            if ($field == '*') {
                // do nothing
            } elseif (false !== strpos($field, '(')) {
                if (preg_match("/(.*?)\\((.*?)\\)(.*)/i", $field, $match)) {
                    // xx() | xx(xx) | xx(xx)xx
                    $field = $match[1] . '(' . QueryBuilder::quoteFieldName($match[2]).')' . QueryBuilder::quoteFieldName($match[3]);
                } else {
                    // xx( | xx(xx
                    $field = preg_replace_callback(
                        "/\\((.+)/i",
                        function($match) {
                            return '('.QueryBuilder::quoteFieldName($match[1]);
                        },
                        $field
                    );
                }

                $fieldArr[$k] = $field;
            } elseif (false !== strpos($field, ')')) {
                // xx)
                $field = preg_replace_callback(
                    "/(.*)\\)/i",
                    function($match) {
                        return QueryBuilder::quoteFieldName($match[1]) . ')';
                    },
                    $field
                );
                
                $fieldArr[$k] = QueryBuilder::quoteFieldName(substr($field, 0, strlen($field) - 1)) . ')';
            } else {
                $fieldArr[$k] = QueryBuilder::quoteFieldName($field);
            }
        }
        
        $rFields = implode(',', $fieldArr);        
        
        return $rFields;
    }
836936
protected function parseWhereItem($field, $val)
    {
        $key = $field ? $this->parseKey($field) : '';
        // 查询规则和条件
        if (!is_array($val)) {
            $val = ['=', $val];
        }
        list($exp, $value) = $val;

        // 对一个字段使用多个查询条件
        if (is_array($exp)) {
            $data = [];
            foreach ($val as $value) {
                $exp = $value[0];
                $value = $value[1];
                if (!in_array($exp, $this->exp)) {
                    $exp = strtolower($exp);
                    if (isset($this->exp[$exp])) {
                        $exp = $this->exp[$exp];
                    }
                }
                $k = '$' . $exp;
                $data[$k] = $value;
            }
            $query[$key] = $data;
            return $query;
        } elseif (!in_array($exp, $this->exp)) {
            $exp = strtolower($exp);
            if (isset($this->exp[$exp])) {
                $exp = $this->exp[$exp];
            }
//            else {
//                Debug::save(ROOT_PATH . 'error.log', [$exp, $this->exp]);
//                throw new Exception('where express error:' . $exp);
//            }
        }

        $query = [];
        if ('=' == $exp) {
            // 普通查询
            $query[$key] = $this->parseValue($value, $key);
        } elseif (in_array($exp, ['neq', 'ne', 'gt', 'egt', 'gte', 'lt', 'lte', 'elt', 'mod'])) {
            // 比较运算
            $k = '$' . $exp;
            $query[$key] = [$k => $this->parseValue($value, $key)];
        } elseif ('null' == $exp) {
            // NULL 查询
            $query[$key] = null;
        } elseif ('not null' == $exp) {
            $query[$key] = ['$ne' => null];
        } elseif ('all' == $exp) {
            // 满足所有指定条件
            $query[$key] = ['$all', $this->parseValue($value, $key)];
        } elseif ('between' == $exp) {
            // 区间查询
            $value = is_array($value) ? $value : explode(',', $value);
            $query[$key] = ['$gte' => $this->parseValue($value[0], $key), '$lte' => $this->parseValue($value[1], $key)];
        } elseif ('not between' == $exp) {
            // 范围查询
            $value = is_array($value) ? $value : explode(',', $value);
            $query[$key] = ['$lt' => $this->parseValue($value[0], $key), '$gt' => $this->parseValue($value[1], $key)];
        } elseif ('exists' == $exp) {
            // 字段是否存在
            $query[$key] = ['$exists' => (bool)$value];
        } elseif ('type' == $exp) {
            // 类型查询
            $query[$key] = ['$type' => intval($value)];
        } elseif ('exp' == $exp) {
            // 表达式查询
            $query['$where'] = $value instanceof Javascript ? $value : new Javascript($value);
        } elseif ('like' == $exp) {
            // 模糊查询 采用正则方式
            $query[$key] = $value instanceof Regex ? $value : new Regex($value, 'i');
        } elseif (in_array($exp, ['nin', 'in'])) {
            // IN 查询
            $value = is_array($value) ? $value : explode(',', $value);
            foreach ($value as $k => $val) {
                $value[$k] = $this->parseValue($val, $key);
            }
            $query[$key] = ['$' . $exp => $value];
        } elseif ('regex' == $exp) {
            $query[$key] = $value instanceof Regex ? $value : new Regex($value, 'i');
        } elseif ('< time' == $exp) {
            $query[$key] = ['$lt' => $this->parseDateTime($value, $field)];
        } elseif ('> time' == $exp) {
            $query[$key] = ['$gt' => $this->parseDateTime($value, $field)];
        } elseif ('between time' == $exp) {
            // 区间查询
            $value = is_array($value) ? $value : explode(',', $value);
            $query[$key] = ['$gte' => $this->parseDateTime($value[0], $field), '$lte' => $this->parseDateTime($value[1], $field)];
        } elseif ('not between time' == $exp) {
            // 范围查询
            $value = is_array($value) ? $value : explode(',', $value);
            $query[$key] = ['$lt' => $this->parseDateTime($value[0], $field), '$gt' => $this->parseDateTime($value[1], $field)];
        } elseif ('near' == $exp) {
            // 经纬度查询
            $query[$key] = ['$near' => $this->parseValue($value, $key)];
        } elseif ('size' == $exp) {
            // 元素长度查询
            $query[$key] = ['$size' => intval($value)];
        } else {
            // 普通查询
            $query[$key] = $this->parseValue($value, $key);
        }
        return $query;
    }
473606
public function getField($field)
    {
        $result = array_filter($this->getFields(), function($input) use ($field) {
            return $field === $input->getName();
        });

        return reset($result);
    }
1241463
public function getValFormatted($field)
    {
        $val = $field->valRaw();
        $num = \preg_replace('/\D/', '', $val);
        $parts = \sscanf($num, '%3s%3s%4s');
        return $parts
            ? \vsprintf('(%s) %s-%s', $parts)
            : '';
    }
964382
public static function camelCaseToSnakeCase(string $field): string
    {
        return strtolower(preg_replace(self::REGEX_CAPITAL, self::REPLACEMENT_UNDERSCORE, $field));
    }
364502
public function readonly($field)
    {
        if (is_string($field)) {
            $field = explode(',', $field);
        }

        $this->readonly = $field;

        return $this;
    }
1349794
protected function formatField(Field $field)
    {
        $jsonKey = $field->column();

        $elementName = $elementClass = $errorKey = [];

        if (is_array($jsonKey)) {
            foreach ($jsonKey as $index => $name) {
                $elementName[$index] = "{$this->column}[$name]";
                $errorKey[$index] = "{$this->column}.$name";
                $elementClass[$index] = "{$this->column}_$name";
            }
        } else {
            $elementName = "{$this->column}[$jsonKey]";
            $errorKey = "{$this->column}.$jsonKey";
            $elementClass = "{$this->column}_$jsonKey";
        }

        $field->setElementName($elementName)
            ->setErrorKey($errorKey)
            ->setElementClass($elementClass);

        return $field;
    }
1041386
public function remove_prefix($field) {
        if (preg_match('~^(-?_?)' . preg_quote($this->extraprefix, '~') . '(.*)$~', $field, $matches)) {
            return $matches[1] . $matches[2];
        } else {
            return null;
        }
    }
1335807
private function getFieldValue($field)
    {
        return isset($this->values[$field]) ? $this->values[$field] : null;
    }
1063344
protected function parseField($fields, $options = [])
    {
        $fieldsStr = '';
        if ('*' == $fields || empty($fields)) {
            $fieldsStr = '*';
        } elseif (is_array($fields)) {
            // 支持 'field1'=>'field2' 这样的字段别名定义
            $array = [];
            foreach ($fields as $key => $field) {
                if (!is_numeric($key)) {
                    $array[] = $this->parseKey($key, $options) . ' AS ' . $this->parseKey($field, $options);
                } else {
                    $array[] = $this->parseKey($field, $options);
                }
            }
            $fieldsStr = implode(',', $array);
        }
        return $fieldsStr;
    }
453798
public function quote_identifier($field) {
		$field = str_replace('`', '``', $field);
		$parts = explode('.', $field);

		foreach (array_keys($parts) as $k) {
			$parts[$k] = '`' . $parts[$k] . '`';
		}

		return implode('.', $parts);
	}
1350883
public function getFieldType($field)
    {
        return isset($this->getFieldsConfig()[$field]) ? $this->getFieldsConfig()[$field]['type'] : null;
    }
211589
public function undot($field)
    {
        if (is_string($field) && (($pos = strrpos($field, ".")) !== false)) {
            return [substr($field, 0, $pos), substr($field, $pos + 1)];
        }
        return ['', $field];
    }
241436
public function getField($field)
    {
        return $this->getFields()->filter(function($input) use ($field) {
            return $field === $input->getName();
        })->first();
    }
392239
protected function normalize($field)
    {
        $normal = array(
            'type' => 'string',
            'field' => array('text'),
            'valid' => array(),
            'label' => '',
            'holder' => '',
            'search' => false,
            'required' => false
        );

        if(isset($field['encoding'])) {
            $normal['encoding'] = $field['encoding'];
        }

        if(isset($field['type'])) {
            $normal['type'] = $field['type'];
        }

        if(isset($field['label'])) {
            $normal['label'] = $field['label'];
        }

        if(isset($field['holder'])) {
            $normal['holder'] = $field['holder'];
        }

        if(isset($field['search'])) {
            $normal['search'] = !!$field['search'];
        }

        if(isset($field['sample'])) {
            $normal['sample'] = $field['sample'];
        }

        if(isset($field['field'])) {
            if($field['field'] === false
                || is_array($field['field'])
            ) {
                $normal['field'] = $field['field'];
            } else if(is_string($field['field'])) {
                $normal['field'][0] = $field['field'];
            }
        }

        if(isset($field['valid'])) {
            if(is_array($field['valid'])) {
                $normal['valid'] = $field['valid'];
            } else if(is_string($field['valid'])) {
                $normal['valid'][] = array($field['valid']);
            }

            foreach($normal['valid'] as $i => $validation) {
                if(!is_array($validation)) {
                    $validation = array($validation);
                }

                $normal['valid'][$i] = $validation;

                if($validation[0] === 'required') {
                    $normal['required'] = true;
                }
            }
        }

        if(isset($field['default'])) {
            $normal['default'] = $field['default'];

            if(is_null($normal['default'])) {
                $normal['default'] = 'null';
            } else if($normal['type'] === 'int'
                && !$this('validation', $normal['default'])->isType('int', true)
            ) {
                $normal['default'] = '0';
                $normal['valid'][] = array('int');
            } else if($normal['type'] === 'float'
                && !$this('validation', $normal['default'])->isType('float', true)
            ) {
                $normal['default'] = '0.00';
                $normal['valid'][] = array('float');
            } else if($normal['type'] === 'boolean'
                && !$this('validation', $normal['default'])->isType('bool', true)
            ) {
                $normal['default'] = '0';
                $normal['valid'][] = array('bool');
            } else if($normal['type'] === 'datetime'
                && ($normal['default'] === 'now'
                || $normal['default'] === 'now()')
            ) {
                $normal['default'] = 'CURRENT_TIMESTAMP';
                $normal['valid'][] = array('datetime');
            } else if($normal['type'] === 'date') {
                $normal['valid'][] = array('date');
            } else if($normal['type'] === 'time') {
                $normal['valid'][] = array('time');
            } else if(is_string($normal['default'])) {
                $normal['default'] = "'".$normal['default']."'";
            }

            $normal['default'] = (string) $normal['default'];
        }

        if(isset($field['options']) && is_array($field['options'])) {
            $normal['options'] = array();

            foreach($field['options'] as $option) {
                if(is_string($option)) {
                    $normal['options'][] = array(
                        'value' => $option,
                        'label' => ucwords($option)
                    );

                    continue;
                }

                $normal['options'][] = $option;
            }

            if($field['field'] !== 'checkbox') {
                $valid = array();
                foreach($normal['options'] as $option) {
                    $valid[] = $option['value'];
                }

                if($normal['type'] !== 'file') {
                    $normal['valid'][] = array('one', $valid);
                }
            }
        }

        $validKeys = array();

        foreach($normal['valid'] as $check) {
            $validKeys[] = $check[0];
        }

        //some types should imply validation
        if(in_array($normal['type'], array(
            'bool',
            'date',
            'float',
            'int',
            'email',
            'url',
            'small'))
            && !in_array($normal['type'], $validKeys)
        ) {
            $normal['valid'][] = array($normal['type']);
        }

        //datetime as well
        if($normal['type'] === 'datetime' && !in_array('date', $validKeys)) {
            $normal['valid'][] = array('date');
        }

        if(!isset($normal['sample']) && isset($normal['default'])) {
            $normal['sample'] = $normal['default'];
        }

        if(!isset($normal['sample'])) {
            $sample = 'foobar';
            foreach($normal['valid'] as $valid) {
                switch($valid[0]) {
                    case 'required':
                    case 'empty':
                        break;
                    case 'one':
                        $sample = $valid[1][0];
                        break;
                    case 'email':
                        $sample = 'test@test.com';
                        break;
                    case 'hex':
                        $sample = '12321';
                        break;
                    case 'cc':
                        $sample = '4111111111111111';
                        break;
                    case 'html':
                        $sample = '<p>Awesome</p>';
                        break;
                    case 'url':
                        $sample = 'http://example.com';
                        break;
                    case 'slug':
                        $sample = 'asd-123';
                        break;
                    case 'json':
                        $sample = '{"error":false}';
                        break;
                    case 'date':
                        $sample = '2015-09-02';
                        break;
                    case 'time':
                        $sample = '12:01:00';
                        break;
                    case 'alphanum':
                        $sample = 'foo123';
                        break;
                    case 'alphanum-':
                        $sample = 'foo-123';
                        break;
                    case 'alphanum_':
                        $sample = 'foo_123';
                        break;
                    case 'alphanum-_':
                        $sample = 'foo-_123';
                        break;
                    case 'bool':
                        $sample = '1';
                        break;
                    case 'small':
                        $sample = '3';
                        break;
                    case 'int':
                        $sample = '3';
                        break;
                    case 'float':
                        $sample = '3.3';
                        break;
                    case 'price':
                        $sample = '3.30';
                        break;
                }
            }

            $normal['sample'] = "'".$sample."'";
        }

        return $normal;
    }