From: Tobias Schultze Date: Thu, 26 Jan 2012 01:32:02 +0000 (+0100) Subject: Made the random function more versatile X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=6f5ceeec106e6f7133270dcae3058724659111fa;p=web%2Fkonrad%2Ftwig.git Made the random function more versatile --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index afa7073..3a7d6f8 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -283,20 +283,35 @@ function twig_cycle($values, $i) } /** - * Returns a random item from sequence. + * Returns a random value depending on the supplied paramter type: + * - a random item from a Traversable or array + * - a random character from a string + * - a random integer between 0 and the integer parameter * - * @param Iterator|array $values An array or an ArrayAccess instance + * @param Traversable|array|int|string $values The values to pick a random item from * * @return mixed A random value from the given sequence */ -function twig_random($values) +function twig_random($values = null) { - if (!is_array($values) && !$values instanceof Traversable) { - return $values; + if (null === $values) { + return mt_rand(); + } + + if (is_int($values) || is_float($values)) { + return mt_rand(0, $values); } - if (is_object($values)) { + if ($values instanceof Traversable) { $values = iterator_to_array($values); + } elseif (is_string($values)) { + // unicode version of str_split() + // split at all positions, but not after the start and not before the end + $values = preg_split('/(?