tempestphp / tempest-framework

The PHP framework that gets out of your way 🌊
https://tempestphp.com
MIT License
873 stars 62 forks source link

[Feature Request]: Improve array helper with others methods #543

Open gturpin-dev opened 2 weeks ago

gturpin-dev commented 2 weeks ago

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 :

Benefits

This will improve the array helper component and make it more useful

brendt commented 1 week ago

Yes, feel free to send a (tested) PR with new methods! I'm gonna close this issue as well, but go ahead!

shaffe-fr commented 3 days ago

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;
gturpin-dev commented 3 days ago

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

ainesophaur commented 3 days ago

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