PSR에 맞게 수정

This commit is contained in:
2018-04-06 19:03:35 +09:00
parent 37339b91b0
commit 2bce82c73e
18 changed files with 584 additions and 481 deletions
+69 -60
View File
@@ -1,30 +1,33 @@
<?php
namespace sammo;
class Util extends \utilphp\util{
public static function hashPassword($salt, $password){
class Util extends \utilphp\util
{
public static function hashPassword($salt, $password)
{
return hash('sha512', $salt.$password.$salt);
}
/**
* 변환할 내용이 _tK_$key_ 형태로 작성된 단순한 템플릿 파일을 이용하여 결과물을 생성해주는 함수.
*/
public static function generateFileUsingSimpleTemplate(string $srcFilePath, string $destFilePath, array $params, bool $canOverwrite=false){
if($destFilePath === $srcFilePath){
public static function generateFileUsingSimpleTemplate(string $srcFilePath, string $destFilePath, array $params, bool $canOverwrite=false)
{
if ($destFilePath === $srcFilePath) {
return 'invalid destFilePath';
}
if(!file_exists($srcFilePath)){
if (!file_exists($srcFilePath)) {
return 'srcFilePath is not exists';
}
if(file_exists($destFilePath) && !$canOverwrite){
if (file_exists($destFilePath) && !$canOverwrite) {
return 'destFilePath is already exists';
}
if(!is_writable(dirname($destFilePath))){
if (!is_writable(dirname($destFilePath))) {
return 'destFilePath is not writable';
}
$text = file_get_contents($srcFilePath);
foreach($params as $key => $value){
foreach ($params as $key => $value) {
$text = str_replace("_tK_{$key}_", $value, $text);
}
file_put_contents($destFilePath, $text);
@@ -39,28 +42,29 @@ class Util extends \utilphp\util{
* float -> int
* numeric(int, float) 포함 -> int
* 기타 -> 예외처리
*
*
* @return int|null
*/
public static function toInt($val, $silent=false){
if(!isset($val)){
public static function toInt($val, $silent=false)
{
if (!isset($val)) {
return null;
}
if($val === null){
if ($val === null) {
return null;
}
if(is_int($val)){
if (is_int($val)) {
return $val;
}
if(is_numeric($val)){
if (is_numeric($val)) {
return intval($val);//
}
if(strtolower($val) === 'null'){
if (strtolower($val) === 'null') {
return null;
}
if($silent){
if($val == null){
if ($silent) {
if ($val == null) {
return null;
}
return intval($val);
@@ -69,12 +73,12 @@ class Util extends \utilphp\util{
}
/**
* Generate a random string, using a cryptographically secure
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
@@ -90,18 +94,20 @@ class Util extends \utilphp\util{
return $str;
}
public static function mapWithDict($callback, $dict){
public static function mapWithDict($callback, $dict)
{
$result = [];
foreach(array_keys($dict) as $key){
foreach (array_keys($dict) as $key) {
$result[$key] = ($callback)($dict[$key]);
}
return $result;
}
public static function convertArrayToDict($arr, $keyName){
public static function convertArrayToDict($arr, $keyName)
{
$result = [];
foreach($arr as $obj){
foreach ($arr as $obj) {
$key = $obj[$keyName];
$result[$key] = $obj;
}
@@ -109,66 +115,65 @@ class Util extends \utilphp\util{
return $result;
}
public static function convertDictToArray($dict, $keys){
public static function convertDictToArray($dict, $keys)
{
$result = [];
foreach($keys as $key){
foreach ($keys as $key) {
$result[] = Util::array_get($dict[$key], null);
}
return $result;
}
public static function isDict(&$array){
if(!is_array($array)){
public static function isDict(&$array)
{
if (!is_array($array)) {
//배열이 아니면 dictionary 조차 아님.
return false;
}
$idx = 0;
$jmp = 0;
foreach ($array as $key=>&$value) {
if(is_string($key)){
if (is_string($key)) {
return true;
}
$jmp = $key - $idx - 1;
$idx = $key;
}
if ($jmp * 5 >= count($array)){
if ($jmp * 5 >= count($array)) {
//빈칸이 많으면 dictionary인걸로.
return true;
}
else{
} else {
return false;
}
}
public static function eraseNullValue($dict, $depth=512){
public static function eraseNullValue($dict, $depth=512)
{
//TODO:Test 추가
if($dict === null){
if ($dict === null) {
return null;
}
if(is_array($dict) && empty($dict)){
if (is_array($dict) && empty($dict)) {
return null;
}
if($depth <= 0){
if ($depth <= 0) {
return $dict;
}
foreach ($dict as $key=>$value) {
if($value === null){
if ($value === null) {
unset($dict[$key]);
}
else if(Util::isDict($value)){
} elseif (Util::isDict($value)) {
$newValue = Util::eraseNullValue($value, $depth - 1);
if($newValue === null){
if ($newValue === null) {
unset($dict[$key]);
}
else{
} else {
$dict[$key] = $newValue;
}
}
}
@@ -176,11 +181,12 @@ class Util extends \utilphp\util{
}
/**
/**
* 0.0~1.0 사이의 랜덤 float
* @return float
*/
public static function randF(){
public static function randF()
{
return mt_rand() / mt_getrandmax();
}
@@ -188,7 +194,8 @@ class Util extends \utilphp\util{
* $prob의 확률로 true를 반환
* @return boolean
*/
public static function randBool($prob = 0.5){
public static function randBool($prob = 0.5)
{
return self::randF() < $prob;
}
@@ -196,11 +203,12 @@ class Util extends \utilphp\util{
/**
* $min과 $max 사이의 값으로 교정
*/
public static function valueFit($value, $min, $max){
if($value < $min){
public static function valueFit($value, $min, $max)
{
if ($value < $min) {
return $min;
}
if($value > $max){
if ($value > $max) {
return $max;
}
return $value;
@@ -208,20 +216,21 @@ class Util extends \utilphp\util{
/**
* 각 값의 비중에 따라 랜덤한 값을 선택
*
*
* @param array $items 각 수치의 비중
*
*
* @return int|string 선택된 랜덤 값의 key값. 단순 배열인 경우에는 index
*/
public static function choiceRandomUsingWeight(array $items){
public static function choiceRandomUsingWeight(array $items)
{
$sum = 0;
foreach($items as $value){
foreach ($items as $value) {
$sum += $value;
}
$rd = self::randF()*$sum;
foreach($items as $key=>$value){
if($rd <= $value){
foreach ($items as $key=>$value) {
if ($rd <= $value) {
return $key;
}
$rd -= $value;
@@ -234,13 +243,13 @@ class Util extends \utilphp\util{
/**
* 배열의 아무거나 고름. Python의 random.choice()
*
*
* @param array $items 선택하고자 하는 배열
*
*
* @return object 선택된 value값.
*/
public static function choiceRandom(array $items){
public static function choiceRandom(array $items)
{
return $items[array_rand($items)];
}
};
};