public function testSentence() { $array = []; $this->assertEquals('', Inflector::sentence($array)); $array = ['Spain']; $this->assertEquals('Spain', Inflector::sentence($array)); $array = ['Spain', 'France']; $this->assertEquals('Spain and France', Inflector::sentence($array)); $array = ['Spain', 'France', 'Italy']; $this->assertEquals('Spain, France and Italy', Inflector::sentence($array)); $array = ['Spain', 'France', 'Italy', 'Germany']; $this->assertEquals('Spain, France, Italy and Germany', Inflector::sentence($array)); $array = ['Spain', 'France']; $this->assertEquals('Spain or France', Inflector::sentence($array, ' or ')); $array = ['Spain', 'France', 'Italy']; $this->assertEquals('Spain, France or Italy', Inflector::sentence($array, ' or ')); $array = ['Spain', 'France']; $this->assertEquals('Spain and France', Inflector::sentence($array, ' and ', ' or ', ' - ')); $array = ['Spain', 'France', 'Italy']; $this->assertEquals('Spain - France or Italy', Inflector::sentence($array, ' and ', ' or ', ' - ')); }
/** * Builds and adds [[comboNotUnique]] error message to the specified model attribute. * @param \yii\base\Model $model the data model. * @param string $attribute the name of the attribute. */ private function addComboNotUniqueError($model, $attribute) { $attributeCombo = []; $valueCombo = []; foreach ($this->targetAttribute as $key => $value) { if (is_int($key)) { $attributeCombo[] = $model->getAttributeLabel($value); $valueCombo[] = '"' . $model->{$value} . '"'; } else { $attributeCombo[] = $model->getAttributeLabel($key); $valueCombo[] = '"' . $model->{$key} . '"'; } } $this->addError($model, $attribute, $this->message, ['attributes' => Inflector::sentence($attributeCombo), 'values' => implode('-', $valueCombo)]); }
/** * Formats time value with given unit to text. * * @param mixed $value time value to be formatted. * @param integer $unit unit type of value. * Must be one on the following: [[TIMETEXT_YEARS]], [[TIMETEXT_MONTHS]] or [[TIMETEXT_DAYS]]. * @return string formatted time text. * @throws InvalidConfigException if no unit is given and [[timeTextUnit]] is not defined. */ public function asTimeText($value, $unit = null) { if ($unit === null) { if ($this->timeTextUnit === null) { throw new InvalidConfigException('The unit for this formatter is not defined.'); } $unit = $this->timeTextUnit; } $value = $this->normalizeNumericValue($value); $components = $this->getTimeComponents($value, $unit); $componentsText = []; if ($components[0] > 0) { $componentsText[] = Yii::t('asBase', '{a, plural, =1{1 year} other{# years}}', ['a' => $components[0]]); } if ($components[1] > 0) { $componentsText[] = Yii::t('asBase', '{m, plural, =1{1 month} other{# months}}', ['m' => $components[1]]); } if ($components[2] > 0) { $componentsText[] = Yii::t('asBase', '{d, plural, =1{1 day} other{# days}}', ['d' => $components[2]]); } return Inflector::sentence($componentsText); }