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

Conversation

pedroluislopez
Copy link

This implementation uses fillable attributes from eloquent models to avoid repetition of these attributes declaration in the schema and hydrator classes (by default, if attributes are declared in schema/hydrator class, these are used).

@pedroluislopez pedroluislopez changed the title Get default attributes for schemas and hydrators from model filleable attribute Get default attributes for schemas and hydrators from model fillable attribute Feb 28, 2017
@lindyhopchris lindyhopchris changed the base branch from master to develop February 28, 2017 12:14
Copy link
Member

@lindyhopchris lindyhopchris left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this. I've had a think about it and think it needs the following changes. Let me know if you have any comments in response!

* @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

protected function defaultHydrateAttributes(StandardObjectInterface $attributes, $record)
{
$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

@@ -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

* @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))
{
$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

protected function getDefaultModelAttributes(Model $model)
{
$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

@@ -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

@pedroluislopez
Copy link
Author

@lindyhopchris I make a PR from a branch only with this changes. I have made other changes about register adapters. Sorry.

@pedroluislopez
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants