Playing
14. Spelling ordinals as words

Transcript

00:00
So let's talk about the SpellOrdinal method on the NumberHelper in Laravel, which has very recently been added. What this allows you to do is take an ordinal number, like first, second, third, etc., and basically go ahead and spell it out.
00:15
Now, it might seem at first like there's some complex functionality behind this. At the end of the episode, we'll go ahead and dive into behind the scenes and actually see what's happening in the background. OK, so if you're not familiar with either the NumberHelper within Laravel or the Ordinal method on the NumberHelper, let's go ahead and take a look at this first of all. So let's go ahead and pass one into here and we'll just go ahead and die dump this out.
00:42
This is incredibly helpful because it allows us to basically take a number and convert it into an ordinal number, like first, second, third. So super helpful in itself, but we now have the ability to spell an ordinal number and you've probably already guessed we have a SpellOrdinal method. So instead of first, this will actually spell out the word first. So pretty helpful if you have the need to do that in your project. There's not really much more to say about this.
01:10
I thought it was a really good thing to add to the course because ordinal numbers, there are lots of guides out there of how to build up this functionality. And if you didn't realise that Laravel contained this method, you'd probably go ahead and build your own functionality for it or use the built in PHP functionality for this. Now, talking about the built in PHP functionality for this, let's go ahead and take a look behind the scenes. Now, originally, I'd have thought, well, this functionality seems pretty complex.
01:39
There must be something going on behind the scenes here to have a huge list of these values to map up to the numbers that we pass in. Well, actually, if we just take a look at the implementation of this, ignoring the method just here, this is all just built in PHP stuff. So the number formatter class that you can see here is a built in formatter within PHP. And this is all it takes to do this in pure PHP.
02:03
But I'm sure you'll agree this will be pretty cumbersome to type out every time you need to use it. So it's really nice that this is now built into a helper. So just to demonstrate this, let's go ahead and take this code and just go over it really, really quickly. We don't need to pull the number formatter in here and we can just go ahead and pass in any value here and we should get exactly the same result.
02:24
So let's just go ahead and die dump on this and let's take a look at what's happening here. So you can see that this expects a locale or in this case, any of the default locales that are included in this particular helper class. And you can actually pass the locale into this if you need this changed. Now for us, that's pretty much always going to be something like ENUS or ENGB.
02:46
So we can just go ahead and pass this in. Then we get this spell out feature just here, which literally spells the ordinal out. And we go ahead and pass this into here when we set this text attribute. So that is pretty much all we need to do to get exactly the same result.
03:01
So at first glance sounds pretty complicated, but since this is already built into PHP, it kind of makes sense that that's the way that it works. And if we take a look at the ordinal helper as well, we get pretty much the same thing just without that set text attribute method, which sets the ordinal spell out. OK, there we go. So really nice addition to the number helper. And this is going to make it a lot cleaner and a lot more convenient to spell out ordinal numbers.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!