Skip to content

Get default attributes for schemas and hydrators from model fillable attribute #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
22 changes: 20 additions & 2 deletions src/Hydrator/EloquentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
*
* @var array
*/
protected $attributes = [];
protected $attributes = NULL;

/**
* The resource attributes that are dates.
Expand Down Expand Up @@ -115,6 +115,24 @@ public function __construct(HttpServiceInterface $service)
{
$this->service = $service;
}

/**
* @param $record
* @return void
*/
protected function attributeKeys($record)
{
if(is_null($this->attributes))
{
$fillableAttributes = [];
foreach($record->getFillable() as $attribute)
{
$fillableAttributes[Str::dasherize($attribute)] = $attribute;
}
return $fillableAttributes;
}
return $this->attributes;
}

/**
* @inheritDoc
Expand All @@ -127,7 +145,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor

$data = [];

foreach ($this->attributes as $resourceKey => $modelKey) {
foreach ($this->attributeKeys($record) as $resourceKey => $modelKey) {
if (is_numeric($resourceKey)) {
$resourceKey = $modelKey;
$modelKey = $this->keyForAttribute($modelKey, $record);
Expand Down
24 changes: 22 additions & 2 deletions src/Schema/EloquentSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ abstract class EloquentSchema extends AbstractSchema
*
* @var array
*/
protected $attributes = [];
protected $attributes = NULL;

/**
* Whether resource member names are hyphenated
Expand Down Expand Up @@ -154,6 +154,26 @@ protected function getDefaultAttributes(Model $model)
return $defaults;
}

/**
* Get attributes for the provided model using fillable attribute.
*
* @param Model $model
* @return array
*/
protected function attributeKeys(Model $model)
{
if(is_null($this->attributes))
{
if(! empty($model->getVisible()))
{
return $model->getVisible();
}
return array_diff($model->getFillable(), $model->getHidden());
}
return $this->attributes;
}


/**
* Get attributes for the provided model.
*
Expand All @@ -164,7 +184,7 @@ protected function getModelAttributes(Model $model)
{
$attributes = [];

foreach ($this->attributes as $modelKey => $attributeKey) {
foreach ($this->attributeKeys($model) as $modelKey => $attributeKey) {
if (is_numeric($modelKey)) {
$modelKey = $attributeKey;
$attributeKey = $this->keyForAttribute($attributeKey);
Expand Down