-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (CamelConverter): 蛇形命名和驼峰互相转换,拆分功能
- Loading branch information
1 parent
35763d8
commit 483512e
Showing
3 changed files
with
80 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
// +---------------------------------------------------------------------- | ||
// | 通用类包 | ||
// +---------------------------------------------------------------------- | ||
// | Copyright (c) 2020 http://www.hmall.com.cn All rights reserved. | ||
// +---------------------------------------------------------------------- | ||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
// +---------------------------------------------------------------------- | ||
// | Author: joyecZhang <[email protected]> | ||
// +---------------------------------------------------------------------- | ||
|
||
declare (strict_types=1); | ||
|
||
namespace JoyceZ\LaravelLib\Helpers; | ||
|
||
|
||
use Illuminate\Support\Str; | ||
|
||
class CamelHelper | ||
{ | ||
/** | ||
* 循环迭代将数组键驼峰转下划线 | ||
* @param $arr | ||
* @return array | ||
*/ | ||
public static function recursiveConvertParameterNameCase($arr) | ||
{ | ||
if (!is_array($arr)) { | ||
return $arr; | ||
} | ||
$params = []; | ||
foreach ($arr as $key => $value) { | ||
if (!is_int($key)) { | ||
if (is_array($value)) { | ||
$params[Str::snake($key)] = self::recursiveConvertParameterNameCase($value); | ||
} else { | ||
$params[Str::snake($key)] = $value; | ||
} | ||
} elseif (is_array($value)) { | ||
$params[$key] = self::recursiveConvertParameterNameCase($value); | ||
} else { | ||
$params[Str::snake($key)] = $value; | ||
} | ||
} | ||
return $params; | ||
} | ||
|
||
/** | ||
* 循环迭代将数组键转换位驼峰 | ||
* @param $arr | ||
* @return array | ||
*/ | ||
public static function recursiveConvertNameCaseToCamel($arr) | ||
{ | ||
if (!is_array($arr)) { | ||
return $arr; | ||
} | ||
$outArray = []; | ||
foreach ($arr as $key => $value) { | ||
if (!is_int($key)) { | ||
if (is_array($value)) { | ||
$outArray[Str::camel($key)] = self::recursiveConvertNameCaseToCamel($value); | ||
} else { | ||
$outArray[Str::camel($key)] = $value; | ||
} | ||
} elseif (is_array($value)) { | ||
$outArray[$key] = self::recursiveConvertNameCaseToCamel($value); | ||
} else { | ||
$outArray[Str::camel($key)] = $value; | ||
} | ||
} | ||
return $outArray; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters