1 00:00:00,640 --> 00:00:03,631 I want to add another requirement to the collection. 2 00:00:03,631 --> 00:00:07,070 The ability to return a short description. 3 00:00:07,070 --> 00:00:11,750 Instead of implementing another interface I'm going to combine all of these 4 00:00:11,750 --> 00:00:14,923 requirements into a single collection interface. 5 00:00:14,923 --> 00:00:19,852 This will allow us to be certain that any class that implements a collection 6 00:00:19,852 --> 00:00:23,924 interface will have all the iterator and countable methods, 7 00:00:23,924 --> 00:00:26,602 as well as the new method we'll define. 8 00:00:26,602 --> 00:00:29,499 Add a new file to the source interface's folder. 9 00:00:29,499 --> 00:00:37,947 We'll name this CollectionInterface.php. 10 00:00:42,454 --> 00:00:47,921 Interface CollectionInterface. 11 00:00:47,921 --> 00:00:51,894 We'll define our new interface by adding the keyword extends. 12 00:00:54,440 --> 00:01:01,251 To extend the Iterator and Countable interfaces. 13 00:01:04,252 --> 00:01:09,053 Note that this is very similar to how classes can extend other classes. 14 00:01:09,053 --> 00:01:12,960 However, we can extend more than one interface. 15 00:01:12,960 --> 00:01:19,706 Now we can add our new method, public function shortDescription. 16 00:01:22,595 --> 00:01:27,433 By extending the iterator and countable interfaces our CollectionInterface 17 00:01:27,433 --> 00:01:32,360 will now include all of the methods defined in those interfaces. 18 00:01:32,360 --> 00:01:37,271 This means that anyone implementing our CollectionInterface will also need to 19 00:01:37,271 --> 00:01:41,973 implement the methods from the Iterator and Countable interfaces as well. 20 00:01:41,973 --> 00:01:44,522 Back in our Collection class. 21 00:01:44,522 --> 00:01:49,649 Let's change this to CollectionInterface. 22 00:01:49,649 --> 00:01:55,540 Now we need to define the short description, 23 00:01:55,540 --> 00:02:00,507 public function shortDescription. 24 00:02:04,893 --> 00:02:08,370 We are going to use the current item details. 25 00:02:08,370 --> 00:02:12,586 First, we're going to check if 26 00:02:12,586 --> 00:02:19,615 the strlen($this->current()->details) 27 00:02:19,615 --> 00:02:23,985 Is less than 510, then we're 28 00:02:23,985 --> 00:02:28,991 going to return the current details. 29 00:02:28,991 --> 00:02:30,674 But we're going to strip the tags. 30 00:02:33,587 --> 00:02:39,435 This current- Details. 31 00:02:42,853 --> 00:02:46,334 If we make it past the early return, 32 00:02:46,334 --> 00:02:51,210 we're going to return our trimmed down version, 33 00:02:51,210 --> 00:02:56,331 return, and again we'll start with strip_tags. 34 00:02:56,331 --> 00:03:00,306 I'm going to add some line spacing to make this easier to read. 35 00:03:00,306 --> 00:03:04,818 Substring this 36 00:03:04,818 --> 00:03:10,373 current details. 37 00:03:10,373 --> 00:03:14,360 We're going to start at our first character, which is 0. 38 00:03:14,360 --> 00:03:21,362 And then find the string position In this current details. 39 00:03:25,801 --> 00:03:30,070 And we're going to look for the first space after 500 characters. 40 00:03:36,073 --> 00:03:38,671 The final touch is to add an ellipses or 41 00:03:38,671 --> 00:03:43,802 triple dots at the end to identify that there's more to this description. 42 00:03:43,802 --> 00:03:45,733 We'll just use dot, dot, dot. 43 00:03:47,142 --> 00:03:51,898 We're finally ready to include our list view with the post which will give us 44 00:03:51,898 --> 00:03:53,323 a sample of each post. 45 00:03:53,323 --> 00:03:54,470 Let's add a new view. 46 00:03:55,480 --> 00:04:00,605 In the views folder, new file list.php. 47 00:04:04,298 --> 00:04:11,773 The first thing I'm going to do is check if the item status is equal to published. 48 00:04:16,787 --> 00:04:19,305 Next we'll create an article block with a title. 49 00:04:35,352 --> 00:04:40,500 We'll add the $item, title, and closer tag. 50 00:04:42,600 --> 00:04:45,080 Finally we can use our short description. 51 00:04:45,080 --> 00:04:49,547 Since the collection handles the short description and 52 00:04:49,547 --> 00:04:52,595 not the item itself, we use content. 53 00:04:52,595 --> 00:05:00,400 So echo $content->shortDescription. 54 00:05:00,400 --> 00:05:01,842 Since we've stripped tags, 55 00:05:01,842 --> 00:05:04,966 let's add a paragraph tag surrounding the short description. 56 00:05:15,591 --> 00:05:19,184 Now we can add this to our index page. 57 00:05:19,184 --> 00:05:24,766 In the for each loop, instead of just the title 58 00:05:24,766 --> 00:05:30,501 we're going to include 'views/list.php. 59 00:05:30,501 --> 00:05:32,140 Let's take a look at this in the browser. 60 00:05:33,290 --> 00:05:35,213 It's starting to look like a blog. 61 00:05:35,213 --> 00:05:40,287 Let's add the ability to read each of these posts in its entirety.