Last active
November 8, 2025 17:24
-
-
Save aklump/84ab03b9f16c4bda4bfb1ab2d91067bb to your computer and use it in GitHub Desktop.
csv using "and"
This file contains hidden or 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 | |
| /** | |
| * A class that formats an array of items into a human-readable string. | |
| * | |
| * When the class is invoked as a callable, it formats the provided array | |
| * by joining all elements with commas and adding "and" before the last item. | |
| * This is typically used for creating natural language representations of lists. | |
| * | |
| * Methods: | |
| * | |
| * - __invoke(array $items): Converts an array into a readable string format. | |
| */ | |
| class HumanList { | |
| public function __invoke(array $items): string { | |
| $last = array_pop($items); | |
| $items_string = implode(', ', $items); | |
| return sprintf('%s and %s', $items_string, $last); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment