Skip to content

Commit

Permalink
The forEach() method
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Jul 28, 2024
1 parent 933af12 commit f7ae52e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
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.7 - 2024-07-28
- Implementing the `forEach()` method

## 0.3.6 - 2024-07-11
- Implementing the `groupBy()` method

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,32 @@ foreach ($table->iterateBlock(false) as $key => $item) {
}
```

### Using forEach() method
The `Block` class implements the `forEach()`method.
> If you need to walk through the `Block` object, you can use the `forEach()` method.
You can specify the function as an argument of the `forEach()` method to manage each single element.

```php
$url = "https://dummyjson.com/posts";
$posts = Block::fromJsonUrl($url) // Load the Block from the remote URL
->getBlock("posts") // get the `posts` as Block object
->where(
field:"tags",
operator: "in",
value: "love",
preseveKeys: false,
) // filter the posts, selecting only the posts with tags "love"
->forEach(fn($element): array => [
"title" => strtoupper((string) $element->get("title")),
"tags" => count($element->get("tags")),
]);
// The `$posts` object is an instance of the `Block` class.
// The `$posts` object contains the items that matches the `where` method.
// You can access to the elements via the nested keys
// $posts->get("0.title"); // "HOPES AND DREAMS WERE DASHED THAT DAY."
// $posts->get("0.tags"); // 3
```

## Validating Data 🆕

You can validate the data in the Block object with JSON schema.
Expand Down
12 changes: 5 additions & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
php81: true,
)
->withPreparedSets(
//deadCode: true,
//codeQuality: true,
deadCode: true,
codeQuality: true,
earlyReturn: true,
// typeDeclarations: true,
typeDeclarations: true,
privatization: true,
)
->withTypeCoverageLevel(10)
->withCodeQualityLevel(10)
->withDeadCodeLevel(10);
// naming: true
);
13 changes: 13 additions & 0 deletions src/Traits/IteratableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ public function offsetUnset(mixed $offset): void
{
unset($this->data[$offset]);
}

/**
* It executes a provided function ($callback) once for each element.
* @param callable $callback the function to call for each element
*/
public function forEach(callable $callback): self
{
$result = [];
foreach ($this as $key => $item) {
$result[$key] = $callback($item);
}
return self::make($result);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@


})->group("url");

it('remote foreach', function (): void {

$url = "https://dummyjson.com/posts";
$posts = Block::fromJsonUrl($url)
->getBlock("posts")
->where(
field:"tags",
operator: "in",
value: "love",
preseveKeys: false,
)
->forEach(fn($element): array => [
"title" => strtoupper((string) $element->get("title")),
"tags" => count($element->get("tags")),
]);
expect($posts)->toBeInstanceOf(Block::class);
expect($posts)->toHaveCount(9);
expect($posts->get("0.title"))->toBe("HOPES AND DREAMS WERE DASHED THAT DAY.");
expect($posts->get("0.tags"))->toBe(3);

})->group("url");

0 comments on commit f7ae52e

Please sign in to comment.