-
Notifications
You must be signed in to change notification settings - Fork 2
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
Update "in" operator #18
Conversation
Currently the 'in' operator tests if a value is in an array field this update still allows that, but also allows people to test if a string field is in an array of their choosing. eg $url = "https://dummyjson.com/posts"; $posts = Block ::fromJsonUrl($url) ->getBlock("posts"); //if field id is in this array $someOddPosts = $posts->where("id", "in", array(1,3,5,7,9)); // if the string "love" in the tags array field $lovePosts = $posts->where("tags", "in", "love"); (I can update this further, eg testing if a string, should maybe also allow numeric,bool?? or just not an array?? If two arrays, perhaps could get intersection) -- but that was feeling complex
Hi @wildwalks , thank you for checking the library. |
thanks @roberto-butti, I really appreciate your comments.
These next few I am not sure about, might not be right but some early ideas. You know the code, if these are separate, maybe we just leave these points below to another time. Scope creep warning :)
so specifically for "has", I guess that can work if we have a clear left side and right side, but I guess that is why I mentioned the expression options idea, does this can confuse this terminology later?? in SQL there is an any() function that is similar as well. |
Thank you @wildwalks for sharing your thoughts. In the Then, I agree to make it more flexible, so this is the reason I'm thinking of introducing a Filter class that can be instantiated and filled by the user for more complex cases (also and/or) and custom functions. And send to the At the moment, I think we can add "has" as a "basic" operator. |
Great -- good plan :) Shall I update this branch to work something like this?
|
i think to keep it easy, an array in the data has to check if has a element (scalar) ->where("field" "has" , "value") |
I am sorry, I am a bit confused. That sounds like the same behaviour as the current implementation of 'in', where we check if an array in the data source contains a scaler? I was proposing we test if a scaler in the data is in an array provided in the where(). (is in the provided array has (contains) the scaler in the data) I was picturing a change like the following, the opposite too in. But that seems different from your wording and feels a backwards? Your project, your call. Sorry, I did not mean to make this complicated. |
The project is open source, so all suggestions are welcome.
Adding some tests helps to understand the scenario and the problem solved with the new operator.
I think with one PR, we can cover this scenario. If you have in mind an additional scenario, we can open another PR |
Hi @wildwalks , I understand where we were having a misunderstanding. The current implementation of the "in" operator is a bit wrong. Using your initial example, on the first message of this thread, the expected behavior i would like to achieve is (adjusting the current behavior of // the id field that is a number (scalar) is checked if it is included in an array
$someOddPosts = $posts->where("id", "in", array(1,3,5,7,9));
// if the string "love" in the `tags` array field
$lovePosts = $posts->where("tags", "has", "love"); |
I wrote some tests that explain what i have in mind: test(
'where method, in operator',
function () use ($fruitsArray): void {
$data = Block::make($fruitsArray);
$greenOrBlack = $data->where("color", "in", ["green", "black"]);
expect($greenOrBlack)->tohaveCount(1);
$noResult = $data->where("color", "in", []);
expect($noResult)->tohaveCount(0);
$greenOrRed = $data->where("color", "in", ["green", "red"]);
expect($greenOrRed)->tohaveCount(3);
},
);
test(
'where method, has operator',
function () use ($fruitsArray): void {
$data = Block::make($fruitsArray);
$sweet = $data->where("tags", "has", "sweet");
expect($sweet)->tohaveCount(2);
$noResult = $data->where("tags", "has", "not-existent");
expect($noResult)->tohaveCount(0);
$softFruit = $data->where("tags", "has", "soft");
expect($softFruit)->tohaveCount(1);
},
); Where the "color" field is a string, and the "tags" field is an array of strings |
Hi @wildwalks, I would like to apply changes to the "in" and "has" operators. Do you have any feedback about the latest message I shared? I think having your opinion is great here. |
Hi @roberto-butti |
yeah, let's start with in and has. then we will decide on the next feat request/pr which is the best operator for"not", also for other operators. |
There's no pressure at all, @wildwalks. I'm asking to know your plans because I want to create a new release. |
Hi. Sorry. Just got back today. Bit behind on emails. I managed to get offline for a while :) No worries I can do some tomorrow. But I am a bit lost in this thread sorry. Shall I update this PR with the new "in and Has"? |
if you want you can apply the changes you can pick from the other branch in this one, so we can close this one |
Change logic for 'in' to be more intuitive and added 'has' (similar to how 'in' was)
Sorry this took me a while. Fingers crossed :) |
Thank you @wildwalks i'm going to merge it |
Thanks Roberto-- love the project. I made this tweak to my code, think it might help others.
Currently the 'in' operator tests if a value is in an array field. This update still allows that, but also
allows people to test if a string field is in an array of their choosing.
(it allows users to effectively do a simple 'or' test)
eg
$url = "https://dummyjson.com/posts";
$posts = Block
::fromJsonUrl($url)
->getBlock("posts");
//if field id is in this array
$someOddPosts = $posts->where("id", "in", array(1,3,5,7,9));
// if the string "love" in the tags array field
$lovePosts = $posts->where("tags", "in", "love");
(I can update this further, eg testing if a string, should maybe also allow numeric,bool?? or just not an array?? If two arrays, perhaps could get intersection) -- but that was feeling complex