Skip to content

Commit 008ce8a

Browse files
committed
Merge branch 'release/0.3.3'
2 parents a221ff4 + 1ddfe9b commit 008ce8a

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Alias for `toArray()`.
7777

7878
Returns all items in the collection as an associative array.
7979

80+
##### `flatten() : array`
81+
82+
Flatten all items in the collection using dot notation.
83+
8084
### \Noname\Common\Validator
8185

8286
Use `Validator` to validate your data based on a set of rules.
@@ -238,4 +242,14 @@ Validator::isstring('Hello world!');
238242

239243
// This is valid because 'String' starts with uppercased letter.
240244
Validator::isString('Hello world!');
241-
```
245+
```
246+
247+
### `\Noname\Common\Arr`
248+
249+
A helper library for working with arrays.
250+
251+
#### Arr Methods
252+
253+
##### `Arr::flatten(array $array, string $separator = '.', string $prepend = '') : array`
254+
255+
Flatten an associative array using a custom separator.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "nonamephp/php7-common",
33
"description": "A collection of common PHP 7 libraries",
4-
"version": "0.3.2",
4+
"version": "0.3.3",
55
"license": "MIT",
66
"authors": [
77
{
88
"name": "Clint Tyler",
99
"email": "[email protected]"
1010
}
1111
],
12-
"keywords": ["noname", "nonamephp", "common", "collection", "validator"],
12+
"keywords": ["noname", "nonamephp", "common", "collection", "validator", "array"],
1313
"homepage": "https://github.com/nonamephp/php7-common",
1414
"require": {
1515
"php": ">=7.0"

src/Arr.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
namespace Noname\Common;
3+
4+
/**
5+
* Class Arr
6+
*
7+
* @package Noname\Common
8+
*/
9+
class Arr
10+
{
11+
/**
12+
* Flatten an associative array using a custom separator.
13+
*
14+
* By default this method will use a dot (.) as the separator.
15+
*
16+
* @param array $array
17+
* @param string $separator
18+
* @param string $prepend
19+
* @return array
20+
*/
21+
public static function flatten(array $array, string $separator = '.', string $prepend = ''): array
22+
{
23+
$flatArray = [];
24+
25+
foreach ($array as $key => $value) {
26+
if (is_array($value) && !empty($value)) {
27+
$flatArray += self::flatten($value, $separator, $prepend . $key . $separator);
28+
} else {
29+
$flatArray[$prepend . $key] = $value;
30+
}
31+
}
32+
33+
return $flatArray;
34+
}
35+
}

src/Collection.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare(strict_types = 1);
22
namespace Noname\Common;
33

44
use ArrayAccess;
@@ -116,7 +116,7 @@ public function pluck($key, $default = null)
116116
*
117117
* @return array
118118
*/
119-
public function all() : array
119+
public function all(): array
120120
{
121121
return $this->toArray();
122122
}
@@ -126,7 +126,7 @@ public function all() : array
126126
*
127127
* @return array
128128
*/
129-
public function keys() : array
129+
public function keys(): array
130130
{
131131
return array_keys($this->items);
132132
}
@@ -136,7 +136,7 @@ public function keys() : array
136136
*
137137
* @return array
138138
*/
139-
public function values() : array
139+
public function values(): array
140140
{
141141
return array_values($this->items);
142142
}
@@ -147,7 +147,7 @@ public function values() : array
147147
* @param string $key
148148
* @return boolean
149149
*/
150-
public function has($key) : bool
150+
public function has($key): bool
151151
{
152152
return isset($this->items[$key]);
153153
}
@@ -161,7 +161,7 @@ public function has($key) : bool
161161
* @return boolean
162162
* @throws \InvalidArgumentException
163163
*/
164-
public function is($key, $value, $operator = null) : bool
164+
public function is($key, $value, $operator = null): bool
165165
{
166166
$keyValue = $this->get($key);
167167

@@ -207,11 +207,21 @@ public function destroy()
207207
*
208208
* @return array
209209
*/
210-
public function toArray() : array
210+
public function toArray(): array
211211
{
212212
return $this->items;
213213
}
214214

215+
/**
216+
* Flatten collection with dot (.) separator.
217+
*
218+
* @return array
219+
*/
220+
public function flatten(): array
221+
{
222+
return Arr::flatten($this->items);
223+
}
224+
215225
///////////////////////////////////
216226
// IteratorAggregate Methods
217227

tests/ArrTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
namespace Noname\Common;
3+
4+
class ArrTest extends \PHPUnit_Framework_TestCase
5+
{
6+
public function testFlatten()
7+
{
8+
$array = ['a' => 'b', 'c' => ['d' => 'e', 'f' => ['g' => [1, 2, 3 => []]]]];
9+
10+
$flatArray = Arr::flatten($array);
11+
12+
// Check for keys
13+
$this->assertArrayHasKey('a', $flatArray);
14+
$this->assertArrayHasKey('c.d', $flatArray);
15+
$this->assertArrayHasKey('c.f.g.0', $flatArray);
16+
$this->assertArrayHasKey('c.f.g.1', $flatArray);
17+
$this->assertArrayHasKey('c.f.g.3', $flatArray);
18+
19+
// Check for values
20+
$this->assertEquals('b', $flatArray['a']);
21+
$this->assertEquals('e', $flatArray['c.d']);
22+
$this->assertEquals(1, $flatArray['c.f.g.0']);
23+
$this->assertEquals(2, $flatArray['c.f.g.1']);
24+
$this->assertEquals([], $flatArray['c.f.g.3']);
25+
}
26+
}

0 commit comments

Comments
 (0)