در حال بارگذاری ...

Introducing Stringable0:00

Laravel 7 ships with a useful Stringable class, which provides an object-oriented API of sorts for interacting with your strings. Now you can access it through the usual String helper class, and I'm sure you've reached for this at some point or another, if you need to title case something, or get the singular form of something, or the kebab case, of course you would do something like String::kebab. Right? Okay. Now you'll find a new method called of that returns an instance of that Stringable class. So have a look. Let me show you.

Using Str::of Chaining0:33

So have a look. Let me show you. If I were to say String of Hello World, and we evaluate this, now we have an instance of Stringable, and at any time if we convert it to a String, we'll get the evaluated String there. Alright, so now we can interact with this in a more object-oriented way. For example, if I want to convert this to title_case, now I can. Maybe I want to start it with begin in a space. Maybe once we're done, I want to convert this into a slug, but we're going to do an underscore for the separator.

Formatting Event Names1:00

Maybe once we're done, I want to convert this into a slug, but we're going to do an underscore for the separator. Or maybe two underscores. You get the idea. Alright, let's review a couple examples. Let's say you have an eventName, something like createdPost, and you want to convert this to a friendly label, like createdPost. Alright, these are things you often have to do. Alright, well in the past, what would you do? You might do something like str_replace, look for an underscore, replace it with a

Alright, well in the past, what would you do? You might do something like str_replace, look for an underscore, replace it with a space using the event as a base, and if we give that a run, yeah, but now we need to capitalize it, so you may wrap that within ucwords. And yeah, we've only done two small tweaks here, and already we can see, well, if we have to do a little more manipulation, it gets annoying, right? Okay, so instead, what we might do is Str of the event name. I'll echo this out. Alright. Why don't we now slugify this, give it a run, and it's going to use a dash there.

Alright. Why don't we now slugify this, give it a run, and it's going to use a dash there. However, we could also say use a space as a separator, and then we will titleCase it. Done. How about another one? Maybe you have an invoice that is formatted with inv_, and then maybe the ID of the invoice. And for whatever reason, you want to strip off inv, maybe for performing a query or something like that. Alright.

Stripping Invoice Prefix2:22

like that. Alright. So in the past, again, you might do something like this, where you look for this, replace it with nothing, using the Invoice as a base, and if we echo that out, yeah, that would work. Once again, and there's no problem with that, of course, but if you'd like, you could just wrap it within the Stringable class, and then grab everything after that sequence of characters. And now if I echo that out, we're good to go. Alright. One more.

Generating Files From Stubs2:47

Alright. One more. Maybe you're creating like a file generator, and you have a stub here, that would be something like this. So let's see. Maybe you have a class name, but we're going to replace the name of the class. So you have a little template syntax here. You get the idea. Alright. Next, let's say the class name, maybe from a command line argument or the request, will

Alright. Next, let's say the class name, maybe from a command line argument or the request, will just be plain old User. And we want to swap out this section here with User. Alright, so again, you might say, we're going to look for this syntax here, we're going to replace it with the class name itself, using the stub, and if I echo that out, sure enough, that works. But then we realize, oh, we have to add php to the top, right? Maybe you need to do that dynamically. Alright.

Maybe you need to do that dynamically. Alright. Well, now we can say class equals php, and then the class itself. And now we basically have it, excluding some formatting there. Okay. Instead, let's do this. Let's say $string of the $stub, we're going to echo out those contents, alright, all we have is that. Now, for standard replacements, like preg_replace, you could still do this. Run it again.

Now, for standard replacements, like preg_replace, you could still do this. Run it again. Now we have our User. And the only remaining step is we have to start the class with php. So I can say, I can do prepend or start. And let's do this. php and then two new lines. We run it, and there we go. Alright, I think you get the basic idea. So have fun with object-oriented string manipulation in Laravel 7.

Alright, I think you get the basic idea. So have fun with object-oriented string manipulation in Laravel 7.

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