1 00:00:00,520 --> 00:00:04,530 We created our first getter and setter methods on the title property 2 00:00:04,530 --> 00:00:07,690 because we want to control the incoming format. 3 00:00:07,690 --> 00:00:11,310 The next property in the class is an array of ingredients. 4 00:00:11,310 --> 00:00:15,910 We could set this up as a simple array of strings, these strings 5 00:00:15,910 --> 00:00:19,950 would be the full ingredients just as you would see them in a recipe. 6 00:00:19,950 --> 00:00:24,970 Two cups flour, one egg, a cup and a half of milk, and so on. 7 00:00:24,970 --> 00:00:29,170 This would work fine, if we just want to print out a list of ingredients. 8 00:00:29,170 --> 00:00:32,790 It does make it harder to control the format this way. 9 00:00:32,790 --> 00:00:36,920 Are we going to accept numbers such as fractions, one-half, 10 00:00:36,920 --> 00:00:40,010 or decimals, like 0.5? 11 00:00:40,010 --> 00:00:44,160 It also makes it more difficult when it comes to combining ingredients 12 00:00:44,160 --> 00:00:47,790 to make a shopping list, which is something we'll be doing a little later. 13 00:00:48,930 --> 00:00:50,940 Instead of using a single string for 14 00:00:50,940 --> 00:00:55,830 each ingredient, we're going to set up our ingredients as an associative array. 15 00:00:55,830 --> 00:01:00,140 This will allow us to break our ingredients up into parts, and 16 00:01:00,140 --> 00:01:03,710 better control the formatting and merging of items. 17 00:01:03,710 --> 00:01:07,520 Let's look at an example of how the data will be stored. 18 00:01:07,520 --> 00:01:11,320 Each ingredient will be an array with three keyed elements. 19 00:01:11,320 --> 00:01:13,280 Items such as flour. 20 00:01:13,280 --> 00:01:15,850 Amount, such as 2.5. 21 00:01:15,850 --> 00:01:18,240 And measurement, such as cup. 22 00:01:18,240 --> 00:01:20,030 We'll add more ingredients and 23 00:01:20,030 --> 00:01:25,460 each of these individual ingredients will then be stored in a basic numbered array. 24 00:01:25,460 --> 00:01:27,110 Let's start adding the ingredients. 25 00:01:28,230 --> 00:01:30,780 Instead of just setting the property all at once, 26 00:01:30,780 --> 00:01:33,380 we're going to add individual ingredients. 27 00:01:33,380 --> 00:01:40,260 So we'll use public function addIngredient. 28 00:01:40,260 --> 00:01:45,238 This method will accept three arguments, 29 00:01:45,238 --> 00:01:48,570 item, amount and measure. 30 00:01:48,570 --> 00:01:51,620 Only the first item will be required. 31 00:01:51,620 --> 00:01:58,500 So we can add a default value of null to both the amount and the measure. 32 00:02:03,835 --> 00:02:09,027 This will allow us to accept things like, egg, one, without a measurement, 33 00:02:09,027 --> 00:02:13,055 or maybe lime to taste, without an amount or a measurement. 34 00:02:13,055 --> 00:02:15,584 After accepting these three arguments, 35 00:02:15,584 --> 00:02:19,425 we want to add them to our ingredients as an associative array. 36 00:02:19,425 --> 00:02:24,569 This ingredient, we'll add an element and 37 00:02:24,569 --> 00:02:28,050 our element will be an array. 38 00:02:30,440 --> 00:02:31,660 We'll have a key of item. 39 00:02:31,660 --> 00:02:35,620 And we'll set this to item. 40 00:02:37,480 --> 00:02:42,500 Our next key will be amount and we'll set this to amount. 41 00:02:44,570 --> 00:02:45,500 And finally measure. 42 00:02:52,440 --> 00:02:56,960 We can also format these values like using, UC words, on our item. 43 00:03:02,021 --> 00:03:06,830 We can also require that they pass a float or an integer as the amount. 44 00:03:06,830 --> 00:03:12,746 Before setting this ingredient, 45 00:03:12,746 --> 00:03:20,090 we'll check if amount is not equal null && 46 00:03:20,090 --> 00:03:24,782 not is_float amount and 47 00:03:24,782 --> 00:03:28,460 not is_int amount. 48 00:03:30,450 --> 00:03:32,956 If we haven't passed a float or an integer, 49 00:03:32,956 --> 00:03:35,615 then we want to exit our script with a message. 50 00:03:43,835 --> 00:03:47,640 We can also add more information about what they did pass. 51 00:03:49,210 --> 00:03:53,180 gettype, tells us which type the amount is. 52 00:04:02,900 --> 00:04:06,710 We can also require that they pass a valid measurement. 53 00:04:06,710 --> 00:04:11,800 To do this we have to first define which measurements we're going to accept. 54 00:04:11,800 --> 00:04:14,435 To do this, we're going to add another private property. 55 00:04:20,175 --> 00:04:25,170 Private because we don't want anything to be able to change our measurements. 56 00:04:25,170 --> 00:04:30,795 We'll call these measurements and we'll set it to an array. 57 00:04:30,795 --> 00:04:36,719 We want tsp, tbsp, 58 00:04:36,719 --> 00:04:42,294 cup, oz, pound, 59 00:04:42,294 --> 00:04:48,566 fluid ounce, pint, 60 00:04:48,566 --> 00:04:54,146 quart and gallon. 61 00:04:57,666 --> 00:05:00,940 You can change these however you want or add other values. 62 00:05:02,170 --> 00:05:05,717 Now that we have defined the acceptable measurements we can make sure that 63 00:05:05,717 --> 00:05:07,845 the measurement that we passed in is valid. 64 00:05:13,425 --> 00:05:19,049 If measure is not equal 65 00:05:19,049 --> 00:05:24,377 to null and in_array, 66 00:05:24,377 --> 00:05:29,705 check measure in this 67 00:05:29,705 --> 00:05:33,860 measurements. 68 00:05:33,860 --> 00:05:36,050 Again, we want to exit our script with a message. 69 00:05:45,075 --> 00:05:48,757 We can tell them which measurements are valid by using implode. 70 00:05:59,895 --> 00:06:03,590 I also want to make sure that it's checking the lowercase version of measure. 71 00:06:04,770 --> 00:06:07,180 str for string tolower. 72 00:06:11,320 --> 00:06:18,040 I also want to set the lowercase version of measure. 73 00:06:18,040 --> 00:06:19,560 Let's see if we can get some errors. 74 00:06:26,600 --> 00:06:29,900 Let's addIngredient. 75 00:06:31,180 --> 00:06:36,490 Press egg and the word one and then dozen. 76 00:06:38,790 --> 00:06:40,015 Now let's run this script in the console. 77 00:06:47,995 --> 00:06:52,450 The amount must be a float: string one given. 78 00:06:52,450 --> 00:06:53,370 Okay, great. 79 00:06:53,370 --> 00:06:55,170 Let's change this string to an int. 80 00:06:57,220 --> 00:06:58,398 Now let's try running it again. 81 00:06:58,398 --> 00:07:01,345 Undefined property. 82 00:07:05,405 --> 00:07:06,800 Let's look up here. 83 00:07:06,800 --> 00:07:12,840 This should be an s for measurements. 84 00:07:12,840 --> 00:07:13,790 Now let's run it again. 85 00:07:15,630 --> 00:07:20,470 Great, now we see please enter a valid measurement with our list of measurements. 86 00:07:20,470 --> 00:07:22,770 We can now take off the measurement and try it again. 87 00:07:29,000 --> 00:07:30,940 Great, no errors. 88 00:07:30,940 --> 00:07:34,050 But we don't have any way to retrieve the ingredients yet. 89 00:07:34,050 --> 00:07:35,360 We need to add a new method.