Skip to content

Commit d05e814

Browse files
author
coa
committed
-selectRandomArrayElements method added along with tests
1 parent 83781dd commit d05e814

4 files changed

Lines changed: 142 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ SimpleArrayLibrary::isSubArray(array(2, 1), array(2)); // true
253253
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('c' => 1)); // false
254254
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('a' => 2)); // false
255255
```
256+
selectRandomArrayElements
257+
------------------------------
258+
Select sub-array of random elements. Keys association is preserved. If required number of random elements is equal or bigger then number
259+
of original array members, entire array is returned.
260+
261+
Second parameter must be positive integer or string representation of the positive integer, otherwise exception is thrown.
262+
```php
263+
SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2), 1); // array('foo' => 2)
264+
SimpleArrayLibrary::selectRandomArrayElements(array(1, 2), 1); // array(1) OR array(1 => 2)
265+
SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2, 'bar'), 3); // array('foo' => 2, 'bar')
266+
```
256267
setColumn
257268
------------------------------
258269
Sets values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more
@@ -265,4 +276,4 @@ exists
265276
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1); // array(array('foo' => 1), array('foo' => 1))
266277
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, false, false); // array(array('foo' => 2), array())
267278
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, true, false); // array(array('foo' => 2), array('foo' => 1))
268-
```
279+
```

src/SimpleArrayLibrary.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,34 @@ public static function isSubArray(array $array, array $subArray, $strictComparis
455455
return $return;
456456
}
457457

458+
/**
459+
* Selects random sub array
460+
*
461+
* @param array $array
462+
* @param int $numberOfRequiredElements
463+
*
464+
* @return array
465+
* @throws InvalidArgumentException
466+
*/
467+
public static function selectRandomArrayElements(array $array, $numberOfRequiredElements)
468+
{
469+
// validation, must be positive int or 0
470+
if (!preg_match('/^[1-9]\d*$/', $numberOfRequiredElements)) {
471+
throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer');
472+
}
473+
474+
$selected = $array;
475+
if (count($array) > $numberOfRequiredElements) {
476+
// select required number of random keys
477+
$selectedKeys = array_rand($array, $numberOfRequiredElements);
478+
$selectedKeys = $numberOfRequiredElements == 1 ? [$selectedKeys] : $selectedKeys;
479+
// select only array members with selected random keys
480+
$selected = array_intersect_key($array, array_fill_keys($selectedKeys, null));
481+
}
482+
483+
return $selected;
484+
}
485+
458486
/**
459487
* @param array $matrix
460488
* @param mixed $column
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use SimpleArrayLibrary\SimpleArrayLibrary;
4+
5+
class SelectRandomArrayElementsTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @dataProvider getData
9+
*/
10+
public function testMethod($array, $expected)
11+
{
12+
// arrange
13+
$requestedNumberOfElements = 2;
14+
15+
// act
16+
$result = SimpleArrayLibrary::selectRandomArrayElements($array, $requestedNumberOfElements);
17+
18+
// assert
19+
if (count($expected) > $requestedNumberOfElements) {
20+
$this->assertCount(2, $result);
21+
$this->assertArraySubset($result, $expected);
22+
} else {
23+
$this->assertEquals($expected, $result);
24+
}
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function getData()
31+
{
32+
return [
33+
// #0 empty array
34+
[
35+
'array' => [],
36+
'expected' => [],
37+
],
38+
// #1 fewer elements requested then array contains
39+
[
40+
'array' => [1],
41+
'expected' => [1],
42+
],
43+
// #2 same number of elements requested as array contains
44+
[
45+
'array' => [1, 2],
46+
'expected' => [1, 2],
47+
],
48+
// #3 more elements requested then array contains
49+
[
50+
'array' => [1, 2, 3],
51+
'expected' => [1, 2, 3],
52+
],
53+
];
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use SimpleArrayLibrary\SimpleArrayLibrary;
4+
5+
class SelectRandomArrayElementsXTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @dataProvider getData
9+
*
10+
* @return void
11+
*/
12+
public function test_function(array $data)
13+
{
14+
// prepare
15+
$this->setExpectedException('InvalidArgumentException', 'Number of requested elements parameter must be a positive integer');
16+
17+
// invoke logic & test
18+
SimpleArrayLibrary::selectRandomArrayElements(array(1), $data['numberOfRequiredElements']);
19+
}
20+
21+
/**
22+
* @return array
23+
*/
24+
public function getData()
25+
{
26+
return array(
27+
// #0 negative integer
28+
array(
29+
array(
30+
'numberOfRequiredElements' => -1
31+
)
32+
),
33+
// #1 zero
34+
array(
35+
array(
36+
'numberOfRequiredElements' => 0
37+
)
38+
),
39+
// #2 not an integer
40+
array(
41+
array(
42+
'numberOfRequiredElements' => 'foo'
43+
)
44+
)
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)