Comments (8)
Hi @holtkamp ,
Interesting idea! However, it seems to be a bit fragile for me :( What happens with interfaces? For example when the property is a DateTimeInterface
? Or when the instantiation of the object is not trivial, e.g. when it contains more constructor parameters? We can't even be sure that the constructor is available because it might not be public
.
That's why I prefer hydrating Data Transfer Objects - which are simple data bags. I usually map them to Value Objects if some kind of validation is needed, or directly use them for presentation.
If your preferred workflow is different then the best thing in my opinion to do is override the ClassDocumentHydrator::hydrateResourceAttributes()
method - but I think this is what you did anyway. :) It might make sense to create a custom hydrator for each resource if the mapping is not trivial.
To be honest I was always sceptical about general solutions for mapping/hydration which are too advanced because they tend to be fragile (in the sense that people will always keep finding features that it doesn't have yet), and I think it's much easier and less complex to do the mapping/hydration manually - for the price of some repetitive work.
from yang.
Yep, I like it :) Thanks!
from yang.
@holtkamp Release is done :) https://github.com/woohoolabs/yang/releases/tag/2.3.0
from yang.
Nice observations!
If your preferred workflow is different then the best thing in my opinion to do is override the ClassDocumentHydrator::hydrateResourceAttributes() method - but I think this is what you did anyway. :)
Indeed, and while doing this, I thought: this can be isolated more by using interfaces: so my "only suggestion" was actually this:
Therefore, next to the DocumentHydratorInterface, a DocumentPropertyHydrator or DocumentAttributeHydrator interface would be nice to have
The example with typed properties I gave was an illustration of "why" this would/could be useful.
So "by default" nothing will change, but one can choose to implement a custom DocumentHydrator, which uses a custom DocumentPropertyHydrator/DocumentAttributeHydrator
Code would probably explain the suggestion better, I can try to come up with a PR and then we can discuss the added value there 😄
from yang.
OK! I'm looking forward for the PR :)
from yang.
@kocsismate as you can see the change is small, but opens up the possibility for somebody to write his own AttributeHydrator without the need to use inheritance to specialize ClassDocumentHydrator::hydrateResourceAttributes()
from yang.
Thanks for merging, does this justify a new minor release? Then this issue can be closed.
from yang.
Sure! I'll release it this week :)
from yang.
Related Issues (17)
- php-http plugin AddHostPlugin does not work with JsonApiRequestBuilder HOT 4
- Add HydratorInterface::hydrateDocument(): stdClass HOT 11
- create/update/delete requests HOT 1
- Allow access to ResourceObject.id HOT 2
- Empty PSR-7 request object not working anymore HOT 1
- hasDocument and hasErrors shouldn't throw exceptions HOT 5
- How to share common schemas/resources/etc between yin and yang HOT 2
- Best practice to hydrate to a specific class? HOT 8
- "make test" fails for fresh clone + composer install HOT 3
- ClassDocumentHydrator::hydrateSingleResource() enforces use of stdClass HOT 5
- Unify Schema/ResourceObject and Request/ResourceObject to improve developer experience HOT 4
- Add ability to set more complex filter query parameter
- Adopt PSR-18 HOT 7
- Missing type hinting HOT 2
- Unexpected Behaviour When To-One Relationship Data Is NULL HOT 7
- Resources with an ID of "0" disappear when using ClassHydrator HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yang.