yii\i18n\Formatter::asOrdinal PHP Метод

asOrdinal() публичный Метод

This function requires the PHP intl extension to be installed.
public asOrdinal ( mixed $value ) : string
$value mixed the value to be formatted
Результат string the formatted result.
    public function asOrdinal($value)
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $value = $this->normalizeNumericValue($value);
        if ($this->_intlLoaded) {
            $f = $this->createNumberFormatter(NumberFormatter::ORDINAL);
            if (($result = $f->format($value)) === false) {
                throw new InvalidParamException('Formatting number as ordinal failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());
            }
            return $result;
        } else {
            throw new InvalidConfigException('Format as Ordinal is only supported when PHP intl extension is installed.');
        }
    }

Usage Example

Пример #1
0
 public function testIntlAsOrdinal()
 {
     $this->assertSame('0th', $this->formatter->asOrdinal(0));
     $this->assertSame('1st', $this->formatter->asOrdinal(1));
     $this->assertSame('2nd', $this->formatter->asOrdinal(2));
     $this->assertSame('3rd', $this->formatter->asOrdinal(3));
     $this->assertSame('5th', $this->formatter->asOrdinal(5));
     $this->formatter->locale = 'de_DE';
     $this->assertSame('0.', $this->formatter->asOrdinal(0));
     $this->assertSame('1.', $this->formatter->asOrdinal(1));
     $this->assertSame('2.', $this->formatter->asOrdinal(2));
     $this->assertSame('3.', $this->formatter->asOrdinal(3));
     $this->assertSame('5.', $this->formatter->asOrdinal(5));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asOrdinal(null));
 }