Best practice for bidirectional external API mapping? #462
Unanswered
tobias-grasse
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My application needs to synchronize models (= read & write) with a third-party JSON REST API. It looks like
laravel-data
is a great fit for that purpose, but I wonder what the best practices for this use case are.To explain by example: I need to synchronize my
Client
Eloquent model with aClientContact
resource on the third-party API.Some properties need just a name mapping:
firstName
(API) <->given_name
(Eloquent). Others are more complex:id
(API) <->external_id
attribute on theprofile
relationship of myClient
model.Example API response:
ERD of my Eloquent models:
My
ClientData
object – properties currently aligned with the Eloquent model:My ideal solution would be something along these lines:
I experimented with the
Map{Input,Output}Name
attributes and thefrom<Type>
magic methods, but sooner or later I find myself repeating the mapping in both directions. Maybe I'm holding it wrong™?If I understand the package code correctly, the
JsonNormalizer
will decode the API response string to an associative array, which is passed to theClientContactData
constructor. Do I need to override bothfromArray
andfromModel
to map both ways?Beta Was this translation helpful? Give feedback.
All reactions