Added examples, improved recipe implementation

This commit is contained in:
Chris 2019-07-09 17:59:45 +02:00
parent 922d9f09dd
commit 04f357fc0a
3 changed files with 88 additions and 7 deletions

32
examples/bases.php Normal file
View File

@ -0,0 +1,32 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Juicer\Ingredient\Base;
use NoccyLabs\Juicer\Ingredient\NicotineBase;
printf("Apparent Specific Gravity of base mixes:\n\n");
printf(" VG | PG | ASG\n");
printf(" ------|------|------------------------\n");
for ($pg = 0; $pg < 100; $pg += 10) {
$base = new Base("PG{$pg}");
printf(" %3d%% | %3d%% | %.4fg/mL\n", 100-$pg, $pg, $base->getSpecificGravity());
}
echo "\n\n";
$asgstrength = [ 24, 20, 18, 15 ];
$asgheader = join("", array_map(function ($s) { return sprintf("%2dmg ", $s); } , $asgstrength));
printf("Apparent Specific Gravity of nicotine bases:\n\n");
printf(" VG | PG | %s\n", $asgheader);
printf(" ------|------|-------------------------------------\n");
for ($pg = 0; $pg < 100; $pg += 10) {
printf(" %3d%% | %3d%% |", 100-$pg, $pg);
$base = new Base("PG{$pg}");
foreach($asgstrength as $s) {
$nicbase = new NicotineBase($base, $s);
printf(" %6.4f ", $nicbase->getSpecificGravity());
}
printf("\n");
}

View File

@ -24,39 +24,59 @@ class Recipe implements RecipeInterface
/** @var IngredientInterface[] */
protected $ingredients = [];
public function setRecipeName(?string $name)
public function setRecipeName(?string $name): Recipe
{
$this->name = $name;
return $this;
}
public function setRecipeAuthor(?string $author)
public function setRecipeAuthor(?string $author): Recipe
{
$this->author = $author;
return $this;
}
public function setTags(array $tags)
public function setTags(array $tags): Recipe
{
$this->tags = $tags;
return $this;
}
public function addTag(string $tag)
public function addTag(string $tag): Recipe
{
$this->tags[] = $tag;
return $this;
}
public function setDescription(?string $description)
public function hasTag(string $tag): bool
{
return in_array($tag, $this->tags);
}
public function removeTag(string $tag): Recipe
{
$this->tags = array_filter($this->tags, function ($input) use ($tag) {
return $input != $tag;
});
return $this;
}
public function setDescription(?string $description): Recipe
{
$this->description = $description;
return $this;
}
public function addIngredient(IngredientInterface $ingredient)
public function addIngredient(IngredientInterface $ingredient): Recipe
{
$this->ingredients[] = $ingredient;
return $this;
}
public function setExtra(array $extra)
public function setExtra(array $extra): Recipe
{
$this->extra = $extra;
return $this;
}
/**

View File

@ -25,4 +25,33 @@ class RecipeTest extends \PhpUnit\Framework\TestCase
$this->assertEquals([$ingredient1, $ingredient2], $recipe->getIngredients());
}
public function testManipulatingRecipeTags()
{
$recipe = new Recipe();
$recipe
->addTag("foo")
->addTag("bar");
$this->assertCount(2, $recipe->getTags());
$this->assertTrue($recipe->hasTag("foo"));
$this->assertTrue($recipe->hasTag("bar"));
$recipe->removeTag("foo");
$this->assertCount(1, $recipe->getTags());
$this->assertTrue($recipe->hasTag("bar"));
$recipe->addTag("baz");
$this->assertCount(2, $recipe->getTags());
$this->assertFalse($recipe->hasTag("foo"));
$this->assertTrue($recipe->hasTag("bar"));
$this->assertTrue($recipe->hasTag("baz"));
$recipe->setTags(["baz"]);
$this->assertCount(1, $recipe->getTags());
$this->assertFalse($recipe->hasTag("foo"));
$this->assertFalse($recipe->hasTag("bar"));
$this->assertTrue($recipe->hasTag("baz"));
}
}