Skip to content

Commit b2084e0

Browse files
Alena HolliganAlena Holligan
Alena Holligan
authored and
Alena Holligan
committed
basic recipe code
1 parent df09828 commit b2084e0

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-0
lines changed

classes/recipe.php

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
class Recipe {
3+
private $title = "";
4+
private $ingredients = array();
5+
private $instructions = array();
6+
private $yield = "";
7+
private $tags = array();
8+
9+
public function __construct($title)
10+
{
11+
$this->setTitle($title);
12+
}
13+
14+
15+
public function getTitle()
16+
{
17+
return $this->title;
18+
}
19+
public function setTitle($title)
20+
{
21+
$this->title = mb_convert_case($title, MB_CASE_TITLE, "UTF-8");
22+
}
23+
24+
25+
public function getIngredientCount()
26+
{
27+
return count($this->getIngredientsArray());
28+
}
29+
public function getLastIngredient()
30+
{
31+
return $this->getIngredientsArray()[$this->getIngredientCount()-1]["ingredient"];
32+
}
33+
function getIngredients()
34+
{
35+
return $this->getIngredientsArray();
36+
}
37+
public function getIngredientsParagraphs()
38+
{
39+
$output = "";
40+
foreach ($this->ingredients as $i) {
41+
var_dump($i);
42+
$output .= "<p>" . $i["amount"] . " " . $i["measure"] . " " .$i["ingredient"] ."</p>";
43+
}
44+
return $output;
45+
}
46+
public function getIngredientsList()
47+
{
48+
$output = "<ul>";
49+
foreach ($this->ingredients as $i) {
50+
$output .= "<li>" . $i["amount"] . " " . $i["measure"] . " " . $i["ingredient"] ."</li>";
51+
}
52+
$output .= "</ul>";
53+
return $output;
54+
}
55+
public function getIngredientsArray()
56+
{
57+
return $this->ingredients;
58+
}
59+
public function setIngredients($ingredients)
60+
{
61+
$this->ingredients = $ingredients;
62+
}
63+
public function addIngredient($ingredient, $amount = null, $measure = null)
64+
{
65+
$this->ingredients[] = array(
66+
"amount" => $amount,
67+
"measure" => $measure,
68+
"ingredient" => ucwords($ingredient)
69+
);
70+
}
71+
72+
73+
public function getInstructions()
74+
{
75+
return $this->getInstructionsArray();
76+
}
77+
public function getInstructionsParagraphs()
78+
{
79+
return "<p>" . implode("</p><p>",$this->instructions) . "</p>";
80+
}
81+
public function getInstructionsList()
82+
{
83+
return "<ol><li>" . implode("</li><li>",$this->instructions) . "</li></ol>";
84+
}
85+
public function getInstructionsArray()
86+
{
87+
return $this->instructions;
88+
}
89+
public function setInstructions($instructions)
90+
{
91+
$this->instructions = $instructions;
92+
}
93+
public function addInstruction($instruction)
94+
{
95+
$this->instructions[] = $instruction;
96+
}
97+
98+
99+
public function getYield()
100+
{
101+
return $this->yield;
102+
}
103+
public function setYield($yield)
104+
{
105+
$this->yield = $yield;
106+
}
107+
108+
109+
public function getTags()
110+
{
111+
return $this->getTagsArray();
112+
}
113+
public function getTagsString()
114+
{
115+
return implode(",",$this->tags);
116+
}
117+
public function getTagsList()
118+
{
119+
return "<ol><li>" . implode("</li><li>",$this->tags) . "</li></ol>";
120+
}
121+
public function getTagsArray()
122+
{
123+
return $this->tags;
124+
}
125+
public function setTags($tags)
126+
{
127+
if (is_array($tags)) {
128+
$this->tags = $tags;
129+
} else {
130+
$this->tags = explode(",",ucwords($tags));
131+
}
132+
}
133+
public function addTag($tags)
134+
{
135+
$this->tags[] = $tags;
136+
}
137+
138+
139+
public function displayRecipe()
140+
{
141+
$output = "<h2>" . $this->getTitle() ."</h2>";
142+
$output .= "<p><em>" . $this->getTagsString() . "</em></p>";
143+
$output .= $this->getIngredientsList();
144+
$output .= $this->getInstructionsParagraphs();
145+
$output .= "<p>Yield: <em>" . $this->getYield() . "</em></p>";
146+
return $output;
147+
}
148+
}

classes/recipecollection.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
class RecipeCollection {
4+
private $title = "";
5+
private $recipes = array();
6+
7+
public function __construct($title = null)
8+
{
9+
if (!empty($title)){
10+
$this->title = ucwords($title);
11+
}
12+
}
13+
14+
public function getTitle()
15+
{
16+
return $this->title;
17+
}
18+
19+
public function getRecipes()
20+
{
21+
return $this->getRecipeArray();
22+
}
23+
public function getRecipeList($list = null)
24+
{
25+
if ($list == null) {
26+
$list = $this->recipes;
27+
}
28+
$output = "<ul>";
29+
foreach ($list as $r) {
30+
$output .= "<li>" . $r->getTitle(). " (".$r->getTagsString().")</li>";
31+
}
32+
$output .= "</ul>";
33+
return $output;
34+
}
35+
public function getRecipeArray()
36+
{
37+
return $this->recipes;
38+
}
39+
40+
public function setRecipes($recipes)
41+
{
42+
if (is_array($recipes)) {
43+
$this->recipes = $recipes;
44+
} else {
45+
$this->recipes = explode(",",$recipes);
46+
}
47+
}
48+
public function addRecipe($recipe)
49+
{
50+
$this->recipes[] = $recipe;
51+
}
52+
53+
public function getRecipeCount()
54+
{
55+
return count($this->recipes);
56+
}
57+
58+
public function shoppingList(){
59+
$shopping_list = array();
60+
$output = "<ul>";
61+
foreach ($this->recipes as $recipe) {
62+
foreach ($recipe->getIngredientsArray() as $i) {
63+
$shopping_list[strtolower($i["ingredient"])][] = $i["amount"]." ".$i["measure"];
64+
}
65+
}
66+
ksort($shopping_list);
67+
foreach ($shopping_list as $i => $amount) {
68+
$output .= "<li>".ucwords($i).": ".implode(", ",$amount);
69+
}
70+
$output .= "</ul>";
71+
return $output;
72+
}
73+
}

cookbook.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
include("classes/recipe.php");
3+
include("classes/recipecollection.php");
4+
include("recipes.php");
5+
6+
$cookbook = new RecipeCollection("Favorite Recipes");
7+
8+
$cookbook->addRecipe($recipe1);
9+
$cookbook->addRecipe($recipe2);
10+
11+
$meal_plan = new RecipeCollection();
12+
$meal_plan->addRecipe($recipe1);
13+
14+
echo "<h1>".$cookbook->getTitle()."</h1>";
15+
echo "<p>This collection contains the following " . $cookbook->getRecipeCount() . " recipes:</p>";
16+
echo $cookbook->getRecipeList($cookbook->getRecipes());
17+
18+
foreach ($cookbook->getRecipes() as $recipe) {
19+
echo $recipe->displayRecipe();
20+
}
21+
22+
23+
echo "<h1>Shopping List</h1>";
24+
echo $cookbook->shoppingList();
25+

recipes.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
$recipe1 = new Recipe("Belgium Waffles");
4+
5+
$recipe1->setIngredients([
6+
[
7+
"ingredient" => "egg",
8+
"amount" => "1",
9+
"measure" => null
10+
],
11+
[
12+
"ingredient" => "sugar",
13+
"amount" => "1/2",
14+
"measure" => "Cup"
15+
]
16+
]);
17+
$recipe1->addIngredient("melted butter","1","Cup");
18+
$recipe1->addIngredient("vanilla","2","tsp");
19+
$recipe1->addIngredient("baking powder","1","Tbs");
20+
$recipe1->addIngredient("flour","2","Cups");
21+
$recipe1->addIngredient("milk","1 1/2","Cups");
22+
23+
$recipe1->addInstruction("Separate eggs. Whip egg whites until stiff peaks form. Set asside.");
24+
$recipe1->addInstruction("Melt butter. Combine the rest of the ingredients except milk and mix well. Slowly add milk until combined.");
25+
$recipe1->addInstruction("Fold in egg whites. Follow instructions on waffle maker or add 1/2 cup of batter to waffle iron and cook for 4 minutes.");
26+
27+
$recipe1->setYield("4 one cup servings");
28+
$recipe1->setTags("breakfast");
29+
30+
$recipe2 = new Recipe("Italian Lemon Chicken");
31+
32+
$recipe2->addIngredient("Pasta, boiled","1","lbs");
33+
$recipe2->addIngredient("Chicken Breast","2","lbs");
34+
$recipe2->addIngredient("olive oil","1/2","Cup");
35+
$recipe2->addIngredient("chopped garlic","2","Tbls");
36+
$recipe2->addIngredient("lemon juice","1/4","Cup");
37+
$recipe2->addIngredient("sugar","1/2","tsp");
38+
$recipe2->addIngredient("parsley","2","tsp");
39+
$recipe2->addIngredient("oregano","2","tsp");
40+
$recipe2->addIngredient("basil","1","Tbls");
41+
$recipe2->addIngredient("parmesian cheese to taste");
42+
43+
$recipe2->addInstruction("In a large skillet on medium high heat, saute garlic in olive oil for 3 minutes. Cut chicken into bite sized pieces.");
44+
$recipe2->addInstruction("Add additional ingredients to sauce pan and cover for 5 minutes or untill chicken is almost completely white.");
45+
$recipe2->addInstruction("Remove lid and cook until reduced to a thick sauce.");
46+
$recipe2->addInstruction("Serve over pasta with ".$recipe2->getLastIngredient());
47+
48+
$recipe2->setYield("6 Servings");
49+
$recipe2->setTags(["Main Dish", "Dinner"]);

0 commit comments

Comments
 (0)