Skip to content

Commit da4b4ab

Browse files
authored
Merge pull request #6 from chadicus/fea/pad
Add Arrays::pad
2 parents 6c4f92c + 9f8d3a3 commit da4b4ab

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ $value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
7575
assert($value === [1, 2, 3, 4, 5]);
7676
```
7777

78+
#### Arrays::pad
79+
80+
This filter pads an array to the specified length with a value. Padding optionally to the front or end of the array.
81+
82+
```php
83+
$value = \TraderInteractive\Filter\Arrays::pad([1, 2], 5, 0, \TraderInteractive\Filter\Arrays::ARRAY_PAD_FRONT);
84+
assert($value === [0, 0, 0, 1, 2]);
85+
```
86+
7887
## Project Build
7988

8089
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:

src/Arrays.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22

33
namespace TraderInteractive\Filter;
44

5+
use InvalidArgumentException;
56
use TraderInteractive\Exceptions\FilterException;
67

78
/**
89
* A collection of filters for arrays.
910
*/
1011
final class Arrays
1112
{
13+
/**
14+
* @var int
15+
*/
16+
const ARRAY_PAD_END = 1;
17+
18+
/**
19+
* @var int
20+
*/
21+
const ARRAY_PAD_FRONT = 2;
22+
1223
/**
1324
* Filter an array by throwing if not an array or count not in the min/max range.
1425
*
@@ -149,4 +160,33 @@ public static function copy(array $source, array $keyMap) : array
149160

150161
return $result;
151162
}
163+
164+
/**
165+
* Pad array to the specified length with a value. Padding optionally to the front or end of the array.
166+
*
167+
* @param array $input Initial array of values to pad.
168+
* @param int $size The new size of the array.
169+
* @param mixed $padValue Value to pad if $input is less than $size.
170+
* @param int $padType Optional argument to specify which end of the array to pad.
171+
*
172+
* @return array Returns a copy of the $input array padded to size specified by $size with value $padValue
173+
*
174+
* @throws InvalidArgumentException Thrown if $padType is invalid.
175+
*/
176+
public static function pad(array $input, int $size, $padValue = null, int $padType = self::ARRAY_PAD_END) : array
177+
{
178+
if ($padType === self::ARRAY_PAD_END) {
179+
return array_pad($input, $size, $padValue);
180+
}
181+
182+
if ($padType !== self::ARRAY_PAD_FRONT) {
183+
throw new InvalidArgumentException('Invalid $padType value provided');
184+
}
185+
186+
while (count($input) < $size) {
187+
array_unshift($input, $padValue);
188+
}
189+
190+
return $input;
191+
}
152192
}

tests/ArraysTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TraderInteractive\Filter;
44

5+
use InvalidArgumentException;
56
use PHPUnit\Framework\TestCase;
67
use TraderInteractive\Exceptions\FilterException;
78

@@ -198,4 +199,45 @@ public function copyEach()
198199
$result
199200
);
200201
}
202+
203+
/**
204+
* @test
205+
* @covers ::pad
206+
*/
207+
public function pad()
208+
{
209+
$result = Arrays::pad([12, 10, 9], 5, 0);
210+
$this->assertSame([12, 10, 9, 0, 0], $result);
211+
}
212+
213+
/**
214+
* @test
215+
* @covers ::pad
216+
*/
217+
public function padArrayLengthGreaterThanSize()
218+
{
219+
$result = Arrays::pad(['a', 'b', 'c'], 2, 0);
220+
$this->assertSame(['a', 'b', 'c'], $result);
221+
}
222+
223+
/**
224+
* @test
225+
* @covers ::pad
226+
*/
227+
public function padFront()
228+
{
229+
$result = Arrays::pad(['a', 'b', 'c'], 5, null, Arrays::ARRAY_PAD_FRONT);
230+
$this->assertSame([null, null, 'a', 'b', 'c'], $result);
231+
}
232+
233+
/**
234+
* @test
235+
* @covers ::pad
236+
*/
237+
public function padInvalidPadType()
238+
{
239+
$this->expectException(InvalidArgumentException::class);
240+
$this->expectExceptionMessage('Invalid $padType value provided');
241+
Arrays::pad(['a', 'b', 'c'], 5, null, 0);
242+
}
201243
}

0 commit comments

Comments
 (0)