Skip to content

Instantly share code, notes, and snippets.

@shayanderson
Created May 14, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shayanderson/0fa098fcd4cd302de4b9 to your computer and use it in GitHub Desktop.
Save shayanderson/0fa098fcd4cd302de4b9 to your computer and use it in GitHub Desktop.
Utility functions for PHP 5.5+
<?php
/**
* Utility functions for PHP 5.5+
*
* @copyright 2015 Shay Anderson <http://www.shayanderson.com>
* @license MIT License <http://www.opensource.org/licenses/mit-license.php>
* @link <http://www.shayanderson.com/php/php-utility-functions.htm>
*/
/**
* Class autoloading
*
* @param array $autoload_paths
* @return void
*/
function autoload(array $autoload_paths)
{
set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $autoload_paths));
function __autoload($class)
{
require_once str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
}
}
/**
* Decorate values
*
* @param string $decorator
* @param mixed $value
* @param mixed $_
* @return string
*/
function decorate($decorator, $value, $_ = null)
{
$value = array_slice(func_get_args(), 1); // rm decorator
$udf = [];
foreach($value as $k => $v) // find user defined functions
{
if(is_array($v))
{
$udf = array_filter($v, function($v) use(&$value, &$k) // set user defined functions
{
if(is_callable($v))
{
unset($value[$k]); // rm UDFs from values
return true;
}
});
}
}
$filter = function($value, &$callable, &$values = null) use(&$udf)
{
if(isset($udf[$callable])) // user defined function
{
return $udf[$callable]($value, $values);
}
else if(is_callable($callable)) // PHP standard built-in function
{
return $callable($value);
}
else
{
user_error('Failed to find filter \'' . $callable . '\'', \E_USER_WARNING);
}
return $value;
};
if(is_string($decorator) && count($value))
{
$pattern = '/{\$([\w]+)(?:\:([^}]+))?}/i';
$pattern_d = '/{\$([\d]+)(?:\:([^}]+))?}/i';
if(is_scalar($value[0])) // scalar value
{
$i = 0;
// test {$[n]} params, ex: {$1}
preg_replace_callback($pattern_d, function($m) use(&$value, &$decorator,
&$filter, &$i)
{
if(isset($value[$m[1] - 1]) || array_key_exists($m[1] - 1, $value))
{
$decorator = str_replace($m[0], isset($m[2])
? $filter($value[$m[1] - 1], $m[2]) : $value[$m[1] - 1],
$decorator);
$i++;
}
}, $decorator);
if(!$i) // use {$named} (ordered) params
{
preg_replace_callback($pattern, function($m) use(&$value, &$decorator,
&$filter, &$i)
{
if(isset($value[$i]) || array_key_exists($i, $value))
{
$decorator = str_replace($m[0], isset($m[2])
? $filter($value[$i], $m[2]) : $value[$i], $decorator);
}
$i++;
}, $decorator);
}
}
else if(is_object($value[0])) // single object
{
$decorator = call_user_func_array(__FUNCTION__, [$decorator, (array)$value[0], $udf]);
}
else if(is_array($value[0])) // array
{
if(!isset($value[0][1])) // one-dimensional
{
$value = $value[0];
preg_replace_callback($pattern, function($m) use(&$value, &$decorator, &$filter)
{
if(isset($value[$m[1]]) || array_key_exists($m[1], $value))
{
$decorator = str_replace($m[0], isset($m[2])
? $filter($value[$m[1]], $m[2], $value)
: $value[$m[1]], $decorator);
}
}, $decorator);
}
else // multidimensional
{
$out = '';
foreach($value[0] as $v)
{
$out .= call_user_func_array(__FUNCTION__, [$decorator, $v, $udf]);
}
$decorator = &$out;
}
}
}
return $decorator;
}
/**
* String/array printer for debugging
*
* @var mixed $v
* @return void
*/
function pa($v = null)
{
if(count(func_get_args()) > 1)
{
foreach(func_get_args() as $arg) pa($arg);
return;
}
echo is_scalar($v) || $v === null ? $v . ( PHP_SAPI === 'cli' ? PHP_EOL : '<br />' )
: ( PHP_SAPI === 'cli' ? print_r($v, true) : '<pre>' . print_r($v, true) . '</pre>' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment