karlomikus / recipe-utils
从菜谱中提取标准化配料数据的实用工具
0.12.0
2024-06-03 17:07 UTC
Requires
- php: ^8.2
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/php-code-coverage: ^10.1
- phpunit/phpunit: ^10.3
- symfony/var-dumper: ^7.1
- symplify/easy-coding-standard: ^12.0
README
用于从(主要是鸡尾酒)菜谱中提取配料数据到结构化对象的实用工具。
安装
通过 composer 安装
$ composer require karlomikus/recipe-utils
解析器使用
所有解析方法都返回类型为 Kami\RecipeUtils\RecipeIngredient
的对象。
<?php declare(strict_types=1); use Kami\RecipeUtils\Parser\Parser; use Kami\RecipeUtils\Parser\UnitParser; use Kami\RecipeUtils\UnitConverter\Units; $ingredientParser = new Parser(); // 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('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'
单位转换器使用
使用枚举实现简单单位转换。并非用于精确度。主要处理鸡尾酒菜谱单位(ml, oz, cl, dash...)。可以处理分数显示数量(¾, 1 1/2..)。
<?php declare(strict_types=1); use Kami\RecipeUtils\Converter; use Kami\RecipeUtils\UnitConverter\Oz; use Kami\RecipeUtils\UnitConverter\Units; use Kami\RecipeUtils\UnitConverter\AmountValue; // Via converter service $ingredientToConvert = new RecipeIngredient( name: 'Vodka', amount: '1 1/2', units: 'oz', ); $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