New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exclude keys in toArray function #11
Conversation
I'm just thinking right now, that maybe we have to add another function like |
I've added another commit with a toArrayWithout function |
Sorry for the number of commits, but I was thinking: There is now an blacklist but not a whitelist. So I've added a whitelist and renamed the functions to toArrayOnly and toArrayExcept |
I like the idea. Let me ping some people to collect some more feedback: |
I don't really like adding complexity and functionality that can be provided otherwise. In this case the ValueObject includes functionality that allows it to modify it's returned contents according to external requirements. Shouldn't the ValueObject just return it's own data without regards as to what someone else might want to do with it? Especially as the two functions are really small I would rather see them in an ArrayModifier class that combines those two methods to static function calls. So instead of the current invocation $newObject = ValueObject::fromArray($state->toArrayOnly('value', 'version')); you'd get this: $newObject = ValueObject::fromArray(ArrayModifier::useOnly($state->toArray(), 'value', 'version')); Of course one could also implement something like this: $useOnlyValueAndVersion = new ArrayModifierUseOnly('value', 'version');
$newObject = ValueObject::fromArray($useOnlyVaslueAndVersion->fromArray($state->toArray()); Yes, it's a bit more verbose but perhaps also a bit more explicit. And the objects do not need to handle stuff that are not their concern... |
good point @heiglandreas |
A different approach that would perhaps be even more explicit could be a ValueObjectFactory like this: $myObject = MyObjectFactory::createFromOtherState($state); No more fooling around with Array-keys and such on the Object level. Yes: The |
Regarding testing: as long as an ImmutableRecord is used as source and target object, you don't need excessive testing. ImmutableRecord is super strict on the internal structure. Arrays are only used as transport but as soon as they are passed to the VO, everything is checked: only known properties, missing properties, defautls, null values, correct types. It is really really hard to do something wrong. But the object factory is definitely a nice way to hide the array stuff. A general purpose |
I would still test whether everything works as expected to immediately see when one of the objects was adapted without adapting the Factory. Because that will break the factory and then cause an Exception that we probably might not want in production... ;-) |
of course automated tests should touch the factory logic. You want those exceptions become visible as early as possible, not in production when it is too late. |
Ok, I'm fine with the idea of an array modifier. But are you ok to add the 2 modifiiers in this package? |
I like the idea, because it simplifies a common task when working with ImmutableRecord. On the other hand it adds complexity to the interface and maybe people start to request more and more helper methods, so we would need to explain them why we merged yours, but won't merge other stuff. So I tend to agree with @heiglandreas that |
Oh and BTW: you can always extend |
I am not sure what to think of that either. I agree with @heiglandreas that this kind of functionality is a little to specific to be in the ImmutableRecordLogic and adding this would justify adding a bunch of other functionality too. I do however think that it would be nice for the library to provide this functionality to the user so that not everyone has to manually implement it. Therefore providing additional traits or things like the ArrayModifier or even something like Decorators might be something everyone can live with? |
Providing "add-ons" is actually a good idea! I made a gist to experiment with the idea. This way could provide some helpers and also show people how they can add more helpers themself and/or add more of these "add-ons" in the future, always as a opt-in option and not the other way round. What do you think? |
Thats pretty much what I had in mind. Could be somewhat tricky to impmement though specifically what functionality should go where and the naming of these Add-Ons. |
@heiglandreas has made some good arguments. Maybe we can simply define plain PHP functions like |
@sandrokeil good point. I'm fine with functions as well. What do we do regarding PHP not supporing function autoloading? Schould we add the files to composer.json so that they are always loaded? |
Or a new package like |
Is there an advantage with using functions over the ArrayModifier or Trait that I am not seeing? Also I personally feel like adding another package just for this is a little overkill, but that is probably a personal preference. |
Good idea. Mezzio uses the term I think in this case, a plain PHP function would be better, because you don't need a trait or interface here. It's simpler. Yes, we have to add the functions file to the composer.json autoloader (example). But this solution does no support deep nesting. The Another idea is to add a plugin system for the serialization / toArray stuff. So you can hook into it and transform the fields like you want. But could be complicated for the deep nesting stuff. |
Oh @sandrokeil good that you mention the deep nesting stuff. You're right, more complex addons require an interface to support transformation of nested ImmutableRecords. We should take that into account even if it is not required for the two simple helper functions discussed here. I think we can agree that we want to add the addon, so that people can use it.
If someone wants to tackle this, I'd be more than happy.
This feels a way to complicated for So my final vote is for Interface + Trait. |
Ok, I‘m also for a new repo with interfaces / traits and two issues:
But we should consider to improve the code first, if it is possible / needed and have support for deep nesting. So we need for the exclude functionality dot notation support ( |
Hi, I would like to add this feature to exclude some keys in the toArray function.
I've already had a few situations where I wanted to convert an ImmutableRecord to an array and always had to unset some keys to use the result as a starting array for another ImmutableRecord.
Are you guys ok to add this feature?