Skip to content

Commit

Permalink
Update "in" operator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wildwalks authored Sep 27, 2024
1 parent 7bcdb06 commit 7d81e68
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Traits/QueryableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function where(
'!=' => $elementToCheck->get($field) != $value,
'!==' => $elementToCheck->get($field) !== $value,
'like' => self::like($elementToCheck->get($field), $value),
'in' => in_array($value, $elementToCheck->get($field)),
'in' => (is_array($elementToCheck->get($field)) && is_string($value)
? in_array($value, $elementToCheck->get($field))
: in_array($elementToCheck->get($field), $value)
),
default => $elementToCheck->get($field) === $value,
};
if ($found) {
Expand Down

0 comments on commit 7d81e68

Please sign in to comment.