Skip to content

Commit

Permalink
add fileTrait and UtilityTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
PutraSudaryanto committed Jul 4, 2018
1 parent b251421 commit 7712508
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
68 changes: 68 additions & 0 deletions FileTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* FileTrait
*
* @author Putra Sudaryanto <[email protected]>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2018 Ommu Platform (www.ommu.co)
* @created date 27 June 2018, 10:55 WIB
* @link https://github.com/ommu/yii-traits
*
* Contains many function that most used :
* formatFileType
* createUploadDirectory
*
*/

trait FileTrait
{
/**
* Explode or Implode Function
*
* @param array/string string if param $type=true and array if param $type=false
* @param bool $type true (explode), false (implode)
* @param string $separator Word separator (usually '-' or '_')
*/
public function formatFileType($data, $type=true, $separator=',')
{
if($type == true)
$result = array_map('trim', explode($separator, $data));
else
$result = implode($separator.' ', $data);

return $result;
}

/**
* Generate upload directory if directory not found in application
*/
public function createUploadDirectory($path, $key=null)
{
$uploadPath = $path;
if($key != null)
$uploadPath = join('/', [$path, $key]);
$verwijderenPath = join('/', [$path, 'verwijderen']);

// Add directory
if(!file_exists($uploadPath) || !file_exists($verwijderenPath)) {
if($key != null)
@mkdir($path, 0755, true);
@mkdir($uploadPath, 0755, true);
@mkdir($verwijderenPath, 0755, true);

// Add file in directory (index.php)
$indexFile = join('/', [$uploadPath, 'index.php']);
if(!file_exists($indexFile))
file_put_contents($indexFile, "<?php\n");

$verwijderenFile = join('/', [$verwijderenPath, 'index.php']);
if(!file_exists($verwijderenFile))
file_put_contents($verwijderenFile, "<?php\n");
} else {
@chmod($uploadPath, 0755, true);
@chmod($verwijderenPath, 0755, true);
}

return true;
}
}
107 changes: 107 additions & 0 deletions UtilityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* UtilityTrait
*
* @author Putra Sudaryanto <[email protected]>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2018 Ommu Platform (www.ommu.co)
* @created date 4 July 2018, 23:55 WIB
* @link https://github.com/ommu/yii-traits
*
* Contains many function that most used :
* urlTitle
* uniqueCode
* licenseCode
*
*/

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));
}

/**
* User salt codes
*/
public 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;
}

/**
* get License
*/
public function licenseCode($source='1234567890', $length=16, $char=4)
{
$mod = $length%$char;
if($mod == 0)
$sep = ($length/$char);
else
$sep = (int)($length/$char)+1;

$sourceLength = strlen($source);
$random = '';
for ($i = 0; $i < $length; $i++)
$random .= $source[rand(0, $sourceLength - 1)];

$license = '';
for ($i = 0; $i < $sep; $i++) {
if($i != $sep-1)
$license .= substr($random,($i*$char),$char).'-';
else
$license .= substr($random,($i*$char),$char);
}

return $license;
}
}

0 comments on commit 7712508

Please sign in to comment.