Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ private function buildProperties(Model $model)
$properties .= $this->files->stub('model/fillable.stub');
}

$columns = $this->hiddenColumns($model->columns());
if (!empty($columns)) {
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/hidden.stub'));
}

$columns = $this->castableColumns($model->columns());
if (!empty($columns)) {
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns), $this->files->stub('model/casts.stub'));
Expand Down Expand Up @@ -158,6 +163,14 @@ private function fillableColumns(array $columns)
]);
}

private function hiddenColumns(array $columns)
{
return array_intersect(array_keys($columns), [
'password',
'remember_token',
]);
}

private function castableColumns(array $columns)
{
return array_filter(array_map(
Expand Down
6 changes: 6 additions & 0 deletions stubs/model/hidden.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [];
6 changes: 6 additions & 0 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function output_generates_models($definition, $path, $model)
->with('model/fillable.stub')
->andReturn(file_get_contents('stubs/model/fillable.stub'));

if ($definition === 'definitions/nested-components.bp') {
$this->files->expects('stub')
->with('model/hidden.stub')
->andReturn(file_get_contents('stubs/model/hidden.stub'));
}

$this->files->expects('stub')
->with('model/casts.stub')
->andReturn(file_get_contents('stubs/model/casts.stub'));
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/definitions/nested-components.bp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
models:
Admin/User:
name: string
password: password

controllers:
Admin/User:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/factories/nested-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'password' => $faker->password,
];
});
9 changes: 9 additions & 0 deletions tests/fixtures/models/nested-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class User extends Model
'name',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
];

/**
* The attributes that should be cast to native types.
*
Expand Down