mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #5732 from open-sausages/pulls/4.0/convert-lowercase
Convert::upperCamelToLowerCamel()
This commit is contained in:
commit
fd9f29c515
@ -439,4 +439,47 @@ class Convert {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts upper camel case names to lower camel case,
|
||||||
|
* with leading upper case characters replaced with lower case.
|
||||||
|
* Tries to retain word case.
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - ID => id
|
||||||
|
* - IDField => idField
|
||||||
|
* - iDField => iDField
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function upperCamelToLowerCamel($str) {
|
||||||
|
$return = null;
|
||||||
|
$matches = null;
|
||||||
|
if(preg_match('/(^[A-Z]{1,})([A-Z]{1})([a-z]+.*)/', $str, $matches)) {
|
||||||
|
// If string has trailing lowercase after more than one leading uppercase characters,
|
||||||
|
// match everything but the last leading uppercase character.
|
||||||
|
$return = implode('', [
|
||||||
|
strtolower($matches[1]),
|
||||||
|
$matches[2],
|
||||||
|
$matches[3]
|
||||||
|
]);
|
||||||
|
} else if(preg_match('/(^[A-Z]{1})([a-z]+.*)/', $str, $matches)) {
|
||||||
|
// If string has trailing lowercase after exactly one leading uppercase characters,
|
||||||
|
// match everything but the last leading uppercase character.
|
||||||
|
$return = implode('', [
|
||||||
|
strtolower($matches[1]),
|
||||||
|
$matches[2]
|
||||||
|
]);
|
||||||
|
} elseif(preg_match('/^[A-Z]+$/', $str)) {
|
||||||
|
// If string has leading uppercase without trailing lowercase,
|
||||||
|
// just lowerase the whole thing.
|
||||||
|
$return = strtolower($str);
|
||||||
|
} else {
|
||||||
|
// If string has no leading uppercase, just return.
|
||||||
|
$return = $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -396,4 +396,42 @@ XML
|
|||||||
Convert::base64url_decode(Convert::base64url_encode($data))
|
Convert::base64url_decode(Convert::base64url_encode($data))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUpperCamelToLowerCamel() {
|
||||||
|
$this->assertEquals(
|
||||||
|
'd',
|
||||||
|
Convert::upperCamelToLowerCamel('D'),
|
||||||
|
'Single character'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'id',
|
||||||
|
Convert::upperCamelToLowerCamel('ID'),
|
||||||
|
'Multi leading upper without trailing lower'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'id',
|
||||||
|
Convert::upperCamelToLowerCamel('Id'),
|
||||||
|
'Single leading upper with trailing lower'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'idField',
|
||||||
|
Convert::upperCamelToLowerCamel('IdField'),
|
||||||
|
'Single leading upper with trailing upper camel'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'idField',
|
||||||
|
Convert::upperCamelToLowerCamel('IDField'),
|
||||||
|
'Multi leading upper with trailing upper camel'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'iDField',
|
||||||
|
Convert::upperCamelToLowerCamel('iDField'),
|
||||||
|
'Single leading lower with trailing upper camel'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'_IDField',
|
||||||
|
Convert::upperCamelToLowerCamel('_IDField'),
|
||||||
|
'Non-alpha leading with trailing upper camel'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user