-
-
Save crishoj/dc8d26edb88f59119b131505bd94949d to your computer and use it in GitHub Desktop.
Simple implementation of Rails' .to_sentence for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (! function_exists('to_sentence')) { | |
function to_sentence(array $parts, $connector = ', ', $finally = ' and '): string | |
{ | |
switch (count($parts)) { | |
case 0: | |
return ''; | |
case 1: | |
return reset($parts); | |
case 2: | |
return reset($parts) . $finally . end($parts); | |
default: | |
return implode($connector, array_slice($parts, 0, -1)) . $finally . end($parts); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment