format() public method

This method will call one of the "as" methods available in this class to do the formatting. For type "xyz", the method "asXyz" will be used. For example, if the format is "html", then Formatter::asHtml will be used. Format names are case insensitive.
public format ( mixed $value, string | array $format ) : string
$value mixed the value to be formatted.
$format string | array the format of the value, e.g., "html", "text". To specify additional parameters of the formatting method, you may use an array. The first element of the array specifies the format name, while the rest of the elements will be used as the parameters to the formatting method. For example, a format of `['date', 'Y-m-d']` will cause the invocation of `asDate($value, 'Y-m-d')`.
return string the formatting result.
Esempio n. 1
0
 public function testFormat()
 {
     $value = time();
     $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date'));
     $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE'));
     $this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'php:Y/m/d']));
     $this->setExpectedException('\\yii\\base\\InvalidParamException');
     $this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
 }
 /**
  * https://github.com/yiisoft/yii2/issues/6263
  *
  * it is a PHP bug: https://bugs.php.net/bug.php?id=45543
  * Fixed in this commit: https://github.com/php/php-src/commit/22dba2f5f3211efe6c3b9bb24734c811ca64c68c#diff-7b738accc3d60f74c259da18588ddc5dL2996
  * Fixed in PHP >5.4.26 and >5.5.10. http://3v4l.org/mlZX7
  *
  * @dataProvider provideTimezones
  */
 public function testIssue6263($dtz)
 {
     $this->formatter->defaultTimeZone = $dtz;
     $this->formatter->timeZone = 'UTC';
     $this->assertEquals('24.11.2014 11:48:53', $this->formatter->format(1416829733, ['date', 'php:d.m.Y H:i:s']));
     $this->formatter->timeZone = 'Europe/Berlin';
     $this->assertEquals('24.11.2014 12:48:53', $this->formatter->format(1416829733, ['date', 'php:d.m.Y H:i:s']));
     $this->assertFalse(DateTime::createFromFormat('Y-m-d', 1416829733));
     $this->assertFalse(DateTime::createFromFormat('Y-m-d', '2014-05-08 12:48:53'));
     $this->assertFalse(DateTime::createFromFormat('Y-m-d H:i:s', 1416829733));
     $this->assertFalse(DateTime::createFromFormat('Y-m-d H:i:s', '2014-05-08'));
 }
Esempio n. 3
0
 /**
  * Generates the static input
  *
  * @param string $type the static input type.
  * @param Model $model the data model.
  * @param integer $index the zero based index of the item in dataProvider.
  * @param array $settings the attribute settings.
  * @param string $attribute the attribute.
  * @param Formatter $formatter the formatter instance.
  *
  * @return string the generated static input.
  */
 protected function getStaticInput($type, $model, $index, $settings, $attribute, $formatter)
 {
     $format = ArrayHelper::getValue($settings, 'format', 'raw');
     if ($type === self::INPUT_HIDDEN_STATIC) {
         $options = ArrayHelper::getValue($settings, 'hiddenStaticOptions', []);
     } else {
         $options = ArrayHelper::getValue($settings, 'options', []);
     }
     $val = null;
     if (isset($settings['staticValue'])) {
         $val = $settings['staticValue'];
     } else {
         if (isset($settings['value'])) {
             $val = $settings['value'];
         } elseif ($model instanceof Model) {
             $val = Html::getAttributeValue($model, $attribute);
         } elseif (($models = $this->dataProvider->getModels()) && !empty($models[$index][$attribute])) {
             $val = $models[$index][$attribute];
         }
     }
     $val = $formatter->format($val, $format);
     $prepend = ArrayHelper::getValue($settings, 'prepend', '');
     $append = ArrayHelper::getValue($settings, 'append', '');
     $val = $prepend . "\n" . $val . "\n" . $append;
     Html::addCssClass($options, 'form-control-static');
     return Html::tag('div', $val, $options);
 }
Esempio n. 4
0
 /**
  * Converts relational object to DetailView structure
  * @return array
  */
 public function getDetailViewProfileData()
 {
     $profileFields = array_keys($this->profileFields);
     $excludeFields = ['name'];
     $data = [];
     if (!empty($profileFields) && $this->profileModel !== null) {
         foreach ($profileFields as $name) {
             if (!in_array($name, $excludeFields) && isset($this->profileModel->{$name}) && !empty($this->profileModel->{$name}) && is_scalar($this->profileModel->{$name})) {
                 switch ($this->profileFields[$name]['field_type']) {
                     case 'date':
                         $format = new Formatter();
                         $data[] = ['attribute' => "profileModel.{$name}", 'label' => $this->profileFields[$name]['label'], 'format' => 'html', 'value' => '<abbr title="' . $this->profileModel->{$name} . '">' . $format->format($this->profileModel->{$name}, 'RelativeTime') . '</abbr>'];
                         break;
                     case 'url':
                         $data[] = ['attribute' => "profileModel.{$name}", 'label' => $this->profileFields[$name]['label'], 'format' => 'raw', 'value' => Html::a($this->profileModel->{$name}, $this->profileModel->{$name}, ['target' => '_blank'])];
                         break;
                     default:
                         $data[] = ['attribute' => "profileModel.{$name}", 'label' => $this->profileFields[$name]['label'], 'value' => $this->profileModel->{$name}];
                         break;
                 }
             }
         }
     }
     return $data;
 }