8000 [Feature] Allow non-camel case adapter relation method names. · CodingSeo/laravel-json-api@59ce758 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59ce758

Browse files
committed
[Feature] Allow non-camel case adapter relation method names.
Although developers should follow the PSR1 standard of camel casing method names, there are some developers who follow the Eloquent attribute standard of snake casing attributes when defining relationships. This commit allows an adapter to have its relation method name in exactly the same format as the JSON API field name. I.e. a field name of `user_history` and a relation method called `user_history`. Otherwise it falls back to the PSR1 standard and expects the method name to be the camel case version of the field name. See cloudcreativity#315 for discussion.
1 parent 19893c4 commit 59ce758

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
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+
- [#315](https://github.com/cloudcreativity/laravel-json-api/issues/315)
9+
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.
12+
513
## [1.0.1] - 2019-03-12
614

715
### Fixed

src/Adapter/AbstractResourceAdapter.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,32 @@ protected function isFillableRelation($field, $record)
197197
}
198198

199199
/**
200-
* @param $field
200+
* Get the method name on this adapter for the supplied JSON API field.
201+
*
202+
* By default we expect the developer to be following the PSR1 standard,
203+
* so the method name on the adapter should use camel case.
204+
*
205+
* However, some developers may prefer to use the actual JSON API field
206+
* name. E.g. they could use `user_history` as the JSON API field name
207+
* and the method name.
208+
*
209+
* Therefore we return the field name if it exactly exists on the adapter,
210+
* otherwise we camelize it.
211+
*
212+
* A developer can use completely different logic by overloading this
213+
* method.
214+
*
215+
* @param string $field
216+
* the JSON API field name.
201217
* @return string|null
218+
* the adapter's method name, or null if none is implemented.
202219
*/
203220
protected function methodForRelation($field)
204221
{
222+
if (method_exists($this, $field)) {
223+
return $field;
224+
}
225+
205226
$method = Str::camelize($field);
206227

207228
return method_exists($this, $method) ? $method : null;

0 commit comments

Comments
 (0)
0