A list of some things quite handy for every cake app and therefore best placed in the bootstrap.php as they are not (yet) part of the cake core.
# Useful when putting a string together in PHP
define('LF', PHP_EOL);
define('NL', "\n"); // new line
define('CR', "\r"); // carriage return
define('TB', "\t"); // tabulator
define('BR', '<br />'); // line break
# Make the app and l10n play nice with Windows.
if (substr(PHP_OS, 0, 3) == 'WIN') {
define('WINDOWS', true);
} else {
define('WINDOWS', false);
}
define('FORMAT_DB_DATETIME','Y-m-d H:i:s'); // used in date(...)
define('FORMAT_DB_DATE','Y-m-d');
define('FORMAT_DB_TIME','H:i:s');
define('DEFAULT_DATETIME', '0000-00-00 00:00:00');
define('DEFAULT_DATE', '0000-00-00');
define('DEFAULT_TIME', '00:00:00');
/**
* convenience function to check on "empty()"
* does also work with methods
* 2009-06-15 ms
*/
function isEmpty($var = null) {
if (empty($var)) {
return true;
}
return false;
}
/**
* of what type is the specific value
* useful for cake < 2 - since cake2.0 it is integrated in debug()
*
* @return type: NULL, array, bool, float, int, string, unknown
* 2009-03-03 ms
*/
function returns($value) {
if ($value === null) {
return 'NULL';
} elseif (is_array($value)) {
return '(array)'.'<pre>'.print_r($value,true).'</pre>';
} elseif ($value === true) {
return '(bool)TRUE';
} elseif ($value === false) {
return '(bool)FALSE';
} elseif (is_numeric($value) && is_float($value)) {
return '(float)'.$value;
} elseif (is_numeric($value) && is_int($value)) {
return '(int)'.$value;
} elseif (is_string($value)) {
return '(string)'.$value;
} elseif (is_object($value)) {
return '(object)'.get_class($value);
} else {
return '(unknown)'.$value;
}
}
/**
* uses native PHP function to retrieve infos about a filename etc.
* @param string type (extension/ext, filename/file, basename/base, dirname/dir)
* @param string filename to check on
* //TODO: switch parameters!!!
* 2009-01-22 ms
*/
function extractPathInfo($type = null, $filename) {
switch ($type) {
case 'extension':
case 'ext':
$infoType = PATHINFO_EXTENSION; break;
case 'filename':
case 'file':
$infoType = PATHINFO_FILENAME; break;
case 'basename':
case 'base':
$infoType = PATHINFO_BASENAME; break;
case 'dirname':
case 'dir':
$infoType = PATHINFO_DIRNAME; break;
default:
$infoType = null;
}
return pathinfo($filename, $infoType);
}
/**
* Shows pr() messages, even with debug=0
*
* @param mixed $content
* @param string $class (optional)
* 2009-04-07 ms
*/
function pre($array, $class = null) {
$pre_array='';
$pre_class='';
if (is_array($array)) {
if (!empty($class)){ $pre_class=' class="'.$class.'"'; }
$pre_array='<pre'.$pre_class.'>'.print_r($array,true).'</pre>';
} else {
$pre_array = '<pre'.$pre_class.'>'.$array.'</pre>';
}
return $pre_array;
}
/**
* Checks if the string [$haystack] contains [$needle]
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @return boolean
*/
function contains($haystack, $needle, $caseSensitive = false) {
return (!$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle)) !== false;
}
/**
* Checks if the string [$haystack] starts with [$needle]
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @return boolean
*/
function startsWith($haystack, $needle, $caseSensitive = false) {
if ($caseSensitive) {
return (mb_strpos($haystack, $needle) === 0);
}
return (mb_stripos($haystack, $needle) === 0);
}
/**
* Checks if the String [$haystack] ends with [$needle]
* @param string $haystack Input string.
* @param string $needle Needed char or string
* @return boolean
*/
function endsWith($haystack, $needle, $caseSensitive = false) {
if ($caseSensitive) {
return mb_strrpos($haystack, $needle) === mb_strlen($haystack)-mb_strlen($needle);
}
return mb_strripos($haystack, $needle) === mb_strlen($haystack)-mb_strlen($needle);
}
/*** < PHP5.3 ***/
if (function_exists('lcfirst') === false) {
function lcfirst($str) {
return (string)(mb_strtolower(mb_substr($str,0,1)).mb_substr($str,1));
}
}
TB and BR etc really help if you write PHP and don’t want to switch to HTML all the time:
echo $this->foo().BR.$this->foo2();
returns() helped me a lot to figure out the return value of functions. E.g. pr() doesn’t show if NULL or FALSE was returned.
echo returns($this->foo2());
//or
$res = returns($this->foo2();
die(returns($res));