11<?php
22
3+ declare (strict_types=1 );
4+
35namespace SebastiaanLuca \Helpers \Collections ;
46
57use Carbon \Carbon ;
@@ -10,21 +12,25 @@ class CollectionMacrosServiceProvider extends ServiceProvider
1012{
1113 /**
1214 * Boot the service provider.
15+ *
16+ * @return void
1317 */
14- public function boot ()
18+ public function boot () : void
1519 {
16- /**
20+ /*
1721 * Create Carbon instances from items in a collection.
1822 */
23+
1924 Collection::macro ('carbonize ' , function () {
2025 return collect ($ this ->items )->map (function ($ time ) {
2126 return new Carbon ($ time );
2227 });
2328 });
2429
25- /**
30+ /*
2631 * Reduce each collection item to the value found between a given start and end string.
2732 */
33+
2834 Collection::macro ('between ' , function ($ start , $ end = null ) {
2935 $ end = $ end ?? $ start ;
3036
@@ -37,46 +43,51 @@ public function boot()
3743 });
3844 });
3945
40- /**
46+ /*
4147 * Perform an operation on the collection's keys.
4248 */
49+
4350 Collection::macro ('transformKeys ' , function (callable $ operation ) {
4451 return collect ($ this ->items )->mapWithKeys (function ($ item , $ key ) use ($ operation ) {
4552 return [$ operation ($ key ) => $ item ];
4653 });
4754 });
4855
49- /**
56+ /*
5057 * Transpose (flip) a collection matrix (array of arrays).
5158 *
5259 * @see https://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/
5360 */
61+
5462 Collection::macro ('transpose ' , function () {
5563 if ($ this ->isEmpty ()) {
5664 return $ this ;
5765 }
58-
66+
5967 $ items = array_map (function (...$ items ) {
6068 return $ items ;
6169 }, ...$ this ->values ());
6270
6371 return new static ($ items );
6472 });
6573
66- /**
74+ /*
6775 * Transpose (flip) a collection matrix (array of arrays) while keeping its columns and row headers intact.
6876 *
6977 * Please note that a row missing a column another row does have can only occur for one column. It cannot
7078 * parse more than one missing column.
7179 */
72- Collection::macro ('transposeWithKeys ' , function (array $ rows = null ) {
80+
81+ Collection::macro ('transposeWithKeys ' , function (?array $ rows = null ) {
7382 if ($ this ->isEmpty ()) {
7483 return $ this ;
7584 }
76-
77- $ rows = $ rows ?? $ this ->values ()->reduce (function (array $ rows , array $ values ) {
85+
86+ if ($ rows === null ) {
87+ $ rows = $ this ->values ()->reduce (function (array $ rows , array $ values ) {
7888 return array_unique (array_merge ($ rows , array_keys ($ values )));
7989 }, []);
90+ }
8091
8192 $ keys = $ this ->keys ()->toArray ();
8293
0 commit comments