/** * Specifies a new irregular that applies to both pluralization and singularization at the * same time. This can only be used for strings, not regular expressions. You simply pass * the irregular in singular and plural form. * * <pre> * $this->irregular('child', 'children'); * $this->irregular('person', 'people'); * </pre> * * @param string $singular * @param string $plural */ public function irregular($singular, $plural) { unset($this->uncountables[$singular]); unset($this->uncountables[$plural]); $s0 = mb_substr($singular, 0, 1); $s0_upcase = upcase($s0); $srest = mb_substr($singular, 1); $p0 = mb_substr($plural, 0, 1); $p0_upcase = upcase($p0); $prest = mb_substr($plural, 1); if ($s0_upcase == $p0_upcase) { $this->plural("/({$s0}){$srest}\$/i", '\\1' . $prest); $this->plural("/({$p0}){$prest}\$/i", '\\1' . $prest); $this->singular("/({$s0}){$srest}\$/i", '\\1' . $srest); $this->singular("/({$p0}){$prest}\$/i", '\\1' . $srest); } else { $s0_downcase = downcase($s0); $p0_downcase = downcase($p0); $this->plural("/{$s0_upcase}(?i){$srest}\$/", $p0_upcase . $prest); $this->plural("/{$s0_downcase}(?i){$srest}\$/", $p0_downcase . $prest); $this->plural("/{$p0_upcase}(?i){$prest}\$/", $p0_upcase . $prest); $this->plural("/{$p0_downcase}(?i){$prest}\$/", $p0_downcase . $prest); $this->singular("/{$s0_upcase}(?i){$srest}\$/", $s0_upcase . $srest); $this->singular("/{$s0_downcase}(?i){$srest}\$/", $s0_downcase . $srest); $this->singular("/{$p0_upcase}(?i){$prest}\$/", $s0_upcase . $srest); $this->singular("/{$p0_downcase}(?i){$prest}\$/", $s0_downcase . $srest); } return $this; }
/** * Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", * if any. Like {@link titleize()}, this is meant for creating pretty output. * * <pre> * $this->humanize('employee_salary'); // "Employee salary" * $this->humanize('author_id'); // "Author" * </pre> * * @param string $lower_case_and_underscored_word * * @return string */ public function humanize($lower_case_and_underscored_word) { $result = (string) $lower_case_and_underscored_word; foreach ($this->inflections->humans as $rule => $replacement) { $result = preg_replace($rule, $replacement, $result, 1, $count); if ($count) { break; } } $acronyms = $this->inflections->acronyms; $result = preg_replace('/_id$/', "", $result); $result = strtr($result, '_', ' '); $result = preg_replace_callback('/([[:alnum:]]+)/u', function ($matches) use($acronyms) { list($m) = $matches; return !empty($acronyms[$m]) ? $acronyms[$m] : downcase($m); }, $result); $result = preg_replace_callback('/^[[:lower:]]/u', function ($matches) { return upcase($matches[0]); }, $result); return $result; }
/** * Returns a copy of str with the first character converted to uppercase and the * remainder to lowercase. * * @param string $str */ function capitalize($str) { return upcase(mb_substr($str, 0, 1)) . downcase(mb_substr($str, 1)); }