Skip to content

Commit

Permalink
Support a single padding value for exploded
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Aug 19, 2014
1 parent 2184db4 commit 594127f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
18 changes: 12 additions & 6 deletions inc/GlobalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,16 @@ function get_public_properties($object)
/**
* Explode a string, filling the remainder with provided defaults.
*
* @param string $delimiter The boundary string
* @param string $string The input string.
* @param array $defaults Array to return, with replacements made
* @param int|null $limit Passed through to the initial explode
* @param string $delimiter The boundary string
* @param string $string The input string.
* @param array|mixed $defaults Array to return, with replacements made,
* or a padding value
* @param int|null $limit Passed through to the initial explode
*
* @return array
*
*/
function exploded($delimiter, $string, array $defaults = [], $limit = null)
function exploded($delimiter, $string, $defaults = null, $limit = null)
{
if($limit === null)
{
Expand All @@ -532,7 +533,12 @@ function exploded($delimiter, $string, array $defaults = [], $limit = null)
$parts = explode($delimiter, $string, $limit);
}

return array_replace($defaults, $parts);
if(is_array($defaults))
{
return array_replace($defaults, $parts);
}

return array_pad($parts, $limit, $defaults);
}
}

Expand Down
11 changes: 10 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
<blacklist>
<directory suffix=".php">vendor</directory>
<directory suffix=".php">tests</directory>
</blacklist>
</filter>
</phpunit>
4 changes: 4 additions & 0 deletions tests/GlobalFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ public function testExploded()
[1, 2, 3, 4, 5],
exploded(",", "1,2,3,4,5", $defaults)
);
$this->assertEquals(
[1, 2, 3, '-', '-'],
exploded(",", "1,2,3", '-', 5)
);
}

public function testBetween()
Expand Down

0 comments on commit 594127f

Please sign in to comment.