MISP/app/Lib/CamelCase.php

52 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
2012-12-18 09:38:29 +01:00
* http://en.wikipedia.org/wiki/CamelCase
2012-12-19 14:34:40 +01:00
* 70 | ERROR | Public method name "notUsed_Call" is not in camel caps format
**/
class CamelCase {
/**
2012-12-18 09:38:29 +01:00
* http://php.net/manual/en/function.lcfirst.php
**/
2012-12-19 02:48:53 +01:00
public function lcfirst($str) {
2012-12-18 09:38:29 +01:00
$str{0} = strtolower($str{0});
return $str;
}
2012-12-19 02:48:53 +01:00
/**
*
**/
public function camelBack($input) {
return $this->lcfirst($this->fromCamelCase($match));
}
2012-12-18 09:38:29 +01:00
/**
* http://stackoverflow.com/questions/1993721/how-to-convert-camelcase-to-camel-case
**/
2012-12-19 02:48:53 +01:00
public function fromCamelCase($input) {
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
2012-12-18 09:38:29 +01:00
$match = $match == strtoupper($match) ? strtolower($match) : $this->lcfirst($match); // TODO string lcfirst
}
return implode('_', $ret);
}
2012-12-18 09:38:29 +01:00
/**
* http://www.paulferrett.com/2009/php-camel-case-functions/
**/
2012-12-19 02:48:53 +01:00
public function fromCamelCase2($str) {
2012-12-18 09:38:29 +01:00
$str[0] = strtolower($str[0]);
$func = create_function('$c', 'return "_" . strtolower($c[1]);');
return preg_replace_callback('/([A-Z])/', $func, $str);
}
/**
*
**/
2012-12-19 02:48:53 +01:00
public function toCamelCase($underscored) {
//App::uses('Inflector', 'lib');
return Inflector::camelize($underscored);
2012-12-19 02:48:53 +01:00
}
}