}
/**
- * Convert possibly underscore separated words to camel case with special handling for 'UF'
- * e.g membership_payment returns MembershipPayment
- *
- * @param string $string
+ * Convert possibly underscore separated words to camel case.
*
+ * @param string $str
+ * @param bool $ucFirst
+ * Should the first letter be capitalized like `CamelCase` or lower like `camelCase`
* @return string
*/
- public static function convertStringToCamel($string) {
- $map = [
- 'acl' => 'Acl',
- 'ACL' => 'Acl',
- 'im' => 'Im',
- 'IM' => 'Im',
- ];
- if (isset($map[$string])) {
- return $map[$string];
- }
-
- $fragments = explode('_', $string);
- foreach ($fragments as & $fragment) {
- $fragment = ucfirst($fragment);
- // Special case: UFGroup, UFJoin, UFMatch, UFField (if passed in without underscores)
- if (strpos($fragment, 'Uf') === 0 && strlen($string) > 2) {
- $fragment = 'UF' . ucfirst(substr($fragment, 2));
- }
- }
- // Special case: UFGroup, UFJoin, UFMatch, UFField (if passed in underscore-separated)
- if ($fragments[0] === 'Uf') {
- $fragments[0] = 'UF';
- }
- return implode('', $fragments);
+ public static function convertStringToCamel($str, $ucFirst = TRUE) {
+ $fragments = explode('_', $str);
+ $camel = implode('', array_map('ucfirst', $fragments));
+ return $ucFirst ? $camel : lcfirst($camel);
}
/**
* @return string
*/
public static function pluralize($str) {
- switch (substr($str, -1)) {
- case 's':
- return $str . 'es';
-
- case 'y':
- return substr($str, 0, -1) . 'ies';
-
- default:
- return $str . 's';
+ $lastLetter = substr($str, -1);
+ $lastTwo = substr($str, -2);
+ if ($lastLetter == 's' || $lastLetter == 'x' || $lastTwo == 'ch') {
+ return $str . 'es';
+ }
+ if ($lastLetter == 'y' && !in_array($lastTwo, ['ay', 'ey', 'iy', 'oy', 'uy'])) {
+ return substr($str, 0, -1) . 'ies';
}
+ return $str . 's';
}
/**