think\Loader::parseName PHP Method

parseName() public static method

字符串命名风格转换 type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
public static parseName ( string $name, integer $type ) : string
$name string 字符串
$type integer 转换类型
return string
    public static function parseName($name, $type = 0)
    {
        if ($type) {
            return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {
                return strtoupper($match[1]);
            }, $name));
        } else {
            return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
        }
    }

Usage Example

Example #1
0
 /**
  * 利用__call方法实现一些特殊的Model方法
  * @access public
  * @param string $method 方法名称
  * @param array $args 调用参数
  * @return mixed
  */
 public function __call($method, $args)
 {
     if (in_array(strtolower($method), ['count', 'sum', 'min', 'max', 'avg'], true)) {
         // 统计查询的实现
         $field = isset($args[0]) ? $args[0] : '*';
         return $this->getField(strtoupper($method) . '(' . $field . ') AS tp_' . $method);
     } elseif (strtolower(substr($method, 0, 5)) == 'getby') {
         // 根据某个字段获取记录
         $field = Loader::parseName(substr($method, 5));
         $where[$field] = $args[0];
         return $this->where($where)->find();
     } elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
         // 根据某个字段获取记录的某个值
         $name = Loader::parseName(substr($method, 10));
         $where[$name] = $args[0];
         return $this->where($where)->getField($args[1]);
     } elseif (isset($this->scope[$method])) {
         // 命名范围的单独调用支持
         return $this->scope($method, $args[0]);
     } else {
         throw new \think\Exception(__CLASS__ . ':' . $method . Lang::get('_METHOD_NOT_EXIST_'));
         return;
     }
 }
All Usage Examples Of think\Loader::parseName