Skip to content

Commit

Permalink
Made the random function more versatile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Jan 26, 2012
1 parent f792b55 commit 6f5ceee
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('/(?<!^)(?!$)/u', $values);
}

if (!is_array($values)) {
return $values;
}

if (0 === count($values)) {
Expand Down

0 comments on commit 6f5ceee

Please sign in to comment.