Skip to content

Commit eed71b0

Browse files
authored
Merge pull request #2 from PBXg33k/feature/ArrayTraits
ArrayTraits
2 parents 29ed04f + f66f11d commit eed71b0

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ matrix:
1212
before_script:
1313
- mkdir -p build/logs
1414
after_script:
15-
- travis_retry php bin/coveralls -v
15+
- travis_retry php vendor/bin/coveralls -v
1616
- php: 7.0
1717
fast_finish: true

src/ArrayTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Pbxg33k\Traits;
4+
5+
trait ArrayTrait
6+
{
7+
/**
8+
* Recusively implodes an array
9+
*
10+
* @param array $array
11+
* @param string $glue
12+
*
13+
* @return string
14+
*/
15+
public function recusiveImplode(array $array, $glue = ',')
16+
{
17+
foreach($array as $key => $element) {
18+
if(is_array($element)) {
19+
$array[$key] = $this->recusiveImplode($element, $glue);
20+
}
21+
}
22+
23+
return implode($glue, $array);
24+
}
25+
26+
/**
27+
* Alias for recursiveImplode
28+
*
29+
* @param array $array
30+
* @param string $glue
31+
* @return string
32+
*/
33+
public function recursiveJoin(array $array, $glue = ',')
34+
{
35+
return $this->recusiveImplode($array, $glue);
36+
}
37+
}

tests/ArrayTraitTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
use Pbxg33k\Traits\ArrayTrait;
3+
4+
require_once('stubs/ArrayTraitClass.php');
5+
6+
class ArrayTraitTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @var ArrayTraitClass
10+
*/
11+
protected $testClass;
12+
13+
public function setUp()
14+
{
15+
$this->testClass = new ArrayTraitClass();
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
public function simpleImplodeReturnString()
22+
{
23+
$arr = [
24+
'testKey' => 'testValue',
25+
'testKey2' => 'testValue2'
26+
];
27+
28+
$this->assertSame(implode(',', $arr), $this->testClass->recusiveImplode($arr, ','));
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function implodingMultiDimensionalArrayReturnsString()
35+
{
36+
$arr = [
37+
'testKey' => 'testValue',
38+
'testArr' => [
39+
'subKey' => 'subValue'
40+
]
41+
];
42+
43+
$expectedOutput = "testValue,subValue";
44+
45+
$this->assertSame($expectedOutput, $this->testClass->recusiveImplode($arr, ','));
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function joinIsAliasingImplode()
52+
{
53+
$arr = [
54+
'testKey' => 'testValue',
55+
'testKey2' => 'testValue2'
56+
];
57+
58+
$this->assertSame(implode(',', $arr), $this->testClass->recursiveJoin($arr, ','));
59+
}
60+
}

tests/stubs/ArrayTraitClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Pbxg33k\Traits\ArrayTrait;
4+
5+
class ArrayTraitClass
6+
{
7+
use ArrayTrait;
8+
}

0 commit comments

Comments
 (0)