Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/n1crack/subset-finder
A Laravel collection-powered PHP package, enables efficient extraction of subsets from collections, ideal for discount calculations and inventory management.
https://github.com/n1crack/subset-finder
collection discount ecommerce php subset-selection
Last synced: 28 days ago
JSON representation
A Laravel collection-powered PHP package, enables efficient extraction of subsets from collections, ideal for discount calculations and inventory management.
- Host: GitHub
- URL: https://github.com/n1crack/subset-finder
- Owner: n1crack
- License: mit
- Created: 2024-02-06T08:02:16.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-07-08T14:56:58.000Z (4 months ago)
- Last Synced: 2024-10-02T08:13:19.517Z (about 1 month ago)
- Topics: collection, discount, ecommerce, php, subset-selection
- Language: PHP
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# SubsetFinder PHP Package
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ozdemir/subset-finder)](https://packagist.org/packages/ozdemir/subset-finder)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/n1crack/subset-finder/run-tests.yml)](https://github.com/n1crack/subset-finder/actions)
[![GitHub](https://img.shields.io/github/license/n1crack/subset-finder)](https://github.com/n1crack/subset-finder/blob/main/LICENSE.md)The SubsetFinder PHP Package, utilizing Laravel collection functionality, empowers users to efficiently identify subsets within a given collection according to defined criteria. This tool proves invaluable in scenarios requiring extraction of specific item subsets from a larger collection, such as in discount calculation algorithms or inventory management systems
## Installation
You can install the SubsetFinder package via Composer:```zsh
composer require ozdemir/subset-finder
```## Usage
Here's a basic example of how to use the SubsetFinder package:```php
use Ozdemir\SubsetFinder\SubsetFinder;
use Ozdemir\SubsetFinder\SubsetCollection;
use Ozdemir\SubsetFinder\Subset;// Define your collection and subset criteria
// Collection should be an instance of Illuminate\Support\Collection
// and contain items that implement the Ozdemir\SubsetFinder\Subsetable interface.// example class that implements the Subsetable interface
// if you use field names other than 'id' and 'quantity', you need to define them with defineProps method
class Something implements Subsetable
{
public function __construct(public int $id, public int $quantity, public int $price)
{
}public function getId(): int
{
return $this->id;
}public function getQuantity(): int
{
return $this->quantity;
}
}$collection = collect([
new Something(id: 1, quantity: 11, price: 15),
new Something(id: 2, quantity: 6, price: 5),
new Something(id: 3, quantity: 6, price: 5)
// Add more items...
]);$subsetCollection = new SubsetCollection([
Subset::of([1, 2])->take(5),
Subset::of([3])->take(2),
// Add more criteria...
]);// Instantiate SubsetFinder
$subsetter = new SubsetFinder($collection, $subsetCollection);// Optionally, configure sorting
$subsetter->sortBy('price');
// Solve the problem
$subsetter->solve();// $subsets will contain the subsets that meet the criteria
$subsets = $subsetter->getFoundSubsets();
// Illuminate\Support\Collection:
// all:[
// Something(id: 2, quantity: 6, price: 5),
// Something(id: 1, quantity: 9, price: 15),
// Something(id: 3, quantity: 6, price: 5)
// ]// $remaining will contain the items that were not selected for any subset
$remaining = $subsetter->getRemaining();
// Illuminate\Support\Collection:
// all:[
// Something(id: 1, quantity: 2, price: 15),
// ]// Get the maximum quantity of sets that can be created from the collection.
$subSetQuantity = $subsetter->getSubsetQuantity()
// 3```
You can check the tests for more examples.
## Configuration
### Prioritize items to be included in the subset
```php
// Seek subsets that meet the criteria
$subsetCollection = new SubsetCollection([
Subset::of([1, 2, 3])->take(5), // Find a subset with a total quantity of 5 from items 1, 2, and 3 in the collection
Subset::of([4, 5])->take(2), // Find a subset with a total quantity of 2 from items 4 and 5 in the collection
Subset::of([12])->take(5), // Find a subset with a total quantity of 5 from item 12 in the collection
// etc...
]);// When we have multiple applicable items for a subset, we can choose to prioritize the ones
// with any field that exists in the main collection.
$subsetter->sortBy('price');
$subsetter->solve();```
### Define the field names for the quantity, items and id fields.
```php
$collection = collect([
new Something() // let's say, we have an object with the ["name" => 1, "amount" => 11, "price" => 15]
// Add more items...
]);// Find a subset with a total amount of 5 from items named 1 and 2 in the collection
// this part doesn't change
$setCollection = collect([
Subset::of([1, 2])->take(5)
// define more...
]);// We need to define the field names for the 'quantity' and 'id' fields.
$subsetter->defineProps(
id: 'name',
quantity: 'amount'
);$subsetter->solve();
```
## Testing
You can run the tests with:```zsh
composer test
```## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or create a pull request on GitHub.## License
This package is open-sourced software licensed under the MIT license.