Last active
March 20, 2019 17:15
-
-
Save alexbowers/81efbe2f4962b698ebe7a4342b88dd97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Collection::macro('mapAttachUntil', new mapAttachUntil); | |
// Examples: | |
collect([ | |
[ | |
'Action' => 'Category', | |
'Name' => 'One', | |
], | |
])->mapAttachUntil('Action', 'Category')->dd(); | |
// Output: | |
// array:1 [ | |
// 0 => array:2 [ | |
// "Action" => "Category" | |
// "Name" => "One" | |
// ] | |
// ] | |
collect([ | |
[ | |
'Action' => 'Category', | |
'Name' => 'One', | |
], | |
[ | |
'Action' => 'Image', | |
'Name' => 'A', | |
], | |
])->mapAttachUntil('Action', 'Category')->toArray(); | |
// Output: | |
// array:1 [ | |
// 0 => array:3 [ | |
// "Action" => "Category" | |
// "Name" => "One" | |
// "Image" => array:1 [ | |
// 0 => array:2 [ | |
// "Action" => "Image" | |
// "Name" => "A" | |
// ] | |
// ] | |
// ] | |
// ] | |
collect([ | |
[ | |
'Action' => 'Category', | |
'Name' => 'One', | |
], | |
[ | |
'Action' => 'Category', | |
'Name' => 'One 2', | |
], | |
])->mapAttachUntil('Action', 'Category')->toArray(); | |
// Output: | |
// array:2 [ | |
// 0 => array:2 [ | |
// "Action" => "Category" | |
// "Name" => "One" | |
// ] | |
// 1 => array:2 [ | |
// "Action" => "Category" | |
// "Name" => "One 2" | |
// ] | |
// ] | |
collect([ | |
[ | |
'Action' => 'Category', | |
'Name' => 'One', | |
], | |
[ | |
'Action' => 'Image', | |
'Name' => 'A', | |
], | |
[ | |
'Action' => 'Image', | |
'Name' => 'B', | |
], | |
[ | |
'Action' => 'Category', | |
'Name' => 'One 2', | |
], | |
[ | |
'Action' => 'Image', | |
'Name' => 'D', | |
], | |
[ | |
'Action' => 'Image', | |
'Name' => 'E', | |
], | |
])->mapAttachUntil('Action', 'Category')->toArray(); | |
// Output: | |
// array:2 [ | |
// 0 => array:3 [ | |
// "Action" => "Category" | |
// "Name" => "One" | |
// "Image" => array:2 [ | |
// 0 => array:2 [ | |
// "Action" => "Image" | |
// "Name" => "A" | |
// ] | |
// 1 => array:2 [ | |
// "Action" => "Image" | |
// "Name" => "B" | |
// ] | |
// ] | |
// ] | |
// 1 => array:3 [ | |
// "Action" => "Category" | |
// "Name" => "One 2" | |
// "Image" => array:2 [ | |
// 0 => array:2 [ | |
// "Action" => "Image" | |
// "Name" => "D" | |
// ] | |
// 1 => array:2 [ | |
// "Action" => "Image" | |
// "Name" => "E" | |
// ] | |
// ] | |
// ] | |
// ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class mapAttachUntil | |
{ | |
public function __invoke() | |
{ | |
return function (string $by, string $until) { | |
$response = collect(); | |
$current = collect(); | |
$attach = []; | |
$this->each(function($record) use ($by, $until, &$response, &$current, &$attach) { | |
if ($current->isEmpty()) { | |
$current = collect($record); | |
} else if (data_get($record, $by) === $until) { | |
$current = $current->merge($attach); | |
$attach = []; | |
$response->push($current->toArray()); | |
$current = collect($record); | |
} else { | |
$attach[data_get($record, $by)][] = $record; | |
} | |
}); | |
if ($current->isNotEmpty()) { | |
$current = $current->merge($attach); | |
$attach = []; | |
$response->push($current->toArray()); | |
$current = collect(); | |
} | |
return $response; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment