EventSaucePHP / ObjectHydrator

Object Hydration library to create Command and Query objects.
MIT License
318 stars 24 forks source link

Make serializing public methods optional #31

Closed Nadyita closed 1 year ago

Nadyita commented 1 year ago

There is currently no way to prevent the DefinitionProvider() from always returning all public methods. I have a lot of objects that calculate values out of other properties and I don't want these to be exported, because they are just for making handling easier (like asking an object for its height and width, but creating it by giving a set of dots in a coordination system). Is there any way to achieve this? I can't seem to solve this elegantly by providing my own DefinitionProvider, because it's a final class and not an interface I can code against. The only halfway elegant solution seems creating a derived class of ObjectMapperUsingReflection and overriding serializeObject(). That seems way over the top.

I'd love to be able to turn off this behavior, preferably by an attribute like (#[SerializeMethods(true/false)]) or something similar on the object being serialized. Just a suggestion. I might be missing something important, but right now, I feel a bit clueless.

frankdejonge commented 1 year ago

How about this: https://github.com/EventSaucePHP/ObjectHydrator/pull/32/files#diff-95db255a9adc74f86396d39aec2ba450bb1f2e35dbcdbff518fc3c61441f85a7R8

Nadyita commented 1 year ago

@frankdejonge Works perfect for me, thanks! Didn't expect a reply that fast, Awesome!

frankdejonge commented 1 year ago

Released as 1.1.0, documented here: https://github.com/EventSaucePHP/ObjectHydrator#omitting-data-for-serialization