تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Defining Line Object0:00

Now, at the end of the previous episode, we discussed introducing a new Line object. So why don't we play around with that idea now? Call it Line, and now this class will be responsible for anything related to a single line. But actually, we should be careful. If I come back to our example, I don't mean a line like this is a line, and that's a line, and that's a line, and that's a line. Really, the pairing here is what makes it a line. This is the text for the line, and this is the timestamp where that line occurs. So that's something we need to think about, but we'll get there in a few. Now if I come back to transcription, yeah, even notice this line that we were tinkering

Moving Line Validation0:36

So that's something we need to think about, but we'll get there in a few. Now if I come back to transcription, yeah, even notice this line that we were tinkering with in the last episode. Right here determines if a particular line in that file is considered valid. And actually, on that note, I like valid better than irrelevant. So why don't we rename this to DiscardInvalidLines. But now think about it. This probably should live here. So why don't we add a method called valid, and that will contain this check. So right now, I'm not sure how we're going to do this, so I'm going to start by making

So why don't we add a method called valid, and that will contain this check. So right now, I'm not sure how we're going to do this, so I'm going to start by making this static, where it just accepts a given line. And then later, maybe we'll look on the line instance, but just to get us going and to show you how this might work. Now logic related to a line should live on the Line class, not the Transcription class. So if I switch back, this can then be replaced with line::valid, and we pass it through. And if we did everything correctly, everything should still pass, and it does. Okay, a little bit of progress there. Next, I'm thinking that right up here, when we fetch our lines, I don't just want an array,

Returning Line Objects1:47

Okay, a little bit of progress there. Next, I'm thinking that right up here, when we fetch our lines, I don't just want an array, I want an array of line objects. So in our case, like we were saying earlier, really, with this change, we're going to have an array of two items. This would be in a line object, and this would be in a line object. So with that in mind, why don't we switch back, it can convert to an array of line objects. Let's work on this a little more. So to start, we want two items now, but of course, it's going to fail because we are returning four.

So to start, we want two items now, but of course, it's going to fail because we are returning four. But then I'm going to add a second test here. There should be a assert. What is it? Yeah, assertContains only instances of Line objects. So if you ever want to say, I expect this array to only consist of Post objects or something like that, this would be a way to do that. And let's see, I'll need my lines. So we will extract that to lines.

And let's see, I'll need my lines. So we will extract that to lines. Yeah. And what's the problem here? Yeah. Okay, so let's give this a run. And yeah, it's going to fail. So if I switch back to my lines method, yeah, it sounds like we're going to do a little bit more work here. Let's try this out.

Pairing Timestamps and Text3:07

bit more work here. Let's try this out. First, we're going to have an array of all the lines. But then I can't just map over it because we basically need to group each of these lines into a pair. So there's probably a fancy Collection method I could use. But without that, maybe a plain old for. For $i is zero, and $i is less than the total number of lines in that array, then $i plus equals two in this case, because we're actually going to be grabbing the current line and then the one that comes after it.

equals two in this case, because we're actually going to be grabbing the current line and then the one that comes after it. So I will instantiate a new Line, that line will accept the current timestamp, which will be the current line, and then the body that comes after it or the text. So that would be lines[i + 1]. Yeah, so real quick, if I had like $results, we could then push to $results and return that. Okay, let's see how that works. Ah, it does pass. Cool. But now notice right here, it's squawking because our Line class doesn't yet accept

Cool. But now notice right here, it's squawking because our Line class doesn't yet accept anything. So let's do that now. We'll have our constructor here. And that's going to, we'll just make it public, that will be your timestamp, and then your the body of the line. Okay, so now that should fix that. And actually, maybe, hmm, can I just do this, reference that directly. And that would allow me to replace results with something a little more accurate, which

And actually, maybe, hmm, can I just do this, reference that directly. And that would allow me to replace results with something a little more accurate, which would be lines. I don't know. Is that okay? Yeah, it is okay. So now we know, when we load a transcription, we start by discarding any of the lines we don't care about. And then when you call this lines method, it's actually going to give you an array of line objects.

And then when you call this lines method, it's actually going to give you an array of line objects. Okay, what else? Discard invalid lines. I think maybe trimming. Hmm. Maybe we should do that elsewhere. So maybe right up here. array, map, trim. Are we still good?

Refactoring Transcription Construction5:23

Array, map, trim. Are we still good? Yes. Okay, so now while we're here, why don't we go ahead and create a dedicated constructor here. This, I can get rid of all of this, is going to be a protected array of the lines. And then our load method is going to return to what it should be, which is just a static constructor, where you pass through the path. But then the constructor function is what actually does some of the work. So let's see.

But then the constructor function is what actually does some of the work. So let's see. Hmm. This, well actually no, the load method should load a file. That is the responsibility. So file, path. And that would give you the lines that you would pass here. But then I think the constructor should be in charge of discarding. So this, discardInvalidLines. And are we at green?

So this, discard invalid lines. And are we at green? No, we're off track. Because we need to trim, which would be here, array, map, trim. There we go. I was going to say, fingers crossed, I hope we're not getting into the weeds too much. And then inline this, and I'll let you take a look at that. Okay, I think that's a little bit cleaner. Now I can instantiate transcription by feeding it an array of the lines for the transcription. Alternatively, I can load a transcription from a given file path.

Now I can instantiate transcription by feeding it an array of the lines for the transcription. Alternatively, I can load a transcription from a given file path. That's better. I like that. And then when I call this lines method, it's going to return to me an array of line objects. So for example, if I were to var_dump the lines here, so you can have a look, yeah, notice we have an array of two items, where each one is a line object that contains the timestamp and the body. So now you can imagine, in my project, I can loop over this array, and for each one, render the necessary paragraph tag.

Extending Line Rendering Logic7:13

So now you can imagine, in my project, I can loop over this array, and for each one, render the necessary <p> tag. And I can wrap it in an <a> tag, where I just simply fetch this particular timestamp. And even further, you can imagine maybe a situation where I don't need this full span, I just need, well, the first timestamp? Well, where would that logic go? That logic would go online. You could maybe have a getter or a method beginningTimestamp. And there, you know, in this case, maybe you're doing a quick preg_match to fetch the beginning.

You could maybe have a getter or a method beginningTimestamp. And there, you know, in this case, maybe you're doing a quick preg_match to fetch the beginningTimestamp, the point being you have a place to put all of that logic now, and that would be here. But let's go back, and let's have a look over everything. What we have is looking a good bit better now. So in the next episode, I'd like to add one more helper method that will translate this whole thing into HTML. And that might be useful. You call a single method, and it prepares a generic HTML document that you can use.

And that might be useful. You call a single method, and it prepares a generic HTML document that you can use.

Object ResponsibilitiesExtract Class

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟