/** * @inheritdoc */ protected function getValue($event) { $isNewSlug = true; if ($this->attribute !== null) { $attributes = (array) $this->attribute; /* @var $owner BaseActiveRecord */ $owner = $this->owner; if (!empty($owner->{$this->slugAttribute})) { $isNewSlug = false; if (!$this->immutable) { foreach ($attributes as $attribute) { if ($owner->isAttributeChanged($attribute)) { $isNewSlug = true; break; } } } } if ($isNewSlug) { $slugParts = []; foreach ($attributes as $attribute) { $slugParts[] = $owner->{$attribute}; } $slug = Inflector::slug(implode('-', $slugParts)); } else { $slug = $owner->{$this->slugAttribute}; } } else { $slug = parent::getValue($event); } if ($this->ensureUnique && $isNewSlug) { $baseSlug = $slug; $iteration = 0; while (!$this->validateSlug($slug)) { $iteration++; $slug = $this->generateUniqueSlug($baseSlug, $iteration); } } return $slug; }
/** * Generates a user friendly attribute label based on the give attribute name. * * This is done by replacing underscores, dashes and dots with blanks and * changing the first letter of each word to upper case. * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'. * * @param string $name the column name * @return string the attribute label */ public function generateAttributeLabel($name) { return Inflector::camel2words($name, true); }
/** * Translit username. * * @param string $username * @return string */ protected function translitUsername($username) { return Inflector::slug(preg_replace('/[\\_\\-]+/iu', '-', $username)); }
protected function defaultFormatOptions() { return ['F' => function (DateTime $datetime) { return $datetime->getLocale()->getMonth($datetime->format('n') - 1); }, 'M' => function (DateTime $datetime) { return $datetime->getLocale()->getShortMonth($datetime->format('n') - 1); }, 'l' => function (DateTime $datetime) { return $datetime->getLocale()->getWeekDay($datetime->format('N') - 1); }, 'D' => function (DateTime $datetime) { return $datetime->getLocale()->getShortWeekDay($datetime->format('N') - 1); }, 'ago' => function (DateTime $datetime) { $diff = $datetime->diff(new DateTime()); $locale = $datetime->getLocale(); if ($diff->y >= 1) { $number = $diff->y; $names = $locale->getYearNames(); } elseif ($diff->total_months >= 1) { $number = $diff->total_months; $names = $locale->getMonthNames(); } elseif ($diff->total_days >= 7) { $number = $diff->total_weeks; $names = $locale->getWeekNames(); } elseif ($diff->total_days >= 1) { $number = $diff->total_days; $names = $locale->getDayNames(); } elseif ($diff->total_hours >= 1) { $number = $diff->total_hours; $names = $locale->getHourNames(); } elseif ($diff->total_minutes >= 1) { $number = $diff->total_minutes; $names = $locale->getMinuteNames(); } else { $number = $diff->total_seconds; $names = $locale->getSecondNames(); } $options = $locale->getOptions(); if (isset($options['ago'])) { return StringHelper::replace($options['ago'], ['number' => $number, 'name' => Inflector::plural($number, $names)]); } return $number . ' ' . Inflector::plural($number, $names) . ' ago'; }]; }
/** * Declares the name of the Sphinx index associated with this AR class. * By default this method returns the class name as the index name by calling {@see \rock\helpers\Inflector::camel2id()}. * For example, 'Article' becomes 'article', and 'StockItem' becomes * 'stock_item'. You may override this method if the index is not named after this convention. * @return string the index name */ public static function indexName() { return Inflector::camel2id(ObjectHelper::basename(get_called_class()), '_'); }
/** * Declares the name of the database table associated with this AR class. * By default this method returns the class name as the table name by calling {@see \rock\helpers\Inflector::camel2id()} * with prefix {@see \rock\db\Connection::$tablePrefix}. * For example if {@see \rock\db\Connection::$tablePrefix} is 'tbl_', 'Customer' becomes 'tbl_customer', * and 'OrderItem' becomes 'tbl_order_item'. You may override this method * if the table is not named after this convention. * * @return string the table name */ public static function tableName() { return '{{%' . Inflector::camel2id(ObjectHelper::basename(get_called_class()), '_') . '}}'; }
/** * @inheritdoc */ public function sanitize($input) { return is_string($input) ? Inflector::slug($input, $this->replacement, $this->lowercase) : $input; }
/** * Resolves class name from widget and asset syntax * * @param string $className class name * @return string */ public function resolveClassName($className) { $className = Inflector::id2camel($className, '_'); if (isset($this->aliases[$className])) { return $this->aliases[$className]; } foreach ($this->namespaces as $namespace) { $resolvedClassName = $namespace . '\\' . $className; if (class_exists($resolvedClassName)) { return $this->aliases[$className] = $resolvedClassName; } } return $className; }
/** * Generates a hyperlink that links to the sort action to sort by the specified attribute. * Based on the sort direction, the CSS class of the generated hyperlink will be appended * with "asc" or "desc". * @param string $attribute the attribute name by which the data should be sorted by. * @param array $options additional HTML attributes for the hyperlink tag. * There is one special attribute `label` which will be used as the label of the hyperlink. * If this is not set, the label defined in {@see \rock\data\Sort::$attributes} will be used. * If no label is defined, {@see \rock\helpers\Inflector::camel2words()} will be called to get a label. * Note that it will not be HTML-encoded. * @return string the generated hyperlink * @throws DataProviderException if the attribute is unknown */ public function link($attribute, $options = []) { if (!class_exists('\\rock\\template\\Html')) { throw new DataProviderException(DataProviderException::NOT_INSTALL_TEMPLATE); } if (($direction = $this->getAttributeOrder($attribute)) !== null) { $class = $direction === SORT_DESC ? 'desc' : 'asc'; if (isset($options['class'])) { $options['class'] .= ' ' . $class; } else { $options['class'] = $class; } } $url = $this->createUrl($attribute); $options['data-sort'] = $this->createSortParam($attribute); if (isset($options['label'])) { $label = $options['label']; unset($options['label']); } else { if (isset($this->attributes[$attribute]['label'])) { $label = $this->attributes[$attribute]['label']; } else { $label = Inflector::camel2words($attribute); } } return Html::a($label, $url, $options); }