Skip to content

Commit c35e107

Browse files
authored
Merge pull request #6 from mits87/feature/laravel-6
Laravel 6 compatible + fix travis
2 parents 605ad1f + 4f8e25d commit c35e107

8 files changed

+72
-52
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/.php_cs
1+
.php_cs.cache
2+
.phpunit.result.cache
23
/composer.lock
34
/phpunit.xml
45
/vendor/

.php_cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require __DIR__.'/vendor/autoload.php';
4+
5+
return (new MattAllan\LaravelCodeStyle\Config())
6+
->setFinder(
7+
PhpCsFixer\Finder::create()->in('./')
8+
)
9+
->setRules([
10+
'@Laravel' => true,
11+
'@Laravel:risky' => true,
12+
'concat_space' => [ 'spacing' => 'one' ],
13+
])
14+
->setRiskyAllowed(true);

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ git:
1010

1111
matrix:
1212
include:
13-
- php: 7.0
14-
- php: 7.1
15-
- php: nightly
13+
- php: 7.2
14+
- php: 7.3
15+
- php: 7.4
1616
fast_finish: true
1717

1818
cache:

composer.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=7.1",
19-
"illuminate/database": "^5.6",
20-
"illuminate/support": "~5.6"
18+
"php": ">=7.2",
19+
"illuminate/database": "^6.8",
20+
"illuminate/support": "^6.8"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "~7.0",
24-
"squizlabs/php_codesniffer": "^3.0",
25-
"mockery/mockery": "~1.0"
23+
"phpunit/phpunit": "^8.5",
24+
"matt-allan/laravel-code-style": "^0.5.0",
25+
"mockery/mockery": "^1.3"
2626
},
2727
"autoload": {
2828
"psr-4": {
@@ -35,9 +35,8 @@
3535
}
3636
},
3737
"scripts": {
38-
"test": [
39-
"vendor/bin/phpunit --colors=always"
40-
]
38+
"test": "vendor/bin/phpunit --colors=always",
39+
"csfix": "php-cs-fixer fix"
4140
},
4241
"config": {
4342
"sort-packages": true

src/Model.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ abstract class Model extends Eloquent
99
/*
1010
* Make nested attributes for model.
1111
*
12-
* Nested attributes allow you to save attributes on associated records through the parent.
13-
* By default nested attribute updating is turned off and you can enable it using the $nested array.
12+
* Nested attributes allow you to save attributes on associated records through the parent.
13+
* By default nested attribute updating is turned off and you can enable it using the $nested array.
1414
* When you enable nested attributes an attribute writer is defined on the model.
1515
*
1616
* @see Traits\HasNestedAttributesTrait
1717
*/
1818
use Traits\HasNestedAttributesTrait;
19-
}
19+
}

src/Traits/HasNestedAttributesTrait.php

+26-20
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33
namespace Eloquent\NestedAttributes\Traits;
44

55
use Exception;
6-
use Illuminate\Support\Facades\DB;
7-
use Illuminate\Database\Eloquent\Relations\HasOne;
8-
use Illuminate\Database\Eloquent\Relations\MorphOne;
6+
use Illuminate\Database\Eloquent\Relations;
97
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Illuminate\Database\Eloquent\Relations\HasOne;
109
use Illuminate\Database\Eloquent\Relations\MorphMany;
10+
use Illuminate\Database\Eloquent\Relations\MorphOne;
11+
use Illuminate\Support\Facades\DB;
1112

1213
trait HasNestedAttributesTrait
1314
{
1415
/**
15-
* Defined nested attributes
16+
* Defined nested attributes.
1617
*
1718
* @var array
1819
*/
1920
protected $acceptNestedAttributesFor = [];
2021

2122
/**
22-
* Defined "destroy" key name
23+
* Defined "destroy" key name.
2324
*
2425
* @var string
2526
*/
2627
protected $destroyNestedKey = '_destroy';
2728

28-
2929
/**
30-
* Get accept nested attributes
30+
* Get accept nested attributes.
3131
*
3232
* @return array
3333
*/
34-
public function getAcceptNestedAttributesFor()
34+
public function getAcceptNestedAttributesFor(): array
3535
{
3636
return $this->acceptNestedAttributesFor;
3737
}
@@ -44,9 +44,9 @@ public function getAcceptNestedAttributesFor()
4444
*
4545
* @throws \Illuminate\Database\Eloquent\MassAssignmentException
4646
*/
47-
public function fill(array $attributes)
47+
public function fill(array $attributes): self
4848
{
49-
if (!empty($this->nested)) {
49+
if (! empty($this->nested)) {
5050
$this->acceptNestedAttributesFor = [];
5151

5252
foreach ($this->nested as $attr) {
@@ -56,6 +56,7 @@ public function fill(array $attributes)
5656
}
5757
}
5858
}
59+
5960
return parent::fill($attributes);
6061
}
6162

@@ -65,30 +66,30 @@ public function fill(array $attributes)
6566
* @param array $options
6667
* @return bool
6768
*/
68-
public function save(array $options = [])
69+
public function save(array $options = []): bool
6970
{
7071
DB::beginTransaction();
7172

72-
if (!parent::save($options)) {
73+
if (! parent::save($options)) {
7374
return false;
7475
}
7576

7677
foreach ($this->getAcceptNestedAttributesFor() as $attribute => $stack) {
77-
$methodName = lcfirst(join(array_map('ucfirst', explode('_', $attribute))));
78+
$methodName = lcfirst(implode(array_map('ucfirst', explode('_', $attribute))));
7879

79-
if (!method_exists($this, $methodName)) {
80+
if (! method_exists($this, $methodName)) {
8081
throw new Exception('The nested atribute relation "' . $methodName . '" does not exists.');
8182
}
8283

8384
$relation = $this->$methodName();
8485

8586
if ($relation instanceof HasOne || $relation instanceof MorphOne) {
86-
if (!$this->saveNestedAttributes($relation, $stack)) {
87+
if (! $this->saveNestedAttributes($relation, $stack)) {
8788
return false;
8889
}
8990
} elseif ($relation instanceof HasMany || $relation instanceof MorphMany) {
9091
foreach ($stack as $params) {
91-
if (!$this->saveManyNestedAttributes($this->$methodName(), $params)) {
92+
if (! $this->saveManyNestedAttributes($this->$methodName(), $params)) {
9293
return false;
9394
}
9495
}
@@ -98,6 +99,7 @@ public function save(array $options = [])
9899
}
99100

100101
DB::commit();
102+
101103
return true;
102104
}
103105

@@ -108,16 +110,18 @@ public function save(array $options = [])
108110
* @param array $params
109111
* @return bool
110112
*/
111-
protected function saveNestedAttributes($relation, array $params)
113+
protected function saveNestedAttributes(Relations $relation, array $params): bool
112114
{
113115
if ($this->exists && $model = $relation->first()) {
114116
if ($this->allowDestroyNestedAttributes($params)) {
115117
return $model->delete();
116118
}
119+
117120
return $model->update($stack);
118121
} elseif ($relation->create($stack)) {
119122
return true;
120123
}
124+
121125
return false;
122126
}
123127

@@ -128,28 +132,30 @@ protected function saveNestedAttributes($relation, array $params)
128132
* @param array $params
129133
* @return bool
130134
*/
131-
protected function saveManyNestedAttributes($relation, array $params)
135+
protected function saveManyNestedAttributes($relation, array $params): bool
132136
{
133137
if (isset($params['id']) && $this->exists) {
134138
$model = $relation->findOrFail($params['id']);
135139

136140
if ($this->allowDestroyNestedAttributes($params)) {
137141
return $model->delete();
138142
}
143+
139144
return $model->update($params);
140145
} elseif ($relation->create($params)) {
141146
return true;
142147
}
148+
143149
return false;
144150
}
145151

146152
/**
147-
* Check can we delete nested data
153+
* Check can we delete nested data.
148154
*
149155
* @param array $params
150156
* @return bool
151157
*/
152-
protected function allowDestroyNestedAttributes(array $params)
158+
protected function allowDestroyNestedAttributes(array $params): bool
153159
{
154160
return isset($params[$this->destroyNestedKey]) && (bool) $params[$this->destroyNestedKey] == true;
155161
}

tests/HasNestedAttributesTraitTest.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
use PHPUnit\Framework\TestCase;
43
use Eloquent\NestedAttributes\Model;
4+
use PHPUnit\Framework\TestCase;
55

66
class HasNestedAttributesTraitTest extends TestCase
77
{
88
/**
99
* Set Up and Prepare Tests.
1010
*/
11-
public function setUp()
11+
public function setUp(): void
1212
{
1313
// Mock the Model that uses the custom traits
1414
$this->model = Mockery::mock('ModelEloquentStub');
@@ -24,22 +24,22 @@ public function setUp()
2424
'model_foos' => [
2525
['text' => 'foo1'],
2626
['text' => 'foo2'],
27-
]
27+
],
2828
];
2929
}
3030

3131
/**
3232
* Tear Down and Clean Up Tests.
3333
*/
34-
public function tearDown()
34+
public function tearDown(): void
3535
{
3636
Mockery::close();
3737
}
3838

3939
/**
4040
* The fillable test.
4141
*/
42-
public function testFillable()
42+
public function testFillable(): void
4343
{
4444
$this->model->fill(array_merge($this->payload, ['title' => 'foo', 'not_exists' => []]));
4545

@@ -48,32 +48,32 @@ public function testFillable()
4848
'model_bar' => ['text' => 'bar'],
4949
'model_foos' => [
5050
['text' => 'foo1'],
51-
['text' => 'foo2']
52-
]
51+
['text' => 'foo2'],
52+
],
5353
], $this->model->getAcceptNestedAttributesFor());
5454
}
5555

5656
/**
5757
* Test that a model with nested attributes can still save without fillable array.
5858
*/
59-
public function testModelWithNestedAttributesCanSaveWithoutFillableArraySet()
59+
public function testModelWithNestedAttributesCanSaveWithoutFillableArraySet(): void
6060
{
6161
$this->modelWithoutFillable->fill($this->payload);
6262

6363
$this->assertEquals([
6464
'model_bar' => ['text' => 'bar'],
6565
'model_foos' => [
6666
['text' => 'foo1'],
67-
['text' => 'foo2']
68-
]
67+
['text' => 'foo2'],
68+
],
6969
], $this->modelWithoutFillable->getAcceptNestedAttributesFor());
7070
}
7171
}
7272

7373
class ModelEloquentStubWithoutFillable extends Model
7474
{
75-
protected $table = 'stubs';
76-
protected $nested = ['model_bar', 'model_foos' ];
75+
protected $table = 'stubs';
76+
protected $nested = ['model_bar', 'model_foos'];
7777

7878
public function modelBar()
7979
{

tests/ModelTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ModelTest extends TestCase
1414
/**
1515
* Set Up and Prepare Tests.
1616
*/
17-
public function setUp()
17+
public function setUp(): void
1818
{
1919
// Mock the Model that uses the custom traits
2020
$this->model = Mockery::mock('ModelStub');
@@ -24,15 +24,15 @@ public function setUp()
2424
/**
2525
* Tear Down and Clean Up Tests.
2626
*/
27-
public function tearDown()
27+
public function tearDown(): void
2828
{
2929
Mockery::close();
3030
}
3131

3232
/**
3333
* Test that the Model uses the traits and in the right order.
3434
*/
35-
public function testModelUsesTraits()
35+
public function testModelUsesTraits(): void
3636
{
3737
// Get the traits off the model
3838
$traits = function_exists('class_uses_recursive') ?
@@ -48,4 +48,4 @@ class_uses_recursive(get_class($this->model)) : class_uses(get_class($this->mode
4848
*/
4949
class ModelStub extends Model
5050
{
51-
}
51+
}

0 commit comments

Comments
 (0)