Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / I18n / View / Helper / Plural.php
blob4bff0f24c290f4300b6b59c948ea4fb41a2849d2
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\I18n\View\Helper;
12 use Zend\I18n\Exception;
13 use Zend\I18n\Translator\Plural\Rule as PluralRule;
14 use Zend\View\Helper\AbstractHelper;
16 /**
17 * Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation
18 * is not needed).
20 * Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to
21 * let the developer define the rules himself, instead of potentially break applications if we change rules in the
22 * future.
24 * However, you can find most of the up-to-date plural rules for most languages in those links:
25 * - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
26 * - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
28 class Plural extends AbstractHelper
30 /**
31 * Plural rule to use
33 * @var PluralRule
35 protected $rule;
37 /**
38 * @throws Exception\ExtensionNotLoadedException if ext/intl is not present
40 public function __construct()
42 if (!extension_loaded('intl')) {
43 throw new Exception\ExtensionNotLoadedException(sprintf(
44 '%s component requires the intl PHP extension',
45 __NAMESPACE__
46 ));
50 /**
51 * Given an array of strings, a number and, if wanted, an optional locale (the default one is used
52 * otherwise), this picks the right string according to plural rules of the locale
54 * @param array|string $strings
55 * @param int $number
56 * @throws Exception\InvalidArgumentException
57 * @return string
59 public function __invoke($strings, $number)
61 if (null === $this->getPluralRule()) {
62 throw new Exception\InvalidArgumentException(sprintf(
63 'No plural rule was set'
64 ));
67 if (!is_array($strings)) {
68 $strings = (array) $strings;
71 $pluralIndex = $this->getPluralRule()->evaluate($number);
73 return $strings[$pluralIndex];
76 /**
77 * Set the plural rule to use
79 * @param PluralRule|string $pluralRule
80 * @return Plural
82 public function setPluralRule($pluralRule)
84 if (!$pluralRule instanceof PluralRule) {
85 $pluralRule = PluralRule::fromString($pluralRule);
88 $this->rule = $pluralRule;
90 return $this;
93 /**
94 * Get the plural rule to use
96 * @return PluralRule
98 public function getPluralRule()
100 return $this->rule;