8000 [Feature] Add default Eloquent hydrator and schema attributes · GIANTCRAB/laravel-json-api@96b7af8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96b7af8

Browse files
[Feature] Add default Eloquent hydrator and schema attributes
Merge pull request cloudcreativity#51 from Neuromobile/default-attributes-from-fillable Get default attributes for schemas and hydrators from model fillable attribute
2 parents 3a696c9 + 0569582 commit 96b7af8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/Hydrator/EloquentHydrator.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
7070
*
7171
* @var array
7272
*/
73-
protected $attributes = [];
73+
protected $attributes = NULL;
7474

7575
/**
7676
* The resource attributes that are dates.
@@ -115,6 +115,24 @@ public function __construct(HttpServiceInterface $service)
115115
{
116116
$this->service = $service;
117117
}
118+
119+
/**
120+
* @param $record
121+
* @return void
122+
*/
123+
protected function attributeKeys($record)
124+
{
125+
if(is_null($this->attributes))
126+
{
127+
$fillableAttributes = [];
128+
foreach($record->getFillable() as $attribute)
129+
{
130+
$fillableAttributes[Str::dasherize($attribute)] = $attribute;
131+
}
132+
return $fillableAttributes;
133+
}
134+
return $this->attributes;
135+
}
118136

119137
/**
120138
* @inheritDoc
@@ -127,7 +145,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor
127145

128146
$data = [];
129147

130-
foreach ($this->attributes as $resourceKey => $modelKey) {
148+
foreach ($this->attributeKeys($record) as $resourceKey => $modelKey) {
131149
if (is_numeric($resourceKey)) {
132150
$resourceKey = $modelKey;
133151
$modelKey = $this->keyForAttribute($modelKey, $record);

src/Schema/EloquentSchema.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ abstract class EloquentSchema extends AbstractSchema
8282
*
8383
* @var array
8484
*/
85-
protected $attributes = [];
85+
protected $attributes = NULL;
8686

8787
/**
8888
* Whether resource member names are hyphenated
@@ -154,6 +154,26 @@ protected function getDefaultAttributes(Model $model)
154154
return $defaults;
155155
}
156156

157+
/**
158+
* Get attributes for the provided model using fillable attribute.
159+
*
160+
* @param Model $model
161+
* @return array
162+
*/
163+
protected function attributeKeys(Model $model)
164+
{
165+
if(is_null($this->attributes))
166+
{
167+
if(! empty($model->getVisible()))
168+
{
169+
return $model->getVisible();
170+
}
171+
return array_diff($model->getFillable(), $model->getHidden());
172+
}
173+
return $this->attributes;
174+
}
175+
176+
157177
/**
158178
* Get attributes for the provided model.
159179
*
@@ -164,7 +184,7 @@ protected function getModelAttributes(Model $model)
164184
{
165185
$attributes = [];
166186

167-
foreach ($this->attributes as $modelKey => $attributeKey) {
187+
foreach ($this->attributeKeys($model) as $modelKey => $attributeKey) {
168188
if (is_numeric($modelKey)) {
169189
$modelKey = $attributeKey;
170190
$attributeKey = $this->keyForAttribute($attributeKey);

0 commit comments

Comments
 (0)
0