Skip to content

Commit e593ea5

Browse files
committed
Merge remote-tracking branch 'origin/group-aggregators' into group-aggregators
2 parents 7008d03 + c6b59ae commit e593ea5

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed

README.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,8 @@ $items = [
24892489
$result = CollectionFactory::from($items)
24902490
->stream()
24912491
->collect(Group::by('type'))
2492-
; // ->
2492+
;
2493+
/* Result ->
24932494
[
24942495
'groceries' => new \WS\Utils\Collections\ArrayList([
24952496
['name' => 'potato', 'type' => 'groceries', 'price' => 25],
@@ -2504,7 +2505,7 @@ $result = CollectionFactory::from($items)
25042505
['name' => 'cinema', 'type' => 'entertainment', 'price' => 20],
25052506
]),
25062507
];
2507-
2508+
*/
25082509
```
25092510

25102511
#### addToSet
@@ -2531,13 +2532,14 @@ $aggregation = Group::by('type')->addToSet('name', 'list');
25312532
$result = CollectionFactory::from($items)
25322533
->stream()
25332534
->collect($aggregation)
2534-
; // ->
2535+
;
2536+
/* Result ->
25352537
[
25362538
'groceries' => ['list' => ['potato', 'milk']],
25372539
'transport' => ['list' => ['taxi']],
25382540
'entertainment' => ['list' => ['cinema']],
25392541
];
2540-
2542+
*/
25412543
```
25422544

25432545
#### avg
@@ -2564,13 +2566,14 @@ $aggregation = Group::by('type')->avg('price', 'avg');
25642566
$result = CollectionFactory::from($items)
25652567
->stream()
25662568
->collect($aggregation)
2567-
; // ->
2569+
;
2570+
/* Result ->
25682571
[
25692572
'groceries' => ['avg' => 40],
25702573
'transport' => ['avg' => 75],
25712574
'entertainment' => ['avg' => 30],
25722575
];
2573-
2576+
*/
25742577
```
25752578

25762579
#### count
@@ -2597,13 +2600,14 @@ $aggregation = Group::by('type')->count('total');
25972600
$result = CollectionFactory::from($items)
25982601
->stream()
25992602
->collect($aggregation)
2600-
; // ->
2603+
;
2604+
/* Result ->
26012605
[
26022606
'groceries' => ['total' => 3],
26032607
'transport' => ['total' => 1],
26042608
'entertainment' => ['total' => 2],
26052609
];
2606-
2610+
*/
26072611
```
26082612

26092613
#### first
@@ -2630,13 +2634,14 @@ $aggregation = Group::by('type')->first('name', 'item');
26302634
$result = CollectionFactory::from($items)
26312635
->stream()
26322636
->collect($aggregation)
2633-
; // ->
2637+
;
2638+
/* Result ->
26342639
[
26352640
'groceries' => ['item' => 'potato'],
26362641
'transport' => ['item' => 'taxi'],
26372642
'entertainment' => ['item' => 'Knicks game'],
26382643
];
2639-
2644+
*/
26402645
```
26412646

26422647
#### last
@@ -2663,13 +2668,14 @@ $aggregation = Group::by('type')->last('name', 'item');
26632668
$result = CollectionFactory::from($items)
26642669
->stream()
26652670
->collect($aggregation)
2666-
; // ->
2671+
;
2672+
/* Result ->
26672673
[
26682674
'groceries' => ['item' => 'milk'],
26692675
'transport' => ['item' => 'airplane'],
26702676
'entertainment' => ['item' => 'cinema'],
26712677
];
2672-
2678+
*/
26732679
```
26742680

26752681
#### max
@@ -2696,13 +2702,14 @@ $aggregation = Group::by('type')->max('price', 'max');
26962702
$result = CollectionFactory::from($items)
26972703
->stream()
26982704
->collect($aggregation)
2699-
; // ->
2705+
;
2706+
/* Result ->
27002707
[
27012708
'groceries' => ['max' => 50],
27022709
'transport' => ['max' => 100],
27032710
'entertainment' => ['max' => 300],
27042711
];
2705-
2712+
*/
27062713
```
27072714

27082715
#### min
@@ -2729,13 +2736,14 @@ $aggregation = Group::by('type')->min('price', 'min');
27292736
$result = CollectionFactory::from($items)
27302737
->stream()
27312738
->collect($aggregation)
2732-
; // ->
2739+
;
2740+
/* Result ->
27332741
[
27342742
'groceries' => ['min' => 30],
27352743
'transport' => ['min' => 50],
27362744
'entertainment' => ['min' => 30],
27372745
];
2738-
2746+
*/
27392747
```
27402748

27412749
#### sum
@@ -2762,13 +2770,14 @@ $aggregation = Group::by('type')->sum('price', 'spent');
27622770
$result = CollectionFactory::from($items)
27632771
->stream()
27642772
->collect($aggregation)
2765-
; // ->
2773+
;
2774+
/* Result ->
27662775
[
27672776
'groceries' => ['spent' => 80],
27682777
'transport' => ['spent' => 150],
27692778
'entertainment' => ['spent' => 330],
27702779
];
2771-
2780+
*/
27722781
```
27732782

27742783
#### addAggregator
@@ -2802,13 +2811,14 @@ $aggregation = Group::by('type')->addAggregator('names', function (Collection $c
28022811
$result = CollectionFactory::from($items)
28032812
->stream()
28042813
->collect($aggregation)
2805-
; // ->
2814+
;
2815+
/* Result ->
28062816
[
28072817
'groceries' => ['names' => 'potato, milk'],
28082818
'transport' => ['names' => 'taxi, airplane'],
28092819
'entertainment' => ['names' => 'Knicks game, cinema'],
28102820
];
2811-
2821+
*/
28122822
```
28132823

28142824
All aggregators can be combined with each other to get the desired result
@@ -2831,11 +2841,12 @@ $aggregation = Group::by('type')->avg('price', 'avg')->min('price', 'min')->max(
28312841
$result = CollectionFactory::from($items)
28322842
->stream()
28332843
->collect($aggregation)
2834-
; // ->
2844+
;
2845+
/* Result ->
28352846
[
28362847
'groceries' => ['avg' => 40, 'min' => 30, 'max' => 50],
28372848
'transport' => ['avg' => 75, 'min' => 50, 'max' => 100],
28382849
'entertainment' => ['avg' => 165, 'min' => 30, 'max' => 300],
28392850
];
2840-
2851+
*/
28412852
```

doc/README.ru.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,8 @@ $items = [
24852485
$result = CollectionFactory::from($items)
24862486
->stream()
24872487
->collect(Group::by('type'))
2488-
; // ->
2488+
;
2489+
/* Result ->
24892490
[
24902491
'groceries' => new \WS\Utils\Collections\ArrayList([
24912492
['name' => 'potato', 'type' => 'groceries', 'price' => 25],
@@ -2500,7 +2501,7 @@ $result = CollectionFactory::from($items)
25002501
['name' => 'cinema', 'type' => 'entertainment', 'price' => 20],
25012502
]),
25022503
];
2503-
2504+
*/
25042505
```
25052506

25062507
#### addToSet
@@ -2528,13 +2529,14 @@ $aggregation = Group::by('type')->addToSet('name', 'list');
25282529
$result = CollectionFactory::from($items)
25292530
->stream()
25302531
->collect($aggregation)
2531-
; // ->
2532+
;
2533+
/* Result ->
25322534
[
25332535
'groceries' => ['list' => ['potato', 'milk']],
25342536
'transport' => ['list' => ['taxi']],
25352537
'entertainment' => ['list' => ['cinema']],
25362538
];
2537-
2539+
*/
25382540
```
25392541

25402542
#### avg
@@ -2562,13 +2564,14 @@ $aggregation = Group::by('type')->avg('price', 'avg');
25622564
$result = CollectionFactory::from($items)
25632565
->stream()
25642566
->collect($aggregation)
2565-
; // ->
2567+
;
2568+
/* Result ->
25662569
[
25672570
'groceries' => ['avg' => 40],
25682571
'transport' => ['avg' => 75],
25692572
'entertainment' => ['avg' => 30],
25702573
];
2571-
2574+
*/
25722575
```
25732576

25742577
#### count
@@ -2596,13 +2599,14 @@ $aggregation = Group::by('type')->count('total');
25962599
$result = CollectionFactory::from($items)
25972600
->stream()
25982601
->collect($aggregation)
2599-
; // ->
2602+
;
2603+
/* Result ->
26002604
[
26012605
'groceries' => ['total' => 3],
26022606
'transport' => ['total' => 1],
26032607
'entertainment' => ['total' => 2],
26042608
];
2605-
2609+
*/
26062610
```
26072611

26082612
#### first
@@ -2630,13 +2634,14 @@ $aggregation = Group::by('type')->first('name', 'item');
26302634
$result = CollectionFactory::from($items)
26312635
->stream()
26322636
->collect($aggregation)
2633-
; // ->
2637+
;
2638+
/* Result ->
26342639
[
26352640
'groceries' => ['item' => 'potato'],
26362641
'transport' => ['item' => 'taxi'],
26372642
'entertainment' => ['item' => 'Knicks game'],
26382643
];
2639-
2644+
*/
26402645
```
26412646

26422647
#### last
@@ -2664,13 +2669,14 @@ $aggregation = Group::by('type')->last('name', 'item');
26642669
$result = CollectionFactory::from($items)
26652670
->stream()
26662671
->collect($aggregation)
2667-
; // ->
2672+
;
2673+
/* Result ->
26682674
[
26692675
'groceries' => ['item' => 'milk'],
26702676
'transport' => ['item' => 'airplane'],
26712677
'entertainment' => ['item' => 'cinema'],
26722678
];
2673-
2679+
*/
26742680
```
26752681

26762682
#### max
@@ -2697,13 +2703,14 @@ $aggregation = Group::by('type')->max('price', 'max');
26972703
$result = CollectionFactory::from($items)
26982704
->stream()
26992705
->collect($aggregation)
2700-
; // ->
2706+
;
2707+
/* Result ->
27012708
[
27022709
'groceries' => ['max' => 50],
27032710
'transport' => ['max' => 100],
27042711
'entertainment' => ['max' => 300],
27052712
];
2706-
2713+
*/
27072714
```
27082715

27092716
#### min
@@ -2731,13 +2738,14 @@ $aggregation = Group::by('type')->min('price', 'min');
27312738
$result = CollectionFactory::from($items)
27322739
->stream()
27332740
->collect($aggregation)
2734-
; // ->
2741+
;
2742+
/* Result ->
27352743
[
27362744
'groceries' => ['min' => 30],
27372745
'transport' => ['min' => 50],
27382746
'entertainment' => ['min' => 30],
27392747
];
2740-
2748+
*/
27412749
```
27422750

27432751
#### sum
@@ -2765,13 +2773,14 @@ $aggregation = Group::by('type')->sum('price', 'spent');
27652773
$result = CollectionFactory::from($items)
27662774
->stream()
27672775
->collect($aggregation)
2768-
; // ->
2776+
;
2777+
/* Result ->
27692778
[
27702779
'groceries' => ['spent' => 80],
27712780
'transport' => ['spent' => 150],
27722781
'entertainment' => ['spent' => 330],
27732782
];
2774-
2783+
*/
27752784
```
27762785

27772786
#### addAggregator
@@ -2806,13 +2815,14 @@ $aggregation = Group::by('type')->addAggregator('names', function (Collection $c
28062815
$result = CollectionFactory::from($items)
28072816
->stream()
28082817
->collect($aggregation)
2809-
; // ->
2818+
;
2819+
/* Result ->
28102820
[
28112821
'groceries' => ['names' => 'potato, milk'],
28122822
'transport' => ['names' => 'taxi, airplane'],
28132823
'entertainment' => ['names' => 'Knicks game, cinema'],
28142824
];
2815-
2825+
*/
28162826
```
28172827

28182828
Все агрегаторы можно комбинировать между собой для получения желаемого результата.
@@ -2835,11 +2845,12 @@ $aggregation = Group::by('type')->avg('price', 'avg')->min('price', 'min')->max(
28352845
$result = CollectionFactory::from($items)
28362846
->stream()
28372847
->collect($aggregation)
2838-
; // ->
2848+
;
2849+
/* Result ->
28392850
[
28402851
'groceries' => ['avg' => 40, 'min' => 30, 'max' => 50],
28412852
'transport' => ['avg' => 75, 'min' => 50, 'max' => 100],
28422853
'entertainment' => ['avg' => 165, 'min' => 30, 'max' => 300],
28432854
];
2844-
2855+
*/
28452856
```

0 commit comments

Comments
 (0)