7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.4 Tying It All Together

Now that we've looked at how to filter out invalid records, let's look at the other side of the code: initialization of the iterators.

3.4 Tying It All Together

Now that we've looked at the call code for filtering out invalid records, now let's look at the other side of the code, the side which initializes the iterators, binding them along with all the parts together. I've done this in a class called src/Episodes/Adapter/EpisodeListerFilesys- tem.php. This class has three member variables. These are postDirectory, which source the directory containing the data files, fileparser which is a class to retrieve information from the filtered files, and finally episode iterator which will contain the list of hydrated episode objects. Now let's work through the constructor. This will take two arguments. A string for the post directory and an object to initialize the file parser. I'll start by initializing the respective member variables from the constructor arguments. That'll instantiate the episode iterative member variable. To a new active episode filter iterator object. This iterator is passed a directory iterator object which is set to scan the post directory. Now let's see how to get a list of the markdown files and instantiate a list of episode objects from it. I'll do that by defining a method called buildEpisodesList and start by defining a new array called episodeListing. Then, using foreach loop, we'll iterate over the list of episode files passing the file object to the buildepisode method which returns an instantiated episode object. The return result is the episode listing object. Now let's look at instantiating episode objects. Instantiating episodes isn't strictly part of looking at iterators, but in the context of the application we're creating it makes sense not to skip over it. So I'll define a new method called build episode. Which takes one perimeter, which is an SDL file infer object, called file. The method we'll start by retrieving the raw content of the file by passing the result of calling files get passed their method, which retrieves the full parts to the file to PHP's file get content method. This initializes a new variable called viral content which contains the contents of the file with no formatting or changes. We then parse file content to the parse method of our fileParser, which creates, in turn, a front YAML object. We next parse the result of parsing document to the getEpisodeData method, which we'll see next as the sole argument to the episode object's constructor. Now, let's look at the getEpisodeData method. Honestly, there's not much to it, it's a simple case of object hydration. What we do is for every property other than content, check if the property is set in the YAML front matter. If it is, we then set the property to the value retrieved from the YAML front matter. For content, we call the GetContent method, which retrieves the markdown body. Or content from the file. Now let's see how to retrieve upcoming episodes. Simpler than the previous example, I'll start by defining the getUpcomingEpisodes method. There as before, I'll instantiate an array to store the retrieved episodes. Next, I'll instantiate a new object called UpcomingEpisodeIterator which is an upcoming episode filter iterator object. This is instantiated by passing in a list of episodes we instantiated before. All that's left to do is iterate over it, install the valid records. As before, all our logic is contained in the iterator class itself. None of it needs to be stored elsewhere, in places where it doesn't need to be, where it would pollute concerns, make code more opaque. Or harder to maintain. The accept method does all the work required to ensure only valid records are returned. Retrieving past episodes is a little different, but not much. As you can assume by now, the only difference is that the past episode filter iterator is used instead of the upcoming episode filter iterator. Okay, just one last method to go, getLatestEpisode. The way this one works is a little convoluted. But it shows how to use a Limititerator. First, I’ll initialize a new Limititerator called iterator by passing in a PastEpisodeFilterIterator as a first parameter, which itself is initialized by an ArrayIterator, whose data is the current EpisodesList, retrieved by calling the buildEpisodesList method. The second and third parameters are the record to start from and the total number of records to make available. So as I only want one, I'll pass in 0 as the record to start from and 1 to filter out all but the first record. With the limit iterator initialized, I'll then call a rewind on it to go to the first record. Then return the result of calling the current method, assuming there is at least one record available, this will return an initialized episode object.

Back to the top