Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/karlomikus/recipe-utils

Utilities for extracting ingredient data from (mostly cocktail) recipes into structured objects.
https://github.com/karlomikus/recipe-utils

Last synced: about 2 months ago
JSON representation

Utilities for extracting ingredient data from (mostly cocktail) recipes into structured objects.

Awesome Lists containing this project

README

        

# Recipe Utilities

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/karlomikus/recipe-utils/code.yml)
![Packagist Version](https://img.shields.io/packagist/v/karlomikus/recipe-utils)
![GitHub License](https://img.shields.io/github/license/karlomikus/recipe-utils)

Utilities for extracting ingredient data from (mostly cocktail) recipes into structured objects.

## Install

Install via composer

```bash
$ composer require karlomikus/recipe-utils
```

## Parser usage

All parse methods return object of type `Kami\RecipeUtils\RecipeIngredient`.

```php
parseLine('30 ml Tequila reposado (preferebly Patron)');
var_dump($ingredient);
// Output:
// $ingredient->amount === '30'
// $ingredient->units === 'ml'
// $ingredient->name === 'Tequila reposado'
// $ingredient->comment === 'preferebly Patron'
// $ingredient->source === '30 ml Tequila reposado'

// Parse a line and convert units if possible
$ingredient = $ingredientParser->parseLine('30 ml Tequila reposado (preferebly Patron)', Units::Oz);
var_dump($ingredient);
// Output:
// $ingredient->amount === 1.0
// $ingredient->units === 'oz'
// $ingredient->name === 'Tequila reposado'
// $ingredient->comment === 'preferebly Patron'
// $ingredient->source === '30 ml Tequila reposado'

// Available via static call
$ingredient = Parser::line('30 ml Tequila reposado (preferebly Patron)');

// Add custom units
$ingredientParser->setUnitParser(
new UnitParser([
'test' => ['lorem', 'ipsum']
])
);

$ingredient = $ingredientParser->parseLine('15 lorem ingredient names');
// Output:
// $ingredient->units === 'test'
```

## Unit converter usage

Simple unit conversion implemented with enums. Not made for accuracy. Handles mostly cocktail recipe units (ml, oz, cl, dash...). Can handle fractional display amounts (¾, 1 1/2..).

```php
amount === 45.0
// $ingredient->units === 'ml'
// $ingredient->name === 'Vodka'

// Via specific units
$amountValue = AmountValue::fromString('1 1/2');
var_dump((new Oz($amountValue))->toMl()->getValue());
// Output:
// float: 45.0
```