Initial checkin

This commit is contained in:
2019-07-08 01:13:30 +02:00
commit 6e662f0263
19 changed files with 795 additions and 0 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace NoccyLabs\Juicer\Recipe\Exporter;
use NoccyLabs\Juicer\Recipe\RecipeInterface;
use NoccyLabs\Juicer\Ingredient\IngredientInterface;
class JsonExporter
{
public function export(RecipeInterface $recipe)
{
$ingredients = [];
/** @var IngredientInterface $ingredient */
foreach ($recipe->getIngredients() as $ingredient) {
$ingredients[] = [
'brand' => $ingredient->getFlavorBrand(),
'flavor' => $ingredient->getFlavorName(),
'percent' => $ingredient->getPercent()
];
}
$document = [
'recipe' => $recipe->getRecipeName(),
'author' => $recipe->getRecipeAuthor(),
'tags' => $recipe->getTags(),
'description' => $recipe->getDescription(),
'extra' => $recipe->getExtra(),
'ingredients' => $ingredients
];
return json_encode($document,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function writeToFile(RecipeInterface $recipe, string $filename)
{
$json = $this->export($recipe);
$fd = fopen($filename, "w");
if (!$fd) {
throw new \InvalidArgumentException();
}
fwrite($fd, $json, strlen($json));
fclose($fd);
}
}