Added new random string static and twig function

This commit is contained in:
Andy Miller
2015-02-17 15:44:34 -07:00
parent 7b32cbe2e1
commit 9bc365fe9e
2 changed files with 26 additions and 1 deletions

View File

@@ -68,7 +68,8 @@ class TwigExtension extends \Twig_Extension
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
new \Twig_SimpleFunction('gist', [$this, 'gistFunc'])
new \Twig_SimpleFunction('gist', [$this, 'gistFunc']),
new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']),
];
}
@@ -433,4 +434,16 @@ class TwigExtension extends \Twig_Extension
{
return '<script src="https://gist.github.com/'.$id.'.js"></script>';
}
/**
* Generate a random string
*
* @param int $count
*
* @return string
*/
public function randomStringFunc($count = 5)
{
return Utils::generateRandomString($count);
}
}

View File

@@ -219,4 +219,16 @@ abstract class Utils
}
return $truncate;
}
/**
* Generate a random string of a given length
*
* @param int $length
*
* @return string
*/
public static function generateRandomString($length = 5)
{
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
}