[Feature Request]: Improve array helper with others methods
Description
Hi 👋 I've read other issues and PR and the code related to the Array Helper component. I think the purpose of it is not to be a clone of Laravel collection but a much simpler wrapper around php's native functions. ( tell me if i'm wrong ) I guess there's some methods missing like a pluck or reduce or other ones. If you are ok with this I can write some implementations. But is it possible to list some of methods you want and don't want ?
Here's the updated TODO from the original PR :
- [x] put()
- [x] combine()
- [x] keys()
- [x] dump()
- [x] diff()
- [x] diffKeys()
- [x] merge()
- [x] intersect()
- [x] intersectKeys()
- [x] unique()
- [x] flip()
- [x] pad()
- [x] add()
- [x] push()
- [ ] reduce()
- [x] pluck()
- [ ] chunk() ?
- [x] random()
- [ ] pull() ?
- [ ] search() ?
- [ ] slice()
- [ ] sort() => and probably its variations
- [ ] flatten() => I must check if unwrap isn't doing the same before
- [ ] wrap() ?
- [ ] remove()
- [ ] forget() => alias of remove()
- [ ] shuffle
Benefits
This will improve the array helper component and make it more useful
Yes, feel free to send a (tested) PR with new methods! I'm gonna close this issue as well, but go ahead!
I often have the use case to remove a value from an array. For such cases, I use filter or reject.
What do you think about adding the following method?
public function without(mixed $item, bool $strict = true): self;
I don't know.
Actually, this helper don't aim to be fully-featured as the Laravel collections or others libs.
Its main goal is to have a clean API to wrap native PHP array functions and others methods that's missing in PHP.
That said, to have a good DX I'm ok to add reject method as an alias of filter to avoid frustration for developers who's used to this syntax.
But, if the without method behavior can be replicated easily using filter I don't really see the added value of such an additional method.
As I understand this, it allow you to do something like :
$array = arr([1, 2, 3])->without(2, true); // [1, 3]
So It can be achieve using filter like this :
$array = arr([1, 2, 3])->filter(fn (int $value) => $value !== 2); // [1, 3]
This is not really verbose and a lot of developers are used to this syntax.
I wonder if a without method which does less things may confuse users.
Any thought ? @aidan-casey
Just a perspective from an outsider looking in..
I originally leaned towards wanting to voice my opinion for including the without method as there are times that I reach for it in my laravel applications. However, in considering where tempest is and what I feel its achieving (again, outsider/user looking in), I think I've flipped my vote to leaving it out, at least for now.
The difference in behavior for associate vs traditional list array would require extra documentation, testing, etc as without() in laravel will exclude associative arrays by key and lists arrays by value.
In playing with tempest, I feel more verbose at times, but that's not necessarily a bad thing, rather I feel I'm being more direct and explicit in my code. Aliasing reject to filter seems like it would provide better DX for devs coming from laravel, but I feel it loses the directness. On the other hand, I'm not sure having a more direct versions like rejectKeys or rejectValues would have warrant enough value to save me from doing
$array = arr([1, 2, 3])->filter(fn (int $value) => $value !== 2);
or
$array = arr(['foo' => 1, 'bar' => 'baz'])->filter(fn ($value, $key) => $key !== 'bar');
I like the slimness that tempest provides and its pretty easy to know which function will get the job done without having four possible methods that could perform the task based on their name
@gturpin-dev FYI https://github.com/tempestphp/tempest-framework/issues/631 has been closed so you can update your TODO list 😉
@gturpin-dev FYI #631 has been closed so you can update your TODO list 😉
Perfect, Thanks 🙌
Just notice that @innocenzi wrote slice and wrap methods in #754 so as those was the latest methods in the TODO, I close this ! 🎉