Feature Request #4626

Add Text::readable_list() method

Added by Mark Locker almost 2 years ago. Updated 10 months ago.

Status:FeedbackStart date:10/24/2012
Priority:NormalDue date:
Assignee:Lorenzo Pisani% Done:

100%

Category:Core
Target version:v3.4.0
Resolution:fixed Points:1

Description

This is a method I added to a subclass of the Text class in my own project, but I think it'd be worth adding it to the core file. The Text class is currently host to a number of string formatting methods, and I think this would compliment them nicely.

It basically takes an array of strings or integers and converts them into a human readable list. For example: array('eggs', 'milk', 'cheese') => "eggs, milk and cheese".

I've inadvertently added the change to the following pull request: https://github.com/kohana/core/pull/312

Let met know what I need to do if I've messed things up with that. I don't know whether these things should be separate? It didn't seem appropriate to create a new feature branch.

Associated revisions

Revision 29e607db
Added by Mark Locker almost 2 years ago

refs #4626: Added readable_list() method to Text class.

Revision 36188ea9
Added by Lorenzo Pisani over 1 year ago

Merge pull request #328 from marklocker/3.4/feature/4626-readable-list-method

refs #4626: Added readable_list() method to Text class.

Revision 53b03b2b
Added by Lorenzo Pisani about 1 year ago

Merge pull request #380 from marklocker/patch-1

Improved examples in DocBlock. (refs #4626)

History

#1 Updated by Robert-Jan Dreu -rjd22- almost 2 years ago

I think it should support translations. I would put the word "and" in a public static so we can translate that one really easy. Like an dutch example: eieren, melk en kaas

#2 Updated by Mark Locker almost 2 years ago

Robert-Jan Dreu rjd22 wrote:

I think it should support translations. I would put the word "and" in a public static so we can translate that one really easy. Like an dutch example: eieren, melk en kaas

When you say a "public static", what are you referring to exactly? I'm struggling to understand how you see it being translated.

#3 Updated by Robert-Jan Dreu -rjd22- almost 2 years ago

On line #719
Replace `$string .= $word.' and ';` with `$string .= $word.' '.Text::$separator_term.' ';`

On line #719
Add `public static $separator_term = 'and';`

#4 Updated by Mark Locker almost 2 years ago

Robert-Jan Dreu rjd22 wrote:

On line #719
Replace `$string .= $word.' and ';` with `$string .= $word.' '.Text::$separator_term.' ';`

On line #719
Add `public static $separator_term = 'and';`

So for someone to update the separator term to their language, they'd have to subclass the Text class and overwrite that property?

#5 Updated by Jeremy Bush almost 2 years ago

  • Target version changed from 3.3.1 to v3.4.0

This definitely needs to be taken out of that pull request.

#6 Updated by Robert-Jan Dreu -rjd22- almost 2 years ago

Mark Locker wrote:

So for someone to update the separator term to their language, they'd have to subclass the Text class and overwrite that property?

Yep, That is how you do it with the other functions too afaik.

#7 Updated by Robert-Jan Dreu -rjd22- almost 2 years ago

Btw you should split off "Miscellaneous spelling corrections." in a different pull request.

#8 Updated by Mark Locker almost 2 years ago

Robert-Jan Dreu rjd22 wrote:

Btw you should split off "Miscellaneous spelling corrections." in a different pull request.

Yeah, as noted in my pull request I thought this would be wrong. How can I create a separate pull request for the same branch though? Or can't I?

#9 Updated by Mark Locker almost 2 years ago

Any advice on the above? What do I need to do now to separate these?

#10 Updated by Jeremy Bush almost 2 years ago

You would need to create a separate branch. They should be different issue #'s anyway.

#11 Updated by Mark Locker almost 2 years ago

I've just made a new pull request for this, which includes Robert's suggestion: https://github.com/kohana/core/pull/328

#12 Updated by Mark Locker almost 2 years ago

I've just made a new pull request for this, which includes Robert's suggestion: https://github.com/kohana/core/pull/328

#13 Updated by David Pommer almost 2 years ago

A class wide conjunction? Why not use a param?

/**
 * Format an array to coordinating conjunctions
 *
 *     // Display: apples, bananas and oranges
 *     echo Text::enumerate(array('apples', 'bananas', 'oranges'));
 *
 * @param   array   items to format
 * @param   string  conjunction, e.g. 'and', 'or'
 * @return  string
 * @since   1.0
 */
public static function enumerate(array $array = NULL, $conjunction = 'and')
{
    // Nothing to enumerate
    if (empty($array))
    {
        return '';
    }

    if (count($array) > 1)
    {
        $last = array_pop($array);
    }

    $text = implode(', ', $array);

    if (isset($last))
    {
        $text .= ' '.$conjunction.' '.$last;
    }

    return $text;
}

An addition could be if(Text::$translate_conjunction === TRUE) { $conjunction = __($conjunction); }

#14 Updated by Lorenzo Pisani over 1 year ago

  • Status changed from New to Assigned
  • Assignee set to Lorenzo Pisani

#15 Updated by Lorenzo Pisani over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100
  • Resolution set to fixed

#16 Updated by Moshe Katz over 1 year ago

  • Status changed from Closed to Feedback

(I wasn't really sure whether this should re-open the same issue or open a new one...)

The documentation of this new method does not reflect the method's default use of the serial, or "Oxford", comma. The method defaults to using this comma but the example given in the documentation directly above it does not.

/**
 * Turns an array of strings/ints into a readable, comma separated list.
 *
 * For example: array('eggs', 'milk', 'cheese') => "eggs, milk and cheese".
 *
 * @throws  InvalidArgumentException
 * @param   array   $words         An array of words.
 * @param   string  $conjunction   The conjunction term used (e.g. 'and', 'or' etc.).
 * @param   bool    $serial_comma  Whether a serial comma should be used.
 * @return  string                 An inline, human readable list.
 */
public static function readable_list(array $words, $conjunction = 'and', $serial_comma = TRUE)
...

#17 Updated by Mark Locker about 1 year ago

Good spot Moshe, I've created a pull request for this.

#18 Updated by Mark Locker 10 months ago

Can the status of this be set to closed now?

Also available in: Atom PDF