3.2 Retrieving All Episode Data
In this lesson, we'll start coding a class to retrieve episode data. I'll show you how this class uses
DirectoryIterator to build on the functionality that they offer.
1.Introduction1 lesson, 00:39
2.Getting Started3 lessons, 12:35
3.Building the Application4 lessons, 12:25
4.Conclusion1 lesson, 00:38
3.2 Retrieving All Episode Data
We'll firstly create a new class called ActiveEpisodeFilterIterator, which in turn extends FilterIterator. And this will be located in src/PodcastSite/Iterator/ActiveEpisodeFil- terIterator.php. The reason for this is that we wanna implement the except method, which is the only required method in FilterIterator. When iterating over a data set, the except method determines the criteria we'll use to determine if a diner item is valid or not. If it is invalid, it won't be returned, and if it is it will be. As I keep saying, said another way, if is valid, then we will be able to iterate over it and if not it won't. Starting with a class constructor, we pass to it a directory iterator, which we set as the iterator which our class will traverse and then we call the rewind method on it. Doing this ensures that we start from the beginning of the list and not some point part way through. There's a bit in what I've just said, to be fair, and did you know that iterators can use iterators. This is really handy, as you can stack a wide array of iterators on top of one another to build up the functionality you need without having to necessarily directly code it yourself. What I've done here is to use the pair of the DirectoryIterator to use a directory as a data source. And then wrapped it in the FilterIterator, to allow me to create custom logic, which filters out any type of file which isn't applicable to our application. Now let's look at the append method in a bit more detail, to see how we do the filtering. Firstly we need to get the current, or active item on the inner iterator, which is our data set. To do that, we call the current method on the response of calling the getInnerIterator method. As we're using a directory iterator, this will return a SVL file infer object which is pre-instantiated for us containing all the information about the current file. It can also be a directory or a sim link. Looking at the definition of SplFileInfo for a moment, you can see that it has a range of methods for working with files, such as isdot, isfile, isreadable and getextension, which we'll be using to determine if the current object is the one that we need. Going back to the code, now that we have the current file let's work through the accept method and see how determines if the current file is valid or not. The accept method returns Boolean, true if the file is valid and false if not. So we'll first check if it's an SBL file for object, in short it means that it was a valid file on file system. Then we'll check that it's the file which we can read. And after that, we'll do a simplistic test using the files extension. We could of skipped right to this, but sometimes I think it's good to be explicit. So first call the getExtension method on the item, to get the file's extension. Then using PHP's in_array method, I'll check if the extension's either .md or .markdown, which are the standard extensions for markdown files. If it's not a return false and if we got past all three of these tests than it's fair to assume that the file is a valid one so we'll return true. And that's it for this video. In the next video, we'll work through class which retrieves the past episode object.