Base::format PHP Method

format() public method

Return locale-aware formatted string
public format ( ) : string
return string
    function format()
    {
        $args = func_get_args();
        $val = array_shift($args);
        // Get formatting rules
        $conv = localeconv();
        return preg_replace_callback('/\\{(?P<pos>\\d+)\\s*(?:,\\s*(?P<type>\\w+)\\s*' . '(?:,\\s*(?P<mod>(?:\\w+(?:\\s*\\{.+?\\}\\s*,?\\s*)?)*)' . '(?:,\\s*(?P<prop>.+?))?)?)?\\}/', function ($expr) use($args, $conv) {
            extract($expr);
            extract($conv);
            if (!array_key_exists($pos, $args)) {
                return $expr[0];
            }
            if (isset($type)) {
                if (isset($this->hive['FORMATS'][$type])) {
                    return $this->call($this->hive['FORMATS'][$type], [$args[$pos], $mod, isset($prop) ? $prop : null]);
                }
                switch ($type) {
                    case 'plural':
                        preg_match_all('/(?<tag>\\w+)' . '(?:\\s*\\{\\s*(?<data>.+?)\\s*\\})/', $mod, $matches, PREG_SET_ORDER);
                        $ord = ['zero', 'one', 'two'];
                        foreach ($matches as $match) {
                            extract($match);
                            if (isset($ord[$args[$pos]]) && $tag == $ord[$args[$pos]] || $tag == 'other') {
                                return str_replace('#', $args[$pos], $data);
                            }
                        }
                    case 'number':
                        if (isset($mod)) {
                            switch ($mod) {
                                case 'integer':
                                    return number_format($args[$pos], 0, '', $thousands_sep);
                                case 'currency':
                                    $int = $cstm = false;
                                    if (isset($prop) && ($cstm = !($int = $prop == 'int'))) {
                                        $currency_symbol = $prop;
                                    }
                                    if (!$cstm && function_exists('money_format')) {
                                        return money_format('%' . ($int ? 'i' : 'n'), $args[$pos]);
                                    }
                                    $fmt = [0 => '(nc)', 1 => '(n c)', 2 => '(nc)', 10 => '+nc', 11 => '+n c', 12 => '+ nc', 20 => 'nc+', 21 => 'n c+', 22 => 'nc +', 30 => 'n+c', 31 => 'n +c', 32 => 'n+ c', 40 => 'nc+', 41 => 'n c+', 42 => 'nc +', 100 => '(cn)', 101 => '(c n)', 102 => '(cn)', 110 => '+cn', 111 => '+c n', 112 => '+ cn', 120 => 'cn+', 121 => 'c n+', 122 => 'cn +', 130 => '+cn', 131 => '+c n', 132 => '+ cn', 140 => 'c+n', 141 => 'c+ n', 142 => 'c +n'];
                                    if ($args[$pos] < 0) {
                                        $sgn = $negative_sign;
                                        $pre = 'n';
                                    } else {
                                        $sgn = $positive_sign;
                                        $pre = 'p';
                                    }
                                    return str_replace(['+', 'n', 'c'], [$sgn, number_format(abs($args[$pos]), $frac_digits, $decimal_point, $thousands_sep), $int ? $int_curr_symbol : $currency_symbol], $fmt[(int) (${$pre . '_cs_precedes'} % 2 . ${$pre . '_sign_posn'} % 5 . ${$pre . '_sep_by_space'} % 3)]);
                                case 'percent':
                                    return number_format($args[$pos] * 100, 0, $decimal_point, $thousands_sep) . '%';
                            }
                        }
                        return number_format($args[$pos], isset($prop) ? $prop : 2, $decimal_point, $thousands_sep);
                    case 'date':
                        if (empty($mod) || $mod == 'short') {
                            $prop = '%x';
                        } elseif ($mod == 'long') {
                            $prop = '%A, %d %B %Y';
                        }
                        return strftime($prop, $args[$pos]);
                    case 'time':
                        if (empty($mod) || $mod == 'short') {
                            $prop = '%X';
                        }
                        return strftime($prop, $args[$pos]);
                    default:
                        return $expr[0];
                }
            }
            return $args[$pos];
        }, $val);
    }

Usage Example

Esempio n. 1
0
 public function __toString()
 {
     return parent::format($this);
 }
All Usage Examples Of Base::format