File: D:/HostingSpaces/fire-tech/fire-tech.nl/app/Helpers/KommaHelpers.php
<?php
namespace App\Helpers;
use DateTime;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
/**
* Short description for the file.
*
* @copyright (c) 2012-2015, Komma
*/
class KommaHelpers
{
/**
* This function will localize the date
*
* @param $date
* @return DateTime
*/
static function localize_date($date, $format = 'd-m-Y H:i', $strftime = false)
{
date_default_timezone_set('UTC');
$datetime = new DateTime($date);
$la_time = new \DateTimeZone('Europe/Amsterdam');
$datetime->setTimezone($la_time);
if ($strftime) {
/**
* When it is an windows server %e and % do not work, so convert these
*
*/
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('/%e/', '%#d', $format);
$format = preg_replace('/%G/', '%Y', $format);
}
return strftime($format, $datetime->getTimestamp());
}
return $datetime->format($format);
}
static function komma_ip()
{
if ($_SERVER['REMOTE_ADDR'] == '5.172.219.238') return true;
return false;
}
static function price($value, $parameters = array())
{
return number_format($value / 100, 2, ',', '.');
}
/**
* Create an empty file on the fly based on the filePath
* Check if it already exist, if it exist return true
* Check if the folder exist, create when needed
* Create a file and fill with the given data.
*
* @param $filePath
* @param $data | String
* @return bool
*/
static function createFile($filePath, $data = '')
{
//Check if file exist, if true return
if (is_file($filePath)) return true;
//split the path in parts based on / (folder)
$parts = explode('/', $filePath);
//Pop the last item from the array, this is the file name
$fileName = array_pop($parts);
//Glue the path back together, without filename
$path = implode('/', $parts);
//If the dir doesn't exist, create this
if (!is_dir($path)) mkdir($path, 0777, true);
//Create an empty file on the location
file_put_contents($path . '/' . $fileName, $data);
return true;
}
/**
* Remove a file from the disc
*
*
* @param $filePath
* @return bool
*/
static function removeFile($filePath)
{
//Check if file exist, if false, return
if (!is_file($filePath)) return true;
//Remove the file
unlink($filePath);
}
/**
* Replace the / to a -
* for the given string.
*
* @param $string
* @return string
*/
static function slashToDash($string)
{
return str_replace('/', '-', $string);
}
/**
* Replace the - to a /
* for the given string.
*
* @param $string
* @return string
*/
static function dashToSlash($string)
{
return str_replace('-', '/', $string);
}
/**
* Replace the / to a _
* for the given string.
*
* @param $string
* @return string
*/
static function slashToUnderScore($string)
{
return str_replace('/', '_', $string);
}
/**
* Replace the _ to a /
* for the given string.
*
* @param $string
* @return string
*/
static function UnderScoreToSlash($string)
{
return str_replace('_', '/', $string);
}
/**
* Get data from the input.
* based on the given key
* and the language id
*
* @param $key
* @param $language
* @return mixed
*/
static function getTranslationInput($key, $language, $suffix = '')
{
$key = $key;
$key .= '_' . $language->id;
if ($suffix != '') $key .= '_' . $suffix;
return \Input::get($key);
}
/**
* This method checks if an Kms attribute is required
*
* @param $attribute
* @return bool
*/
static function isRequired($attribute)
{
if (!isset($attribute->options)) return false;
if (!isset($attribute->options['validation'])) return false;
if (!isset($attribute->options['validation']['rules'])) return false;
if (!preg_match('/required/', $attribute->options['validation']['rules'])) return false;
return true;
}
/**
* Make the place-holder replacements on a line.
* This is a duplicate from the translator class
* Illuminate\Translation\Translator
*
*
* @param string $line
* @param array $replace
* @return string
*/
static function makeReplacements($line, array $replace)
{
$replace = KommaHelpers::sortReplacements($replace);
foreach ($replace as $key => $value) {
$line = str_replace(':' . $key, $value, $line);
}
return $line;
}
/**
* Sort the replacements array.
* This is a duplicate from the translator class
* Illuminate\Translation\Translator
*
* @param array $replace
* @return array
*/
static function sortReplacements(array $replace)
{
return (new Collection($replace))->sortBy(function ($value, $key) {
return mb_strlen($key) * -1;
});
}
/**
* @param string $code
* @param string $message
* @return mixed
*/
static function ajaxAbort($code = '400', $message = 'Oops something went wrong')
{
return \Response::json([
'message' => $message,
], $code);
}
/**
* @param $string
* @param int $length
* @return null|string|string[]
*/
static function str_limit_full_word($string, $length = 75){
$length = abs((int)$length);
if(strlen($string) > $length) {
$string = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $string);
}
return($string);
}
/**
* Returns the maximum upload size in bytes
*
* @return int
*/
public static function fileUploadMaxSize() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = self::parseSize(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = self::parseSize(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
/**
* Returns a php ini file size as bytes. taking into account that it may have unit characters in the size.
*
* @param $size
* @return float
*/
public static function parseSize($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
}