1
0
www.mikescher.com/www/protected/components/MsHelper.php

116 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2014-06-08 15:32:00 +02:00
<?php
class MsHelper {
public static function getStringDBVar($name)
{
$connection = Yii::app()->db;
$command=$connection->createCommand("SELECT SValue FROM {{othervalues}} WHERE Name = '$name'");
$val = $command->queryScalar();
return $val;
}
public static function setStringDBVar($name, $value)
{
$connection = Yii::app()->db;
$command=$connection->createCommand("REPLACE INTO {{othervalues}} (Name, SValue) VALUES ('$name', '$value')");
$command->query();
}
2014-06-08 15:32:00 +02:00
public static function getIntDBVar($name)
{
$connection = Yii::app()->db;
$command=$connection->createCommand("SELECT [Value] FROM {{othervalues}} WHERE Name = '$name'");
$val = $command->queryScalar();
return $val;
}
public static function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
public static function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
public static function formatMilliseconds($millis)
{
if ($millis < 1000)
{
return $millis . 'ms';
}
else if ($millis < 10 * 1000)
{
return number_format($millis / (1000), 2) . 's';
}
else if ($millis < 60 * 1000)
{
return floor($millis / (1000)) . 's';
}
else if ($millis < 10 * 60 * 1000)
{
return floor($millis / (60 * 1000)) . 'min ' . floor(($millis % (60 * 1000)) / 1000) . 's';
}
else if ($millis < 60 * 60 * 1000)
{
return floor($millis / (60 * 1000)) . 'min';
}
else if ($millis < 10 * 60 * 60 * 1000)
{
return number_format($millis / (60 * 60 * 1000), 2) . ' hours';
}
else
{
return floor($millis / (60 * 60 * 1000)) . ' hours';
}
}
/**
* Appends/Prepends $before/$after to every line in $input
*
* @param $input
* @param $before
* @param $after
* @return mixed
*/
public static function encloseLines($input, $before, $after)
{
$array = preg_split("/\r\n|\n|\r/", $input);
for($i = 0; $i < count($array); $i++)
$array[$i] = $before . $array[$i] . $after;
return implode(PHP_EOL, $array);
}
2014-06-08 15:32:00 +02:00
}