Skip to content

Commit

Permalink
add getSubsetItems method
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack committed Feb 7, 2024
1 parent f998fd2 commit ade06a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/SubsetFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,15 @@ protected function calculateRemainingQuantity(array $item, array $setItems): arr

return $item;
}

/**
* Return a subset of the collection based on the given integer.
*
* @param int $int
* @return Collection
*/
public function getSubsetItems(int $int)
{
return $this->getFlatCollection()->take($int);
}
}
33 changes: 33 additions & 0 deletions tests/SubsetFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,39 @@
]);
});

it('can get n many items as ordered', function () {
$collection = collect([
["id" => 1, "quantity" => 11, "price" => 15],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 3, "quantity" => 18, "price" => 10],
["id" => 5, "quantity" => 4, "price" => 2],
["id" => 12, "quantity" => 5, "price" => 6],
]);

$setCollection = new SubsetCollection([
Subset::of([1, 2, 3, 5, 12])->take(5),
]);

$subsetter = new SubsetFinder($collection, $setCollection);
$subsetter->sortBy('price');


// somemethod will return the first 11 items that is ordered
expect($subsetter->getSubsetItems(11)->toArray())->toBe([
["id" => 5, "quantity" => 4, "price" => 2],
["id" => 5, "quantity" => 4, "price" => 2],
["id" => 5, "quantity" => 4, "price" => 2],
["id" => 5, "quantity" => 4, "price" => 2],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 2, "quantity" => 6, "price" => 5],
["id" => 12, "quantity" => 5, "price" => 6],
]);
});


it('can get the subsets with large number of sets', function() {
$collection = collect([
Expand Down

0 comments on commit ade06a4

Please sign in to comment.