Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PBXg33k/feature/ArrayTraits
ArrayTraits
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Pbxg33k\Traits; | ||
|
||
trait ArrayTrait | ||
{ | ||
/** | ||
* Recusively implodes an array | ||
* | ||
* @param array $array | ||
* @param string $glue | ||
* | ||
* @return string | ||
*/ | ||
public function recusiveImplode(array $array, $glue = ',') | ||
{ | ||
foreach($array as $key => $element) { | ||
if(is_array($element)) { | ||
$array[$key] = $this->recusiveImplode($element, $glue); | ||
} | ||
} | ||
|
||
return implode($glue, $array); | ||
} | ||
|
||
/** | ||
* Alias for recursiveImplode | ||
* | ||
* @param array $array | ||
* @param string $glue | ||
* @return string | ||
*/ | ||
public function recursiveJoin(array $array, $glue = ',') | ||
{ | ||
return $this->recusiveImplode($array, $glue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
use Pbxg33k\Traits\ArrayTrait; | ||
|
||
require_once('stubs/ArrayTraitClass.php'); | ||
|
||
class ArrayTraitTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ArrayTraitClass | ||
*/ | ||
protected $testClass; | ||
|
||
public function setUp() | ||
{ | ||
$this->testClass = new ArrayTraitClass(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function simpleImplodeReturnString() | ||
{ | ||
$arr = [ | ||
'testKey' => 'testValue', | ||
'testKey2' => 'testValue2' | ||
]; | ||
|
||
$this->assertSame(implode(',', $arr), $this->testClass->recusiveImplode($arr, ',')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodingMultiDimensionalArrayReturnsString() | ||
{ | ||
$arr = [ | ||
'testKey' => 'testValue', | ||
'testArr' => [ | ||
'subKey' => 'subValue' | ||
] | ||
]; | ||
|
||
$expectedOutput = "testValue,subValue"; | ||
|
||
$this->assertSame($expectedOutput, $this->testClass->recusiveImplode($arr, ',')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function joinIsAliasingImplode() | ||
{ | ||
$arr = [ | ||
'testKey' => 'testValue', | ||
'testKey2' => 'testValue2' | ||
]; | ||
|
||
$this->assertSame(implode(',', $arr), $this->testClass->recursiveJoin($arr, ',')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Pbxg33k\Traits\ArrayTrait; | ||
|
||
class ArrayTraitClass | ||
{ | ||
use ArrayTrait; | ||
} |