- Overview
- Transcript
3.3 Retrieving Past and Upcoming Episodes
Now that we have a list of episodes, let's look at how we can use that episode data to retrieve only the past or future episodes. In this lesson, I'll show you how to iterate over the instantiated list of Episode objects and filter them by publish date. Let's step through it and see how it's done.
1.Introduction1 lesson, 00:39
1.1Introduction00:39
2.Getting Started3 lessons, 12:35
2.1Getting Started04:52
2.2The Sample Application02:29
2.3What Is the SPL?05:14
3.Building the Application4 lessons, 12:25
3.1The Core Classes02:09
3.2Retrieving All Episode Data03:17
3.3Retrieving Past and Upcoming Episodes02:24
3.4Tying It All Together04:35
4.Conclusion1 lesson, 00:38
4.1Conclusion00:38
3.3 Retrieving Past and Upcoming Episodes
Now that we have a list of episodes. Let's look at how to use that episode data to retrieve only the past episodes. Put simply, a past episode is any episode which has a publish date no later than today. Or actually, more specifically, no later than right now. We're going to iterate over the instantiated list of episode objects. And filter out any which have a publish date somewhere in the future. Let's step through it and see how it's done. We first create a new class, called PastEpisodeFilterIterator. Located inside src/PodcastSite/Iterator. As with the previous class, the constructor will receive an iterator, called iterator. Which itself contains a list of instantiated episode objects. This is done, by passing it to the parent constructor. Then calling the rewind method, to insure that we start at the start of a data set. Now let's define the accept method. To filter out future episodes, we'll first retrieve the current item. By calling the current method, on the result of calling the getInnerIterator method. Then we'll instantiate a new DateTime object, called episode. Passing nothing to the constructor. This initializes it using the current system timestamp. Then we'll instantiate a second DateTime object, called today. Using the episode's published date value. Obtained by calling the getPublishDate method on it. Finally, we'll return the result of checking, if episode is earlier than today. If so, it will return true, making the record valid. And if not, it will return false. Making the record invalid, and skipping over it. I took this comparison approach, because you can compare two daytime objects. As you would compare strings, insigns, and so on. It makes it really easy to find out which one has an earlier or later time stamp. And this is a handy, easy form of date comparison. It saves us a lot of effort in coding a date time comparison routine. Now let's look at retrieving upcoming episodes. I want to spend a lot of time on this. As the upcoming episodes accept method. Is almost exactly the same as that of the past episodes. Given that, I've pre-written a class. Which you can find in src/PodcastSite/Iterator/UpcomingEpisodeF- ilterIterator.php. I've cut straight down to the return comparison. Just so that you know I'm not cheating you out of anything. And that's it for this video. In the next video, we'll start tying the code we've written together, and initialize it.







