Skip to content

Commit

Permalink
fix: use groupBy correctly eliminating the need for a new method
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Jan 7, 2025
1 parent 5ff5402 commit 7366165
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
6 changes: 5 additions & 1 deletion src/Helper/MyParcelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,12 @@ public function createConcepts(): self
$newConsignments = $this->where('consignment_id', '!=', null)->toArray();
$this->addMissingReferenceId();

$grouped = $this->where('consignment_id', null)->groupBy(function($item) {
return $item->getApiKey() . ($item->hasSender() ? '-sender' : '');
});

/* @var MyParcelCollection $consignments */
foreach ($this->where('consignment_id', null)->groupInto(['apiKey', 'sender']) as $consignments) {
foreach ($grouped as $consignments) {
$headers = MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT;
if ($consignments->first()->hasSender()) {
$headers += MyParcelRequest::HEADER_SET_CUSTOM_SENDER;
Expand Down
27 changes: 0 additions & 27 deletions src/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,33 +773,6 @@ public function groupBy($groupBy, $preserveKeys = false)
return $result;
}

/**
* Group this collection into separate collections by one or more fields.
* Returns an array of separate collections grouped by md5 encoded string of the grouped fields.
*
* @param array $by
* @return array
*/
public function groupInto(array $by): array {
$result = [];

foreach ($this->items as $item) {
$group = '';
foreach ($by as $key) {
$group .= $this->helper->data_get($item, $key);
}
$group = md5($group);

if (!isset($result[$group])) {
$result[$group] = new static();
}

$result[$group]->push($item);
}

return $result;
}

/**
* Key an associative array by a field or using a callback.
*
Expand Down

0 comments on commit 7366165

Please sign in to comment.