karlomikus/recipe-utils 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

karlomikus/recipe-utils

最新稳定版本:0.17.0

Composer 安装命令:

composer require karlomikus/recipe-utils

包简介

Utilities for extracting normalized ingredient data from recipes

README 文档

README

GitHub Actions Workflow Status Packagist Version GitHub License

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

Install

Install via composer

$ composer require karlomikus/recipe-utils

Parser usage

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

Supported formats:

  • Variable amounts and fractional amounts: "1 - 2", "0.5 to 1", "½ or 2 1/5"
  • Comments are anything after comma or anything in brackets: "12 ml ingredient, preferebly something specific"
  • Basic units mostly used in cocktails: ml, cl, oz, sprigs, dashes, etc...
<?php

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'

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

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

统计信息

  • 总下载量: 3.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-07