From d41bcb87f65788400e05237600391ba3644149b4 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 8 Aug 2024 22:01:00 +0200 Subject: [PATCH] The exists() method --- CHANGELOG.md | 3 +++ README.md | 15 +++++++++++++++ src/Traits/QueryableBlock.php | 9 ++++++++- tests/Feature/QueryBlockTest.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d0badf..1b91729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.3.9 - 2024-08-08 +- Implementing the `exists()` method + ## 0.3.8 - 2024-08-03 - Implementing the `applyField()` method diff --git a/README.md b/README.md index e537b75..0cb5c62 100644 --- a/README.md +++ b/README.md @@ -538,6 +538,21 @@ $grouped->dumpJson(); */ ``` +### The `exists()` method + +You can use the `exists()` method to check if an element that meets a certain condition exists. This method is a convenient way to determine if any records match your query without needing to count them explicitly. + +Here’s how you can use it: + +```php +$has = $composerContent + ->getBlock("story.content.body")->where( + "component", + "banner", + )->exists(); +``` + +This will return true if a banner component exists, and false if it does not. ## Looping Data The Block class implements the Iterator interface. diff --git a/src/Traits/QueryableBlock.php b/src/Traits/QueryableBlock.php index cb7a14e..155f79a 100644 --- a/src/Traits/QueryableBlock.php +++ b/src/Traits/QueryableBlock.php @@ -16,20 +16,27 @@ public static function like(mixed $value1, mixed $value2): bool return str_contains($strValue1, $strValue2); } + public function exists(): bool + { + return $this->count() > 0; + } + public function where( string|int $field, mixed $operator = null, mixed $value = null, bool $preseveKeys = true, ): self { + if (func_num_args() === 1) { $value = true; $operator = '=='; } if (func_num_args() === 2) { $value = $operator; - $operator = '==='; + $operator = '=='; } + $returnData = []; foreach ($this as $key => $element) { diff --git a/tests/Feature/QueryBlockTest.php b/tests/Feature/QueryBlockTest.php index 632687e..2211660 100644 --- a/tests/Feature/QueryBlockTest.php +++ b/tests/Feature/QueryBlockTest.php @@ -113,3 +113,31 @@ expect($posts->get("0.id"))->toBe(1); expect($posts->get("0.reactions.likes"))->toBe(192); }); + +test('Query Block with has', function (): void { + $jsonString = file_get_contents("./tests/data/story.json"); + $composerContent = Block::fromJsonString($jsonString); + $has = $composerContent->getBlock("story.content.body")->where( + "component", + "==", + "banner", + )->exists(); + expect($has)->toBeTrue(); + $has = $composerContent->getBlock("story.content.body")->where( + "component", + "!=", + "banner", + )->exists(); + expect($has)->toBeTrue(); + $has = $composerContent->getBlock("story.content.body")->where( + "component", + "==", + "bannerXXX", + )->exists(); + expect($has)->toBeFalse(); + $has = $composerContent->getBlock("story.content.body")->where( + "component", + "banner", + )->exists(); + expect($has)->toBeTrue(); +});