Skip to content

Commit ee05e63

Browse files
committed
[Cleanup] Code cleanup and update doc blocks
1 parent 96b7af8 commit ee05e63

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## Unreleased
6+
7+
### Added
8+
- Default Eloquent hydration attributes are now calculated using `Model::getFillable()`. To use this,
9+
set the `$attributes` property on an Eloquent hydrator to `null`.
10+
- Default Eloquent serialization attribution in the `EloquentSchema` class. These are calculated using
11+
either `Model::getVisible()` or `Model::getFillable()` minus any `Model::getHidden()` keys. To use this,
12+
set the `$attributes` property on an Eloquent schema to `null`.
13+
14+
### Changed
15+
- When generating an Eloquent hydrator or schema using the `make:json-api` commands, the `$attributes`
16+
property will be set as `null` by default.
17+
518
## [0.6.2] - 2017-02-23
619

720
### Fixed

src/Hydrator/EloquentHydrator.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* Class EloquentHydrator
41+
*
4142
* @package CloudCreativity\LaravelJsonApi
4243
*/
4344
class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterface
@@ -48,6 +49,10 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
4849
/**
4950
* The resource attribute keys to hydrate.
5051
*
52+
* - Empty array = hydrate no attributes.
53+
* - Non-empty array = hydrate the specified attribute keys (see below).
54+
* - Null = calculate the attributes to hydrate using `Model::getFillable()`
55+
*
5156
* List the keys from the resource's attributes that should be transferred to your
5257
* model using the `fill()` method. To map a resource attribute key to a different
5358
* model key, use a key/value pair where the key is the resource attribute and the
@@ -68,9 +73,12 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
6873
* attribute will be converted to `foo_bar` if the Model uses snake case attributes,
6974
* or `fooBar` if it does not use snake case.
7075
*
71-
* @var array
76+
* If this property is `null`, the attributes to hydrate will be calculated using
77+
* `Model::getFillable()`.
78+
*
79+
* @var array|null
7280
*/
73-
protected $attributes = NULL;
81+
protected $attributes = null;
7482

7583
/**
7684
* The resource attributes that are dates.
@@ -109,24 +117,27 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
109117

110118
/**
111119
* EloquentHydrator constructor.
120+
*
112121
* @param HttpServiceInterface $service
113122
*/
114123
public function __construct(HttpServiceInterface $service)
115124
{
116125
$this->service = $service;
117126
}
118-
127+
119128
/**
120-
* @param $record
121-
* @return void
129+
* @param StandardObjectInterface $attributes
130+
* the attributes received from the client.
131+
* @param Model $record
132+
* the model being hydrated
133+
* @return array
134+
* the JSON API attribute keys to hydrate
122135
*/
123-
protected function attributeKeys($record)
136+
protected function attributeKeys(StandardObjectInterface $attributes, $record)
124137
{
125-
if(is_null($this->attributes))
126-
{
138+
if (is_null($this->attributes)) {
127139
$fillableAttributes = [];
128-
foreach($record->getFillable() as $attribute)
129-
{
140+
foreach ($record->getFillable() as $attribute) {
130141
$fillableAttributes[Str::dasherize($attribute)] = $attribute;
131142
}
132143
return $fillableAttributes;
@@ -145,7 +156,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor
145156

146157
$data = [];
147158

148-
foreach ($this->attributeKeys($record) as $resourceKey => $modelKey) {
159+
foreach ($this->attributeKeys($attributes, $record) as $resourceKey => $modelKey) {
149160
if (is_numeric($resourceKey)) {
150161
$resourceKey = $modelKey;
151162
$modelKey = $this->keyForAttribute($modelKey, $record);
@@ -193,7 +204,7 @@ public function hydrateRelated(ResourceInterface $resource, $record)
193204
foreach ($resource->getRelationships()->getAll() as $key => $relationship) {
194205

195206
/** If there is a specific method for this related member, we'll hydrate that */
196-
$related = $this->callHydrateRelated($key, $relationship, $record);
207+
$related = $this->callHydrateRelatedRelationship($key, $relationship, $record);
197208

198209
if (false !== $related) {
199210
$results = array_merge($results, $related);

src/Schema/EloquentSchema.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
/**
2828
* Class EloquentSchema
29+
*
2930
* @package CloudCreativity\LaravelJsonApi
3031
*/
3132
abstract class EloquentSchema extends AbstractSchema
@@ -80,9 +81,18 @@ abstract class EloquentSchema extends AbstractSchema
8081
/**
8182
* The model attribute keys to serialize.
8283
*
83-
* @var array
84+
* - Empty array: no attributes to serialize.
85+
* - Non-empty array: serialize the specified model keys.
86+
* - Null: work out the keys to serialize.
87+
*
88+
* If `null`, then `Model::getVisible()` is used if it returns a non-empty array.
89+
* Otherwise, `Model::getFillable()` will be used, minus any keys specified in
90+
* `Model::getHidden()`. We use `getFillable` because that is also the default
91+
* in `EloquentHydrator`.
92+
*
93+
* @var array|null
8494
*/
85-
protected $attributes = NULL;
95+
protected $attributes = null;
8696

8797
/**
8898
* Whether resource member names are hyphenated
@@ -162,18 +172,15 @@ protected function getDefaultAttributes(Model $model)
162172
*/
163173
protected function attributeKeys(Model $model)
164174
{
165-
if(is_null($this->attributes))
166-
{
167-
if(! empty($model->getVisible()))
168-
{
175+
if (is_null($this->attributes)) {
176+
if (!empty($model->getVisible())) {
169177
return $model->getVisible();
170178
}
171179
return array_diff($model->getFillable(), $model->getHidden());
172180
}
173181
return $this->attributes;
174182
}
175183

176-
177184
/**
178185
* Get attributes for the provided model.
179186
*

stubs/eloquent/hydrator.stub

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ class Hydrator extends EloquentHydrator
88
{
99

1010
/**
11-
* @var array
11+
* @var array|null
1212
*/
13-
protected $attributes = [
14-
//
15-
];
13+
protected $attributes = null;
1614

1715
/**
1816
* @var array

stubs/eloquent/schema.stub

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ class Schema extends EloquentSchema
1313
const RESOURCE_TYPE = 'dummyResourceType';
1414

1515
/**
16-
* @var array
16+
* @var array|null
1717
*/
18-
protected $attributes = [
19-
//
20-
];
18+
protected $attributes = null;
2119

2220
/**
2321
* @return string

0 commit comments

Comments
 (0)