Skip to content
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

The exists() method #17

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/Traits/QueryableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/QueryBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});