PHP code example of karlomikus / recipe-utils

1. Go to this page and download the library: Download karlomikus/recipe-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

karlomikus / recipe-utils example snippets




declare(strict_types=1);

use Kami\RecipeUtils\ParserFactory;
use Kami\RecipeUtils\Parser\UnitParser;
use Kami\RecipeUtils\UnitConverter\Units;

// Create parser with sensible defaults
$ingredientParser = ParserFactory::make();

// Parse a single ingredient line
$ingredient = $ingredientParser->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('1/2 - 1 ounce lime juice (freshly squeezed)')->convertTo(Units::Ml);
var_dump($ingredient);
// Output:
// $ingredient->amount === 15.0
// $ingredient->amount_max === 30.0
// $ingredient->units === 'ml'
// $ingredient->name === 'lime juice'
// $ingredient->comment === 'freshly squeezed'



declare(strict_types=1);

use Kami\RecipeUtils\Converter;
use Kami\RecipeUtils\UnitConverter\Oz;
use Kami\RecipeUtils\UnitConverter\Units;
use Kami\RecipeUtils\UnitConverter\AmountValue;

// Via existing ingredient object
$ingredientToConvert = new RecipeIngredient(
    name: 'Vodka',
    amount: '1 1/2',
    units: 'oz',
)->convertTo(Units::Ml);

$convertedIngredient = Converter::tryConvert($ingredientToConvert, Units::Ml);
var_dump($convertedIngredient);
// Output:
// $ingredient->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