Skip to content

Commit 665e93f

Browse files
authored
Allow configuration to use $guarded = []; (#173)
1 parent 9df2071 commit 665e93f

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

config/blueprint.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@
6969
'use_constraints' => false,
7070

7171
'fake_nullables' => true,
72+
73+
/*
74+
|--------------------------------------------------------------------------
75+
| Models
76+
|--------------------------------------------------------------------------
77+
|
78+
| Here you may choose to use $guarded instead $fillable.
79+
|
80+
*/
81+
82+
'use_guarded' => false,
7283
];

src/Generators/ModelGenerator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ private function buildProperties(Model $model)
9292
{
9393
$properties = '';
9494

95-
$columns = $this->fillableColumns($model->columns());
96-
if (!empty($columns)) {
97-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub'));
95+
if (config('blueprint.use_guarded')) {
96+
$properties .= $this->files->stub('model/guarded.stub');
9897
} else {
99-
$properties .= $this->files->stub('model/fillable.stub');
98+
$columns = $this->fillableColumns($model->columns());
99+
if (!empty($columns)) {
100+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub'));
101+
} else {
102+
$properties .= $this->files->stub('model/fillable.stub');
103+
}
100104
}
101105

102106
$columns = $this->hiddenColumns($model->columns());

stubs/model/guarded.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The attributes that aren't mass assignable.
3+
*
4+
* @var array
5+
*/
6+
protected $guarded = [];

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public function output_generates_phpdoc_for_model($definition, $path, $model)
197197
$this->files->expects('exists')
198198
->with(dirname($path))
199199
->andReturnTrue();
200+
200201
$this->files->expects('put')
201202
->with($path, $this->fixture($model));
202203

@@ -206,6 +207,42 @@ public function output_generates_phpdoc_for_model($definition, $path, $model)
206207
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
207208
}
208209

210+
/**
211+
* @test
212+
*/
213+
public function output_generates_models_with_guarded_property_when_config_option_is_set()
214+
{
215+
$this->app['config']->set('blueprint.use_guarded', true);
216+
217+
$this->files->expects('stub')
218+
->with('model/class.stub')
219+
->andReturn(file_get_contents('stubs/model/class.stub'));
220+
221+
$this->files->expects('stub')
222+
->with('model/guarded.stub')
223+
->andReturn(file_get_contents('stubs/model/guarded.stub'));
224+
225+
$this->files->expects('stub')
226+
->with('model/casts.stub')
227+
->andReturn(file_get_contents('stubs/model/casts.stub'));
228+
229+
$this->files->shouldReceive('stub')
230+
->with('model/method.stub')
231+
->andReturn(file_get_contents('stubs/model/method.stub'));
232+
233+
$this->files->expects('exists')
234+
->with(dirname('app/Comment.php'))
235+
->andReturnTrue();
236+
237+
$this->files->expects('put')
238+
->with('app/Comment.php', $this->fixture('models/model-guarded.php'));
239+
240+
$tokens = $this->blueprint->parse($this->fixture('definitions/model-guarded.bp'));
241+
$tree = $this->blueprint->analyze($tokens);
242+
243+
$this->assertEquals(['created' => ['app/Comment.php']], $this->subject->output($tree));
244+
}
245+
209246
public function modelTreeDataProvider()
210247
{
211248
return [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
models:
2+
Comment:
3+
content: text
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Comment extends Model
8+
{
9+
/**
10+
* The attributes that aren't mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $guarded = [];
15+
16+
/**
17+
* The attributes that should be cast to native types.
18+
*
19+
* @var array
20+
*/
21+
protected $casts = [
22+
'id' => 'integer',
23+
];
24+
}

0 commit comments

Comments
 (0)