Skip to content

Commit b87de46

Browse files
authored
Add $hidden attribute to Models (#98)
1 parent 0b06d99 commit b87de46

File tree

6 files changed

+36
-0
lines changed

6 files changed

+36
-0
lines changed

src/Generators/ModelGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ private function buildProperties(Model $model)
9999
$properties .= $this->files->stub('model/fillable.stub');
100100
}
101101

102+
$columns = $this->hiddenColumns($model->columns());
103+
if (!empty($columns)) {
104+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/hidden.stub'));
105+
}
106+
102107
$columns = $this->castableColumns($model->columns());
103108
if (!empty($columns)) {
104109
$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)
158163
]);
159164
}
160165

166+
private function hiddenColumns(array $columns)
167+
{
168+
return array_intersect(array_keys($columns), [
169+
'password',
170+
'remember_token',
171+
]);
172+
}
173+
161174
private function castableColumns(array $columns)
162175
{
163176
return array_filter(array_map(

stubs/model/hidden.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The attributes that should be hidden for serialization.
3+
*
4+
* @var array
5+
*/
6+
protected $hidden = [];

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function output_generates_models($definition, $path, $model)
5555
->with('model/fillable.stub')
5656
->andReturn(file_get_contents('stubs/model/fillable.stub'));
5757

58+
if ($definition === 'definitions/nested-components.bp') {
59+
$this->files->expects('stub')
60+
->with('model/hidden.stub')
61+
->andReturn(file_get_contents('stubs/model/hidden.stub'));
62+
}
63+
5864
$this->files->expects('stub')
5965
->with('model/casts.stub')
6066
->andReturn(file_get_contents('stubs/model/casts.stub'));

tests/fixtures/definitions/nested-components.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
models:
22
Admin/User:
33
name: string
4+
password: password
45

56
controllers:
67
Admin/User:

tests/fixtures/factories/nested-components.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
$factory->define(User::class, function (Faker $faker) {
99
return [
1010
'name' => $faker->name,
11+
'password' => $faker->password,
1112
];
1213
});

tests/fixtures/models/nested-components.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ class User extends Model
1515
'name',
1616
];
1717

18+
/**
19+
* The attributes that should be hidden for serialization.
20+
*
21+
* @var array
22+
*/
23+
protected $hidden = [
24+
'password',
25+
];
26+
1827
/**
1928
* The attributes that should be cast to native types.
2029
*

0 commit comments

Comments
 (0)