8000 [Feature] Allow camel casing of include paths to be disabled · GIANTCRAB/laravel-json-api@8205613 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8205613

Browse files
committed
[Feature] Allow camel casing of include paths to be disabled
Adds a property on the includes models trait so that camel casing of include path segments can be disabled. Note that the default behaviour will remaing to camel case these as PSR1 says that method names on classes must be camel case. See cloudcreativity#315
1 parent 59ce758 commit 8205613

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ All notable changes to this project will be documented in this file. This projec
77
### Added
88
- [#315](https://github.com/cloudcreativity/laravel-json-api/issues/315)
99
Allow developers to use the exact JSON API field name as the relationship method name on their
10-
adapters. Although we recommend following the PSR1 standard of using camel case for method names,
11-
this does allow a developer to use snake case field names with snake case method names.
10+
adapters, plus for default conversion of names in include paths. Although we recommend following
11+
the PSR1 standard of using camel case for method names, this does allow a developer to use snake
12+
case field names with snake case method names.
1213

1314
## [1.0.1] - 2019-03-12
1415

src/Eloquent/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ private function guessRelation()
622622
{
623623
list($one, $two, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
624624

625-
return $caller['function'];
625+
return $this->methodForRelation($caller['function']);
626626
}
627627

628628
}

src/Eloquent/Concerns/IncludesModels.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ trait IncludesModels
7979
*/
8080
protected $includePaths = [];
8181

82+
/**
83+
* Whether Eloquent relations are camel cased.
84+
*
85+
* @var bool
86+
*/
87+
protected $camelCaseRelations = true;
88+
8289
/**
8390
* Add eager loading to the query.
8491
*
@@ -145,7 +152,38 @@ protected function convertIncludePath($path)
145152
}
146153

147154
return collect(explode('.', $path))->map(function ($segment) {
148-
return Str::camelize($segment);
155+
return $this->modelRelationForField($segment);
149156
})->implode('.');
150157
}
158+
159+
/**
160+
* Convert a JSON API field name to an Eloquent model relation name.
161+
*
162+
* According to the PSR1 spec, method names on classes MUST be camel case.
163+
* However, there seem to be some Laravel developers who snake case
164+
* relationship methods on their models, so that the method name matches
165+
* the snake case format of attributes (column values).
166+
*
167+
* The `$camelCaseRelations` property controls the behaviour of this
168+
* conversion:
169+
*
170+
* - If `true`, a field name of `user-history` or `user_history` will
171+
* expect the Eloquent model relation method to be `userHistory`.
172+
* - If `false`, the field name will be expected to be identical to
173+
* the Eloquent method, i.e. `user_history` in both JSON API and on the
174+
* model. (For this scenario, `user-history` will not work as a field
175+
* name.)
176+
*
177+
* If the developer has different conversion logic, they should overload
178+
* this method and implement it themselves.
179+
*
180+
* @param string $field
181+
* the JSON API field name.
182+
* @return string
183+
* the expected relation name on the Eloquent model.
184+
*/
185+
protected function modelRelationForField($field)
186+
{
187+
return $this->camelCaseRelations ? Str::camelize($field) : $field;
188+
}
151189
}

0 commit comments

Comments
 (0)
0