Skip to content

Commit

Permalink
add slice method, see #4
Browse files Browse the repository at this point in the history
  • Loading branch information
imsamurai committed May 19, 2014
1 parent 8ecf241 commit 95821a0
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
143 changes: 143 additions & 0 deletions Test/Case/Utility/ArrayObjectATest.php
Expand Up @@ -161,6 +161,22 @@ public function testInheritance() {
}), 'Unique');
$this->assertInstanceOf($className, $Array->resetKeys(), 'Reset keys');
}

/**
* Test slice
*
* @param array $array
* @param int $offset
* @param int $length
* @param bool $preserve_keys
* @param array $result
*
* @dataProvider sliceProvider
*/
public function testSlice(array $array, $offset, $length, $preserve_keys, array $result) {
$this->assertSame((new ArrayObjectA($array))->slice($offset, $length, $preserve_keys)->getArrayCopy(), $result);

}

/**
* Data provider for testMerge
Expand Down Expand Up @@ -306,5 +322,132 @@ public function uniqueProvider() {
)
);
}

/**
* Data provider for testSlice
*
* @return array
*/
public function sliceProvider() {
return array(
//set #0
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
1,
//length
null,
//preserve_key
true,
//result
array(
1 => 'two',
2 => 'three'
)
),
//set #1
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
1,
//length
null,
//preserve_key
false,
//result
array(
0 => 'two',
1 => 'three'
)
),
//set #1
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
1,
//length
1,
//preserve_key
true,
//result
array(
1 => 'two',
)
),
//set #2
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
1,
//length
1,
//preserve_key
false,
//result
array(
0 => 'two',
)
),
//set #3
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
3,
//length
10,
//preserve_key
false,
//result
array(
)
),
//set #4
array(
//array
array(
0 => 'one',
1 => 'two',
2 => 'three'
),
//offset
0,
//length
10,
//preserve_key
true,
//result
array(
0 => 'one',
1 => 'two',
2 => 'three'
)
),
);
}

}
9 changes: 9 additions & 0 deletions Utility/ArrayObjectA.php
Expand Up @@ -109,5 +109,14 @@ public function unique(callable $callback) {
public function resetKeys() {
return new static(array_values($this->getArrayCopy()));
}

/**
* Slice array
*
* @return \ArrayObjectA
*/
public function slice($offset, $length = null, $preserve_keys = false) {
return new static(array_slice($this->getArrayCopy(), $offset, $length, $preserve_keys));
}

}

0 comments on commit 95821a0

Please sign in to comment.