Skip to content

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

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion src/Hydrator/EloquentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ public function __construct(HttpServiceInterface $service)
{
$this->service = $service;
}

/**
* @param StandardObjectInterface $attributes
* @param $record
* @return void
*/
protected function defaultHydrateAttributes(StandardObjectInterface $attributes, $record)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be called attributeKeys for consistency with a trait that exists (which will at some point be used in this EloquentHydrator). The method signature is correct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the function name would be defaultHydrateAttributeKeys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name would just be attributeKeys

{
$hydratorAttributes = $this->attributes;
if(empty($hydratorAttributes))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to use is_null instead of empty. I.e. an empty array can mean "hydrate no attributes" and a null value can mean "hydrate fillable attributes"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

{
$hydratorAttributes = [];
foreach($record->getFillable() as $attribute)
{
if(strpos($attribute, '_') !== false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to convert the attribute key you should use the Str::dasherize method (the Str class already has a use statement). There's no need to us strpos - just run the attribute key through the dasherize method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

{
$hydratorAttributes[str_replace('_', '-', $attribute)] = $attribute;
}
else
{
$hydratorAttributes[] = $attribute;
}
}
}
return $hydratorAttributes;
}

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

$data = [];

foreach ($this->attributes as $resourceKey => $modelKey) {
foreach ($this->defaultHydrateAttributes($attributes, $record) as $resourceKey => $modelKey) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributeKeys method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

if (is_numeric($resourceKey)) {
$resourceKey = $modelKey;
$modelKey = $this->keyForAttribute($modelKey, $record);
Expand Down
19 changes: 18 additions & 1 deletion src/Schema/EloquentSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ protected function getDefaultAttributes(Model $model)
return $defaults;
}

/**
* Get attributes for the provided model using fillable attribute.
*
* @param Model $model
* @return array
*/
protected function getDefaultModelAttributes(Model $model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency this can also be attributeKeys

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the function name would be getDefaultModelAttributesKeys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name would be attributeKeys

{
$schemaAttributes = $this->attributes;
if(empty($schemaAttributes))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use is_null as per my comment on the hydrator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

{
$schemaAttributes = $model->getFillable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $model->getVisible() is not empty, that list should be used (as it is a white-list of serializable attributes).

If using $model->getFillable(), you'll need to remove any keys that are in $model->getHidden().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

}
return $schemaAttributes;
}


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

foreach ($this->attributes as $modelKey => $attributeKey) {
foreach ($this->getDefaultModelAttributes($model) as $modelKey => $attributeKey) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributeKeys

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

if (is_numeric($modelKey)) {
$modelKey = $attributeKey;
$attributeKey = $this->keyForAttribute($attributeKey);
Expand Down