diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 99e301c2..9f154716 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -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')); @@ -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( diff --git a/stubs/model/hidden.stub b/stubs/model/hidden.stub new file mode 100644 index 00000000..5850f9ae --- /dev/null +++ b/stubs/model/hidden.stub @@ -0,0 +1,6 @@ + /** + * The attributes that should be hidden for serialization. + * + * @var array + */ + protected $hidden = []; diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 5edae1f4..64f6e582 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -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')); diff --git a/tests/fixtures/definitions/nested-components.bp b/tests/fixtures/definitions/nested-components.bp index 297763ae..dd7a244d 100644 --- a/tests/fixtures/definitions/nested-components.bp +++ b/tests/fixtures/definitions/nested-components.bp @@ -1,6 +1,7 @@ models: Admin/User: name: string + password: password controllers: Admin/User: diff --git a/tests/fixtures/factories/nested-components.php b/tests/fixtures/factories/nested-components.php index bc73b11d..ba3dbdc9 100644 --- a/tests/fixtures/factories/nested-components.php +++ b/tests/fixtures/factories/nested-components.php @@ -8,5 +8,6 @@ $factory->define(User::class, function (Faker $faker) { return [ 'name' => $faker->name, + 'password' => $faker->password, ]; }); diff --git a/tests/fixtures/models/nested-components.php b/tests/fixtures/models/nested-components.php index 60a51d22..e05da0c9 100644 --- a/tests/fixtures/models/nested-components.php +++ b/tests/fixtures/models/nested-components.php @@ -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. *