Skip to content

Commit 606adf2

Browse files
authored
Release/1.5.0 (#13)
1 parent 4c2bf3a commit 606adf2

File tree

15 files changed

+177
-26
lines changed

15 files changed

+177
-26
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/tests export-ignore
22
/vendor export-ignore
33

4-
/README.md export-ignore
54
/LICENSE export-ignore
65
/Makefile export-ignore
6+
/README.md export-ignore
77
/phpmd.xml export-ignore
88
/phpunit.xml export-ignore
99
/phpstan.neon.dist export-ignore
1010
/infection.json.dist export-ignore
1111

1212
/.github export-ignore
1313
/.gitignore export-ignore
14+
/.gitattributes export-ignore

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,30 @@ combining elements.
259259

260260
These methods allow the Collection's elements to be transformed or converted into different formats.
261261

262-
#### Mapping elements
262+
#### Applying actions without modifying elements
263263

264-
- `map`: Applies transformations to each element in the Collection and returns a new collection with the transformed
265-
elements.
264+
- `each`: Executes actions on each element in the Collection without modification.
265+
The method is helpful for performing side effects, such as logging or adding elements to another collection.
266266

267267
```php
268-
$collection->map(transformations: fn(int $value): int => $value * 2);
268+
$collection->each(actions: fn(Invoice $invoice): void => $collectionB->add(elements: new InvoiceSummary(amount: $invoice->amount, customer: $invoice->customer)));
269269
```
270+
271+
#### Grouping elements
270272

271-
#### Applying actions without modifying elements
273+
- `groupBy`: Groups the elements in the Collection based on the provided grouping criterion.
272274

273-
- `each`: Executes actions on each element in the Collection without modification.
274-
The method is helpful for performing side effects, such as logging or adding elements to another collection.
275+
```php
276+
$collection->groupBy(grouping: fn(Amount $amount): string => $amount->currency->name);
277+
```
278+
279+
#### Mapping elements
280+
281+
- `map`: Applies transformations to each element in the Collection and returns a new collection with the transformed
282+
elements.
275283

276284
```php
277-
$collection->each(actions: fn(Invoice $invoice): void => $collectionB->add(elements: new InvoiceSummary(amount: $invoice->amount, customer: $invoice->customer)));
285+
$collection->map(transformations: fn(int $value): int => $value * 2);
278286
```
279287

280288
#### Convert to array

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"array",
1313
"yield",
1414
"iterator",
15+
"iterators",
1516
"generator",
1617
"collection",
1718
"tiny-blocks"

phpstan.neon.dist

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ parameters:
66
ignoreErrors:
77
- '#Unsafe usage of new static#'
88
- '#does not specify its types#'
9-
- '#contains incompatible type#'
10-
- '#specified in iterable type iterable#'
11-
- '#is not subtype of native type static#'
12-
- '#PHPDoc tag @extends has invalid value#'
13-
- '#type has no value type specified in iterable type array#'
9+
- '#type specified in iterable type#'
1410
reportUnmatchedIgnoredErrors: false

src/Collectible.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ interface Collectible extends Countable, IteratorAggregate
2727
* @param iterable<Element> $elements The elements to initialize the collection with.
2828
* @return Collectible<Element> A new Collectible instance.
2929
*/
30-
public static function createFrom(iterable $elements): static;
30+
public static function createFrom(iterable $elements): Collectible;
3131

3232
/**
3333
* Creates an empty Collectible instance.
3434
*
3535
* @return Collectible<Element> An empty Collectible instance.
3636
*/
37-
public static function createFromEmpty(): static;
37+
public static function createFromEmpty(): Collectible;
3838

3939
/**
4040
* Adds one or more elements to the collection.
@@ -116,6 +116,15 @@ public function getBy(int $index, mixed $defaultValueIfNotFound = null): mixed;
116116
*/
117117
public function getIterator(): Traversable;
118118

119+
/**
120+
* Groups the elements in the collection based on the provided criteria.
121+
*
122+
* @param Closure(Element): Key $grouping The function to define the group key for each element.
123+
* @return Collectible<Key, Collectible<Key, Element, Element>, Element> A collection of collections,
124+
* grouped by the key returned by the closure.
125+
*/
126+
public function groupBy(Closure $grouping): Collectible;
127+
119128
/**
120129
* Determines if the collection is empty.
121130
*

src/Collection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use TinyBlocks\Collection\Internal\Operations\Retrieve\Get;
1818
use TinyBlocks\Collection\Internal\Operations\Retrieve\Last;
1919
use TinyBlocks\Collection\Internal\Operations\Transform\Each;
20+
use TinyBlocks\Collection\Internal\Operations\Transform\GroupBy;
2021
use TinyBlocks\Collection\Internal\Operations\Transform\Map;
2122
use TinyBlocks\Collection\Internal\Operations\Transform\MapToArray;
2223
use TinyBlocks\Collection\Internal\Operations\Transform\MapToJson;
@@ -31,9 +32,6 @@
3132
* Represents a collection that provides a set of utility methods for operations like adding,
3233
* filtering, mapping, and transforming elements. Internally uses iterators to apply operations
3334
* lazily and efficiently.
34-
*
35-
* @template Element of mixed
36-
* @implements Collectible<Element>
3735
*/
3836
class Collection implements Collectible
3937
{
@@ -108,6 +106,11 @@ public function getIterator(): Traversable
108106
yield from $this->iterator->getIterator();
109107
}
110108

109+
public function groupBy(Closure $grouping): Collectible
110+
{
111+
return new static(iterator: $this->iterator->add(operation: GroupBy::from(grouping: $grouping)));
112+
}
113+
111114
public function isEmpty(): bool
112115
{
113116
return !$this->iterator->getIterator()->valid();

src/Internal/Iterators/InternalIterator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
final class InternalIterator implements IteratorAggregate
2020
{
21+
/**
22+
* @var LazyOperation[]
23+
*/
2124
private array $operations;
2225

2326
/**

src/Internal/Operations/Filter/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Generator;
99
use TinyBlocks\Collection\Internal\Operations\LazyOperation;
1010

11-
final class Filter implements LazyOperation
11+
final readonly class Filter implements LazyOperation
1212
{
1313
private array $predicates;
1414

src/Internal/Operations/ImmediateOperation.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
/**
88
* Interface for operations that are applied immediately and not lazily.
9-
*
10-
* @extends Operation
119
*/
1210
interface ImmediateOperation extends Operation
1311
{

src/Internal/Operations/LazyOperation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*
1212
* @template Key of array-key
1313
* @template Value
14-
* @extends Operation
1514
*/
1615
interface LazyOperation extends Operation
1716
{

0 commit comments

Comments
 (0)