-
Notifications
You must be signed in to change notification settings - Fork 3.6k
allow randomElements
to accept a Traversable object
#1389
Changes from 6 commits
cff4c18
26b11af
0a00054
8d2b01b
e177693
8f337a3
5762ea5
657df1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,9 +168,17 @@ public static function randomAscii() | |
* | ||
* @return array New array with $count elements from $array | ||
*/ | ||
public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false) | ||
public static function randomElements($array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false) | ||
{ | ||
$allKeys = array_keys($array); | ||
if ($array instanceof \Traversable) { | ||
foreach ($array as $element) { | ||
$traversables[] = $element; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i could, but this is why i use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then use only 1 intermediate variable if you want, but pushing to a non-existing array will cause a warning. If it doesn't, it's a bad practice anyway. |
||
} | ||
} | ||
|
||
$arr = isset($traversables) ? $traversables : $array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't check |
||
|
||
$allKeys = array_keys($arr); | ||
$numKeys = count($allKeys); | ||
|
||
if (!$allowDuplicates && $numKeys < $count) { | ||
|
@@ -191,7 +199,7 @@ public static function randomElements(array $array = array('a', 'b', 'c'), $coun | |
$keys[$num] = true; | ||
} | ||
|
||
$elements[] = $array[$allKeys[$num]]; | ||
$elements[] = $arr[$allKeys[$num]]; | ||
$numElements++; | ||
} | ||
|
||
|
@@ -206,7 +214,7 @@ public static function randomElements(array $array = array('a', 'b', 'c'), $coun | |
*/ | ||
public static function randomElement($array = array('a', 'b', 'c')) | ||
{ | ||
if (!$array) { | ||
if (!$array || ($array instanceof \Traversable && !count($array))) { | ||
return null; | ||
} | ||
$elements = static::randomElements($array, 1); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you re-add the type hint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know what you mean by this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just revert your changes to this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the whole point of this PR is to accept things other than arrays, so if I add the type hint back, that negates all of the new code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, you're right, my bad