File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/Helpers/KommaHelpers.php
<?php
namespace App\Helpers;
use DateTime;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
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);
}
}