From 51136ed450189c356aed398416dcadc0be1ef86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 13 Apr 2020 21:24:20 +0200 Subject: [PATCH 1/4] Add tests --- tests/Feature/Generator/ModelGeneratorTest.php | 6 ++++++ tests/fixtures/definitions/nested-components.bp | 1 + tests/fixtures/models/nested-components.php | 9 +++++++++ 3 files changed, 16 insertions(+) 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/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. * From 36aa63c5a556ea0b9be89d62ad71084f504b7525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 13 Apr 2020 21:24:34 +0200 Subject: [PATCH 2/4] Add implementation --- src/Generators/ModelGenerator.php | 15 +++++++++++++++ stubs/model/hidden.stub | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 stubs/model/hidden.stub diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 99e301c2..43c24824 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,16 @@ private function fillableColumns(array $columns) ]); } + private function hiddenColumns(array $columns) + { + return array_filter(array_keys($columns), function ($column) { + return in_array($column, [ + '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 = []; From 64c5164d52a841d65186105a06e3e6697ccb5f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 13 Apr 2020 21:25:06 +0200 Subject: [PATCH 3/4] Side effect of modifying definition --- tests/fixtures/factories/nested-components.php | 1 + 1 file changed, 1 insertion(+) 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, ]; }); From 6d5853cf3c9502fec96d45f1c1f15d9914bfbcae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 13 Apr 2020 22:36:20 +0200 Subject: [PATCH 4/4] Refactor --- src/Generators/ModelGenerator.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 43c24824..9f154716 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -165,12 +165,10 @@ private function fillableColumns(array $columns) private function hiddenColumns(array $columns) { - return array_filter(array_keys($columns), function ($column) { - return in_array($column, [ - 'password', - 'remember_token', - ]); - }); + return array_intersect(array_keys($columns), [ + 'password', + 'remember_token', + ]); } private function castableColumns(array $columns)