System.I18N.core
[ class tree: System.I18N.core ] [ index: System.I18N.core ] [ all elements ]

Class: ChoiceFormat

Source Location: /I18N/core/ChoiceFormat.php

Class Overview


ChoiceFormat class.

Class Details

[line 56]
ChoiceFormat class.

ChoiceFormat converts between ranges of numeric values and string names for those ranges.

A ChoiceFormat splits the real number line -Inf to +Inf into two or more contiguous ranges. Each range is mapped to a string. ChoiceFormat is generally used in a MessageFormat for displaying grammatically correct plurals such as "There are 2 files."

  1. $string = '[0] are no files |[1] is one file |(1,Inf] are {number} files';
  2.  
  3. $formatter = new MessageFormat(...); //init for a source
  4. $translated = $formatter->format($string);
  5.  
  6. $choice = new ChoiceFormat();
  7. echo $choice->format($translated, 0);

The message/string choices are separated by the pipe "|" followed by a set notation of the form

  • <t>[1,2]</t> -- accepts values between 1 and 2, inclusive.
  • <t>(1,2)</t> -- accepts values between 1 and 2, excluding 1 and 2.
  • <t>{1,2,3,4}</t> -- only values defined in the set are accepted.
  • <t>[-Inf,0)</t> -- accepts value greater or equal to negative infinity and strictly less than 0
Any non-empty combinations of the delimiters of square and round brackets are acceptable.




version:  v1.0, last update on Fri Dec 24 20:46:16 EST 2004
author:  Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>


[ Top ]

Variables

Methods



Class Variables

$inf =

[line 74]

The value for positive infinity.



access:  protected

Type:   float


[ Top ]

$parse =  '/\s?\|?([\(\[\{]([-Inf\d]+,?[\+Inf\d]*)+[\)\]\}])\s?/'

[line 68]

The pattern to parse the formatting string.



access:  protected

Type:   string


[ Top ]

$validate =  '/[\(\[\{]|[-Inf\d]+|,|[\+Inf\d]+|[\)\]\}]/ms'

[line 62]

The pattern to validate a set notation



access:  protected

Type:   string


[ Top ]



Class Methods


constructor __construct [line 80]

ChoiceFormat __construct( )

Constructor.



[ Top ]

method format [line 173]

string format( string $string, float $number)

For the choice string, and a number, find and return the string that satisfied the set within the choices.



return:  the choosen string.
access:  public


Parameters:

string   $string   the choices string.
float   $number   the number to test.

[ Top ]

method isValid [line 92]

boolean isValid( float $number, string $set)

Determine if the given number belongs to a given set



return:  true if number is in the set, false otherwise.


Parameters:

float   $number   the number to test.
string   $set   the set, in set notation.

[ Top ]

method parse [line 148]

array parse( string $string)

Parse a choice string and get a list of sets and a list of strings corresponding to the sets.



return:  array($sets, $strings)


Parameters:

string   $string   the string containing the choices

[ Top ]


© 2004-2005 PRADO Software Group. All Rights Reserved.