-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nb. move urlTitle to him, flashMessage and uniqueCode
- Loading branch information
1 parent
dc41938
commit 5e603c0
Showing
2 changed files
with
100 additions
and
45 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
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,98 @@ | ||
<?php | ||
/** | ||
* UtilityTrait | ||
* | ||
* @author Putra Sudaryanto <putra@sudaryanto.id> | ||
* @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 = '<div class="alert alert-'.$class.' alert-dismissible fade in">'; | ||
$result .= $message.'</div>'; | ||
} | ||
|
||
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; | ||
} | ||
} |