-
Notifications
You must be signed in to change notification settings - Fork 106
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
Changes from 1 commit
6b71b84
78e2852
75bd121
1ef830a
050e267
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
$hydratorAttributes = $this->attributes; | ||
if(empty($hydratorAttributes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
{ | ||
$hydratorAttributes = []; | ||
foreach($record->getFillable() as $attribute) | ||
{ | ||
if(strpos($attribute, '_') !== false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to convert the attribute key you should use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
{ | ||
$hydratorAttributes[str_replace('_', '-', $attribute)] = $attribute; | ||
} | ||
else | ||
{ | ||
$hydratorAttributes[] = $attribute; | ||
} | ||
} | ||
} | ||
return $hydratorAttributes; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
if (is_numeric($resourceKey)) { | ||
$resourceKey = $modelKey; | ||
$modelKey = $this->keyForAttribute($modelKey, $record); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency this can also be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, the function name would be getDefaultModelAttributesKeys? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function name would be |
||
{ | ||
$schemaAttributes = $this->attributes; | ||
if(empty($schemaAttributes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
{ | ||
$schemaAttributes = $model->getFillable(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If If using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
} | ||
return $schemaAttributes; | ||
} | ||
|
||
|
||
/** | ||
* Get attributes for the provided model. | ||
* | ||
|
@@ -164,7 +181,7 @@ protected function getModelAttributes(Model $model) | |
{ | ||
$attributes = []; | ||
|
||
foreach ($this->attributes as $modelKey => $attributeKey) { | ||
foreach ($this->getDefaultModelAttributes($model) as $modelKey => $attributeKey) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
if (is_numeric($modelKey)) { | ||
$modelKey = $attributeKey; | ||
$attributeKey = $this->keyForAttribute($attributeKey); | ||
|
There was a problem hiding this comment.
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 thisEloquentHydrator
). The method signature is correct.There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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