Skip to content

Commit 8958109

Browse files
authored
Merge pull request #3 from chadicus/master
Add Arrays::arrayize
2 parents cc5453e + 6d0a52c commit 8958109

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ before_script:
1515
- composer update $PREFER_LOWEST
1616
script:
1717
- ./vendor/bin/phpunit
18-
after_success: ./vendor/bin/coveralls -v
18+
after_success: ./vendor/bin/php-coveralls -v

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
"require-dev": {
2323
"squizlabs/php_codesniffer": "^3.2",
2424
"phpunit/phpunit": "^6.5",
25-
"php-coveralls/php-coveralls": "^1.0"
25+
"php-coveralls/php-coveralls": "^2.1"
2626
}
2727
}

src/Arrays.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,24 @@ function ($item) use (&$result) {
8686

8787
return $result;
8888
}
89+
90+
/**
91+
* Converts any non-array value to a single element array.
92+
*
93+
* @param mixed $value The value to convert.
94+
*
95+
* @return array The coverted array or the original value.
96+
*/
97+
public static function arrayize($value) : array
98+
{
99+
if ($value === null) {
100+
return [];
101+
}
102+
103+
if (!is_array($value)) {
104+
return [$value];
105+
}
106+
107+
return $value;
108+
}
89109
}

tests/ArraysTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,32 @@ public function flatten()
119119
{
120120
$this->assertSame([1, 2, 3, 4, 5], Arrays::flatten([[1, 2], [[3, [4, 5]]]]));
121121
}
122+
123+
/**
124+
* @test
125+
* @covers ::arrayize
126+
*/
127+
public function arrayizeReturnsInputIfItIsAnArray()
128+
{
129+
$this->assertSame([1, 2, 3, 4, 5], Arrays::arrayize([1, 2, 3, 4, 5]));
130+
}
131+
132+
/**
133+
* @test
134+
* @covers ::arrayize
135+
*/
136+
public function arrayizeWrapsNonArrayValue()
137+
{
138+
$value = new \StdClass();
139+
$this->assertSame([$value], Arrays::arrayize($value));
140+
}
141+
142+
/**
143+
* @test
144+
* @covers ::arrayize
145+
*/
146+
public function arrayizeConvertsNullToEmptyArray()
147+
{
148+
$this->assertSame([], Arrays::arrayize(null));
149+
}
122150
}

0 commit comments

Comments
 (0)