Source of file Plural.php
Size: 2,940 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/I18n/View/Helper/Plural.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\I18n\View\Helper; use Zend\I18n\Exception; use Zend\I18n\Translator\Plural\Rule as PluralRule; use Zend\View\Helper\AbstractHelper; /** * Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation * is not needed). * * Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to * let the developer define the rules himself, instead of potentially break applications if we change rules in the * future. * * However, you can find most of the up-to-date plural rules for most languages in those links: * - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html * - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals */ class Plural extends AbstractHelper { /** * Plural rule to use * * @var PluralRule */ protected $rule; /** * @throws Exception\ExtensionNotLoadedException if ext/intl is not present */ public function __construct() { if (!extension_loaded('intl')) { throw new Exception\ExtensionNotLoadedException(sprintf( '%s component requires the intl PHP extension', __NAMESPACE__ )); } } /** * Given an array of strings, a number and, if wanted, an optional locale (the default one is used * otherwise), this picks the right string according to plural rules of the locale * * @param array|string $strings * @param int $number * @throws Exception\InvalidArgumentException * @return string */ public function __invoke($strings, $number) { if (null === $this->getPluralRule()) { throw new Exception\InvalidArgumentException(sprintf( 'No plural rule was set' )); } if (!is_array($strings)) { $strings = (array) $strings; } $pluralIndex = $this->getPluralRule()->evaluate($number); return $strings[$pluralIndex]; } /** * Set the plural rule to use * * @param PluralRule|string $pluralRule * @return Plural */ public function setPluralRule($pluralRule) { if (!$pluralRule instanceof PluralRule) { $pluralRule = PluralRule::fromString($pluralRule); } $this->rule = $pluralRule; return $this; } /** * Get the plural rule to use * * @return PluralRule */ public function getPluralRule() { return $this->rule; } } |