Punic v3.3.0
  • Namespace
  • Class
  • Tree
  • Todo
  • Deprecated

Namespaces

  • Punic
    • Exception

Classes

  • Punic\Calendar
  • Punic\Comparer
  • Punic\Currency
  • Punic\Data
  • Punic\Language
  • Punic\Misc
  • Punic\Number
  • Punic\Phone
  • Punic\Plural
  • Punic\Territory
  • Punic\Unit

Exceptions

  • Punic\Exception
  • Punic\Exception\BadArgumentType
  • Punic\Exception\BadDataFileContents
  • Punic\Exception\DataFileNotFound
  • Punic\Exception\DataFileNotReadable
  • Punic\Exception\DataFolderNotFound
  • Punic\Exception\InvalidDataFile
  • Punic\Exception\InvalidLocale
  • Punic\Exception\InvalidOverride
  • Punic\Exception\NotImplemented
  • Punic\Exception\ValueNotInList
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 
<?php

namespace Punic;

/**
 * Numbers helpers.
 */
class Number
{
    /**
     * Check if a variable contains a valid number for the specified locale.
     *
     * @param string $value The string value to check
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return bool
     */
    public static function isNumeric($value, $locale = '')
    {
        return static::unformat($value, $locale) !== null;
    }

    /**
     * Check if a variable contains a valid integer number for the specified locale.
     *
     * @param string $value The string value to check
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return bool
     */
    public static function isInteger($value, $locale = '')
    {
        $result = false;
        $number = static::unformat($value, $locale);
        if (is_int($number)) {
            $result = true;
        } elseif (is_float($number)) {
            if ($number === (float) round($number)) {
                $result = true;
            }
        }

        return $result;
    }

    /**
     * Localize a number representation (for instance, converts 1234.5 to '1,234.5' in case of English and to '1.234,5' in case of Italian).
     *
     * @param int|float|string $value The string value to convert
     * @param int|null $precision The wanted precision (well use {@link http://php.net/manual/function.round.php})
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return string Returns an empty string $value is not a number, otherwise returns the localized representation of the number
     */
    public static function format($value, $precision = null, $locale = '')
    {
        $result = '';
        $number = null;
        if (is_numeric($value)) {
            if (is_string($value) && $precision === null) {
                $precision = self::getPrecision($value);
            }
            $number = (float) $value;
        }
        if ($number !== null) {
            $data = Data::get('numbers', $locale);
            if ($value < 0) {
                $sign = $data['symbols']['minusSign'];
                $number = abs($number);
            } else {
                $sign = '';
            }
            if (is_nan($number)) {
                $result = $data['symbols']['nan'];
            } elseif (is_infinite($number)) {
                $result = $sign.$data['symbols']['infinity'];
            } else {
                $precision = is_numeric($precision) ? (int) $precision : null;
                if ($precision !== null) {
                    $number = round($number, $precision);
                }
                $decimal = $data['symbols']['decimal'];
                $groupLength = (isset($data['groupLength']) && is_numeric($data['groupLength'])) ? (int) $data['groupLength'] : 3;
                $full = explode('.', (string) $number, 2);
                $intPart = $full[0];
                $floatPath = count($full) > 1 ? $full[1] : '';
                $len = strlen($intPart);
                if (($groupLength > 0) && ($len > $groupLength)) {
                    $groupSign = $data['symbols']['group'];
                    $preLength = 1 + (($len - 1) % 3);
                    $pre = substr($intPart, 0, $preLength);
                    $intPart = $pre.$groupSign.implode($groupSign, str_split(substr($intPart, $preLength), $groupLength));
                }
                $result = $sign.$intPart;
                if ($precision === null) {
                    if ($floatPath !== '') {
                        $result .= $decimal.$floatPath;
                    }
                } elseif ($precision > 0) {
                    $result .= $decimal.substr(str_pad($floatPath, $precision, '0', STR_PAD_RIGHT), 0, $precision);
                }
            }
        }

        return $result;
    }

    /**
     * Localize a percentage (for instance, converts 12.345 to '1,234.5%' in case of English and to '1.234,5 %' in case of Danish).
     *
     * @param int|float|string $value The string value to convert
     * @param int|null $precision The wanted precision (well use {@link http://php.net/manual/function.round.php})
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return string Returns an empty string $value is not a number, otherwise returns the localized representation of the percentage
     */
    public static function formatPercent($value, $precision = null, $locale = '')
    {
        $result = '';
        if (is_numeric($value)) {
            $data = Data::get('numbers', $locale);

            if ($precision === null) {
                $precision = self::getPrecision($value);
            }
            $formatted = self::format(100 * abs($value), $precision, $locale);

            $format = $data['percentFormats']['standard'][$value >= 0 ? 'positive' : 'negative'];
            $sign = $data['symbols']['percentSign'];

            $result = sprintf($format, $formatted, $sign);
        }

        return $result;
    }

    /**
     * Localize a currency amount (for instance, converts 12.345 to '1,234.5%' in case of English and to '1.234,5 %' in case of Danish).
     *
     * @param int|float|string $value The string value to convert
     * @param string $currencyCode The 3-letter currency code
     * @param string $kind The currency variant, either "standard" or "accounting"
     * @param int|null $precision The wanted precision (well use {@link http://php.net/manual/function.round.php})
     * @param string $which The currency symbol to use, "" for default, "long" for the currency name, "narrow", "alt" for alternative, or "code" for the 3-letter currency code
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return string Returns an empty string $value is not a number, otherwise returns the localized representation of the amount
     */
    public static function formatCurrency($value, $currencyCode, $kind = 'standard', $precision = null, $which = '', $locale = '')
    {
        $result = '';
        if (is_numeric($value)) {
            $data = Data::get('numbers', $locale);
            $data = $data['currencyFormats'];

            if ($precision === null) {
                $currencyData = Data::getGeneric('currencyData');
                if (isset($currencyData['fractions'][$currencyCode]['digits'])) {
                    $precision = $currencyData['fractions'][$currencyCode]['digits'];
                } else {
                    $precision = $currencyData['fractionsDefault']['digits'];
                }
            }
            $formatted = self::format(abs($value), $precision, $locale);

            if (!isset($data[$kind])) {
                throw new Exception\ValueNotInList($kind, array_keys($data));
            }
            $format = $data[$kind][$value >= 0 ? 'positive' : 'negative'];

            $symbol = null;
            switch ($which) {
                case 'long':
                    $value = number_format($value, $precision, '.', '');
                    $symbol = Currency::getName($currencyCode, $value, $locale);
                    break;
                case 'code':
                    $symbol = $currencyCode;
                    break;
                default:
                    $symbol = Currency::getSymbol($currencyCode, $which, $locale);
                    break;
            }
            if (!$symbol) {
                $symbol = $currencyCode;
            }

            if ($which === 'long') {
                $pluralRule = 'count-'.Plural::getRuleOfType($value, Plural::RULETYPE_CARDINAL, $locale);
                if (!isset($data['unitPattern'][$pluralRule])) {
                    $pluralRule = 'count-other';
                }
                $unitPattern = $data['unitPattern'][$pluralRule];

                $result = sprintf($unitPattern, $formatted, $symbol);
            } else {
                list($before, $after) = explode('%2$s', $format);
                if ($after &&
                    preg_match($data['currencySpacing']['afterCurrency']['currency'], $symbol) &&
                    preg_match($data['currencySpacing']['afterCurrency']['surrounding'], sprintf($after, $formatted))) {
                    $symbol .= $data['currencySpacing']['afterCurrency']['insertBetween'];
                }
                if ($before &&
                    preg_match($data['currencySpacing']['beforeCurrency']['currency'], $symbol) &&
                    preg_match($data['currencySpacing']['beforeCurrency']['surrounding'], sprintf($before, $formatted))) {
                    $symbol = $data['currencySpacing']['beforeCurrency']['insertBetween'].$symbol;
                }

                $result = sprintf($format, $formatted, $symbol);
            }
        }

        return $result;
    }

    /**
     * Convert a localized representation of a number to a number (for instance, converts the string '1,234' to 1234 in case of English and to 1.234 in case of Italian).
     *
     * @param string $value The string value to convert
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return int|float|null Returns null if $value is not valid, the numeric value otherwise
     */
    public static function unformat($value, $locale = '')
    {
        $result = null;
        if (is_int($value) || is_float($value)) {
            $result = $value;
        } elseif (is_string($value) && $value !== '') {
            $data = Data::get('numbers', $locale);
            $plus = $data['symbols']['plusSign'];
            $plusQ = preg_quote($plus);
            $minus = $data['symbols']['minusSign'];
            $minusQ = preg_quote($minus);
            $decimal = $data['symbols']['decimal'];
            $decimalQ = preg_quote($decimal);
            $group = $data['symbols']['group'];
            $groupQ = preg_quote($group);
            $ok = true;
            if (preg_match('/^'."($plusQ|$minusQ)?(\\d+(?:$groupQ\\d+)*)".'$/', $value, $m)) {
                $sign = $m[1];
                $int = $m[2];
                $float = null;
            } elseif (preg_match('/^'."($plusQ|$minusQ)?(\\d+(?:$groupQ\\d+)*)$decimalQ".'$/', $value, $m)) {
                $sign = $m[1];
                $int = $m[2];
                $float = '';
            } elseif (preg_match('/^'."($plusQ|$minusQ)?(\\d+(?:$groupQ\\d+)*)$decimalQ(\\d+)".'$/', $value, $m)) {
                $sign = $m[1];
                $int = $m[2];
                $float = $m[3];
            } elseif (preg_match('/^'."($plusQ|$minusQ)?$decimalQ(\\d+)".'$/', $value, $m)) {
                $sign = $m[1];
                $int = '0';
                $float = $m[2];
            } else {
                $ok = false;
                $float = $int = $sign = null;
            }
            if ($ok) {
                if ($sign === $minus) {
                    $sign = '-';
                } else {
                    $sign = '';
                }
                $int = str_replace($group, '', $int);
                if ($float === null) {
                    $result = (int) "$sign$int";
                } else {
                    $result = (float) "$sign$int.$float";
                }
            }
        }

        return $result;
    }

    /**
     * Spell out a number (e.g. "one hundred twenty-three" or "twenty-third") or convert to a different numbering system, e.g Roman numerals.
     *
     * Some types are language-dependent and reflect e.g. gender and case. Refer to the CLDR XML source for supported types.
     *
     * Available numbering systems are specified in the "root" locale.
     *
     * @param int|float|string $value The value to localize/spell out
     * @param string $type The format type, e.g. "spellout-numbering", "spellout-numbering-year", "spellout-cardinal", "digits-ordinal", "roman-upper".
     * @param string $locale The locale to use. If empty we'll use the default locale set in \Punic\Data
     *
     * @return string The spelled number
     *
     * @see https://www.unicode.org/repos/cldr/trunk/common/rbnf/
     * @see https://www.unicode.org/repos/cldr/trunk/common/rbnf/root.xml
     */
    public static function spellOut($value, $type, $locale)
    {
        return self::formatRbnf($value, $type, null, $locale);
    }

    /**
     * This method should not be called from outside this class.
     *
     * It is declared public for compatibility with PHP 5.3.
     *
     * @param int|float|string $value
     * @param string $type
     * @param int $base
     * @param string $locale
     *
     * @internal
     */
    public static function formatRbnf($value, $type, $base, $locale)
    {
        $data = Data::get('rbnf', $locale);
        if (!isset($data[$type])) {
            $data += Data::get('rbnf', 'root', true);
        }
        if (!isset($data[$type])) {
            throw new Exception\ValueNotInList($type, array_keys($data));
        }
        $data = $data[$type];

        list($rule, $left, $right, $prevBase) = self::getRbnfRule($value, $data, $base);

        $rule = preg_replace_callback('/([<>=])(.*?)\1\1?|\$\((.*?),(.*?)\)\$/', function ($match) use ($value, $left, $right, $type, $prevBase, $locale) {
            if (isset($match[4])) {
                $rule = Plural::getRuleOfType($left, $match[3] ? $match[3] : Plural::RULETYPE_CARDINAL, $locale);
                if (preg_match('/'.$rule.'{(.*?)}/', $match[4], $match2)) {
                    return $match2[1];
                }
            } else {
                $base = null;
                if ($match[2]) {
                    if ($match[2][0] !== '%') {
                        $i = strpos($match[2], '.');
                        if ($i === false) {
                            $precision = 0;
                        } elseif ($match[2][$i + 1] === '#') {
                            $precision = null;
                        } else {
                            $precision = strspn($match[2], '0', $i + 1);
                        }

                        return Number::format($value, $precision, $locale);
                    }
                    $type = substr($match[2], 1);
                }

                switch ($match[1]) {
                    case '=':
                        break;
                    case '<':
                        $value = $left;
                        break;
                    case '>':
                        $value = $right;
                        if ($match[0] == '>>>') {
                            $base = $prevBase;
                        }
                        break;
                }

                return implode(' ', array_map(function ($v) use ($type, $base, $locale) {
                    return Number::formatRbnf($v, $type, $base, $locale);
                }, (array) $value));
            }
        }, $rule);

        return $rule;
    }

    protected static function getRbnfRule($value, $data, $base = null)
    {
        $left = 0;
        $right = 0;
        $prevBase = 0;
        if (!is_numeric($value)) {
            $rule = '';
        } elseif (is_nan($value) && isset($data['NaN'])) {
            $rule = $data['NaN']['rule'];
        } elseif ($value < 0) {
            $right = -$value;
            $rule = $data['-x']['rule'];
        } elseif (is_infinite($value) && isset($data['Inf'])) {
            $rule = $data['Inf']['rule'];
        } elseif (strpos($value, '.') !== false && isset($data['x.x'])) {
            list($left, $right) = explode('.', $value);
            $right = str_split($right);
            if ($left == 0 && isset($data['0.x'])) {
                $rule = $data['0.x']['rule'];
            } else {
                $rule = $data['x.x']['rule'];
            }
        } else {
            $bases = array_keys($data['integer']);
            if ($base) {
                $i = array_search($base, $bases);
            } else {
                for ($i = count($bases) - 1; $i >= 0; --$i) {
                    $base = $bases[$i];
                    if ($base <= $value) {
                        break;
                    }
                }
            }
            $prevBase = $i > 0 ? $bases[$i - 1] : null;

            $r = $data['integer'][$base] + array('radix' => 10);
            $rule = $r['rule'];
            $radix = $r['radix'];

            // Add .5 to avoid floating-point rounding error in PHP 5. $base is
            // an integer, so adding a number < 1 will not break anything.
            $divisor = pow($radix, floor(abs(log($base + .5, $radix))));

            $right = fmod($value, $divisor);
            $left = floor($value / $divisor);

            if ($right) {
                $rule = str_replace(array('[', ']'), '', $rule);
            } else {
                $rule = preg_replace('/\[.*?\]/', '', $rule);
            }
        }

        return array($rule, $left, $right, $prevBase);
    }

    private static function getPrecision($value)
    {
        $precision = null;
        if (is_string($value)) {
            $i = strrpos($value, '.');
            if ($i !== false) {
                $precision = strlen($value) - $i - 1;
            }
        }

        return $precision;
    }
}
Punic v3.3.0 API documentation generated by ApiGen