Skip to content

Commit 67c2de3

Browse files
author
Steve Porter
committed
feat: initial commit
1 parent 92ff116 commit 67c2de3

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.project
2+
*.sublime-project
3+
*.sublime-workspace
4+
.DS_Store
5+
.idea/
6+
composer.lock
7+
composer.phar
8+
phpunit.phar
9+
vendor

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
env:
9+
matrix:
10+
- COMPOSER_FLAGS="--prefer-lowest"
11+
- COMPOSER_FLAGS=""
12+
13+
before_script:
14+
- travis_retry composer self-update
15+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
16+
17+
script:
18+
- vendor/bin/phpunit

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-recursive-collection` will be documented in this file
4+
5+
## 1.0.0 - 2018-01-10
6+
- initial release

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Laravel Recursive Collection
2+
===============
3+
4+
[![Latest Stable Version](http://img.shields.io/github/release/designmynight/laravel-recursive-collection.svg)](https://packagist.org/packages/designmynight/laravel-recursive-collection) [![Total Downloads](http://img.shields.io/packagist/dm/designmynight/laravel-recursive-collection.svg)](https://packagist.org/packages/designmynight/laravel-recursive-collection)
5+
6+
A service provider to add support for for converting nested arrays and associve arrays into laravel collections
7+
8+
Table of contents
9+
-----------------
10+
* [Installation](#installation)
11+
* [Example](#example)
12+
13+
Installation
14+
------------
15+
16+
Installation using composer:
17+
18+
```sh
19+
composer require designmynight/laravel-recursive-collection
20+
```
21+
22+
### Laravel version Compatibility
23+
24+
Laravel | Package
25+
:---------|:----------
26+
5.5.x | 1.0.x
27+
28+
And add the service provider in `config/app.php`:
29+
30+
```php
31+
DesignMyNight\Laravel\RecursiveCollectionServiceProvider::class,
32+
```
33+
34+
For usage with [Lumen](http://lumen.laravel.com), add the service provider in `bootstrap/app.php`.
35+
36+
```php
37+
$app->register(DesignMyNight\Laravel\RecursiveCollectionServiceProvider::class);
38+
```
39+
40+
Example
41+
------------
42+
43+
```
44+
$data = [
45+
[
46+
'name' => 'John Doe',
47+
'email' => '[email protected]',
48+
'bookings' => [
49+
[
50+
'venue' => 'Venue A',
51+
'date' => '2000-01-01'
52+
'guests' => 2
53+
],
54+
[
55+
'venue' => 'Venue B',
56+
'date' => '2001-01-01'
57+
'guests' => 2
58+
],
59+
],
60+
],
61+
];
62+
63+
$collection = (new Collection($data))->recursive();
64+
$collection = collect($data)->recursive(); // Shorthand
65+
```

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "designmynight/laravel-recursive-collection",
3+
"description": "A package to convert nested arrays and associative arrays into nested collections",
4+
"homepage": "https://github.com/designmynight/laravel-recursive-collection",
5+
"license": "MIT",
6+
"keywords": [
7+
"laravel",
8+
"laravel-collection",
9+
"laravel-recursive-collection",
10+
"collection",
11+
"designmynight"
12+
],
13+
"require": {
14+
"illuminate/support": "^5.5"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"DesignMyNight\\Laravel\\": "src"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"DesignMyNight\\Laravel\\Tests\\": "tests"
24+
}
25+
},
26+
"authors": [
27+
{
28+
"name": "Steve Porter",
29+
"email": "[email protected]",
30+
"role": "Developer"
31+
}
32+
],
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"DesignMyNight\\Laravel\\RecursiveCollectionServiceProvider"
37+
]
38+
}
39+
},
40+
"require-dev": {
41+
"orchestra/testbench": "^3.5",
42+
"phpunit/phpunit": "^6.5"
43+
}
44+
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
verbose="true">
12+
<testsuites>
13+
<testsuite name="DesignMyNight Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace DesignMyNight\Laravel;
3+
4+
use Illuminate\Support\Collection;
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class RecursiveCollectionServiceProvider extends ServiceProvider
8+
{
9+
public function register()
10+
{
11+
Collection::macro('recursive', function () {
12+
return $this->map(function ($value) {
13+
if (is_array($value) || is_object($value)) {
14+
return (new Collection($value))->recursive();
15+
}
16+
17+
return $value;
18+
});
19+
});
20+
}
21+
}

tests/CollectionTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace DesignMyNight\Laravel\Tests;
3+
4+
use DesignMyNight\Laravel\Tests\TestCase;
5+
use Illuminate\Support\Collection;
6+
7+
class CollectionTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function it_converts_nested_arrays_into_collections()
13+
{
14+
$collection = (new Collection([
15+
[1, 2, 3],
16+
]))->recursive();
17+
18+
$this->assertInstanceOf(Collection::class, $collection[0]);
19+
}
20+
21+
/**
22+
* @test
23+
*/
24+
public function it_converts_deep_nested_arrays_into_collections()
25+
{
26+
$collection = (new Collection([
27+
[
28+
[1, 2, 3],
29+
],
30+
]))->recursive();
31+
32+
$this->assertInstanceOf(Collection::class, $collection[0][0]);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function it_converts_nested_associative_arrays_into_collections()
39+
{
40+
$collection = (new Collection([
41+
'a' => [1, 2, 3],
42+
]))->recursive();
43+
44+
$this->assertInstanceOf(Collection::class, $collection['a']);
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function it_converts_deep_nested_associative_arrays_into_collections()
51+
{
52+
$collection = (new Collection([
53+
'a' => [
54+
'a' => [1, 2, 3],
55+
],
56+
]))->recursive();
57+
58+
$this->assertInstanceOf(Collection::class, $collection['a']['a']);
59+
}
60+
}

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace DesignMyNight\Laravel\Tests;
3+
4+
use DesignMyNight\Laravel\RecursiveCollectionServiceProvider;
5+
use Orchestra\Testbench\TestCase as Orchestra;
6+
7+
abstract class TestCase extends Orchestra
8+
{
9+
protected function getPackageProviders($app)
10+
{
11+
return [RecursiveCollectionServiceProvider::class];
12+
}
13+
}

0 commit comments

Comments
 (0)