From 5e603c0d4e990f6dd832eb36a24a5d71e4e19ab4 Mon Sep 17 00:00:00 2001 From: Putra Sudaryanto Date: Sun, 13 May 2018 00:31:53 +0700 Subject: [PATCH] add UtilityTrait nb. move urlTitle to him, flashMessage and uniqueCode --- FileTrait.php | 47 +---------------------- UtilityTrait.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 45 deletions(-) create mode 100644 UtilityTrait.php diff --git a/FileTrait.php b/FileTrait.php index c025193..898c17e 100644 --- a/FileTrait.php +++ b/FileTrait.php @@ -8,11 +8,7 @@ * @created date 17 April 2018, 08:36 WIB * @link https://github.com/ommu/yii2-traits * - * FileTrait berisi kumpulan fungsi yang berhubungan sistem file, seperti buat folder, hapus folder, - * normalisasi nama file sehingga url friendly dll. - * * Contains many function that most used : - * urlTitle * formatFileType * createUploadDirectory * @@ -20,47 +16,8 @@ namespace ommu\traits; -trait FileTrait { - - /** - * Create URL Title - * - * Takes a "title" string as input and creates a - * human-friendly URL string with a "separator" string - * as the word separator. - * - * @todo Remove old 'dash' and 'underscore' usage in 3.1+. - * @param string $str Input string - * @param string $separator Word separator (usually '-' or '_') - * @param bool $lowercase Wether to transform the output string to lowercase - * @return string - */ - public function urlTitle($str, $separator = '-', $lowercase = true) - { - if($separator === 'dash') - $separator = '-'; - - elseif($separator === 'underscore') - $separator = '_'; - - $qSeparator = preg_quote($separator, '#'); - $trans = [ - '&.+?:;' => '', - '[^a-z0-9 _-]' => '', - '\s+' => $separator, - '('.$qSeparator.')+' => $separator - ]; - - $str = strip_tags($str); - foreach ($trans as $key => $val) - $str = preg_replace('#'.$key.'#i', $val, $str); - - if ($lowercase === true) - $str = strtolower($str); - - return trim(trim($str, $separator)); - } - +trait FileTrait +{ /** * Explode or Implode Function * diff --git a/UtilityTrait.php b/UtilityTrait.php new file mode 100644 index 0000000..73af6e7 --- /dev/null +++ b/UtilityTrait.php @@ -0,0 +1,98 @@ + + * @contact (+62)856-299-4114 + * @copyright Copyright (c) 2018 Ommu Platform (opensource.ommu.co) + * @created date 12 May 2018, 22:47 WIB + * @link https://github.com/ommu/yii2-traits + * + * Contains many function that most used : + * urlTitle + * flashMessage + * uniqueCode + * + */ + +namespace ommu\traits; + +trait UtilityTrait +{ + /** + * Create URL Title + * + * Takes a "title" string as input and creates a + * human-friendly URL string with a "separator" string + * as the word separator. + * + * @todo Remove old 'dash' and 'underscore' usage in 3.1+. + * @param string $str Input string + * @param string $separator Word separator (usually '-' or '_') + * @param bool $lowercase Wether to transform the output string to lowercase + * @return string + */ + public function urlTitle($str, $separator = '-', $lowercase = true) + { + $str = trim($str); + + if($separator === 'dash') + $separator = '-'; + + elseif($separator === 'underscore') + $separator = '_'; + + $qSeparator = preg_quote($separator, '#'); + $trans = [ + '&.+?:;' => '', + '[^a-z0-9 _-]' => '', + '\s+' => $separator, + '('.$qSeparator.')+' => $separator + ]; + + $str = strip_tags($str); + foreach ($trans as $key => $val) + $str = preg_replace('#'.$key.'#i', $val, $str); + + if ($lowercase === true) + $str = strtolower($str); + + return trim(trim($str, $separator)); + } + + /** + * Provide style for error message + * + * @param mixed $msg + * @param string $type "success, info, warning, danger" + */ + public function flashMessage($message, $class='success') + { + if($message != '') { + $result = '
'; + $result .= $message.'
'; + } + + return $result; + } + + /** + * User salt codes + */ + public static function uniqueCode($length=32, $str=2) + { + $chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + srand((double)microtime()*time()); + $i = 1; + $salt = '' ; + + while ($i <= $length) { + $num = rand() % 33; + $tmp = substr($chars, $num, $str); + $salt = $salt . $tmp; + $i++; + } + + return $salt; + } +}