scaping characters as necessary
*
* @param $value string
* @return string
*/
protected function _encodeString(&$string)
{
// Escape these characters with a backslash:
// " \ / \n \r \t \b \f
$search = array('\\', "\n", "\t", "\r", "\b", "\f", '"');
$replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"');
$string = str_replace($search, $replace, $string);
// Escape certain ASCII characters:
// 0x08 => \b
// 0x0c => \f
$string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
return '"' . $string . '"';
}
/**
* Encode the constants associated with the ReflectionClass
* parameter. The encoding format is based on the class2 format
*
* @param $cls ReflectionClass
* @return string Encoded constant block in class2 format
*/
static private function _encodeConstants(ReflectionClass $cls)
{
$result = "constants : {";
$constants = $cls->getConstants();
$tmpArray = array();
if (!empty($constants)) {
foreach ($constants as $key => $value) {
$tmpArray[] = "$key: " . self::encode($value);
}
$result .= implode(', ', $tmpArray);
}
return $result . "}";
}
/**
* Encode the public methods of the ReflectionClass in the
* class2 format
*
* @param $cls ReflectionClass
* @return string Encoded method fragment
*
*/
static private function _encodeMethods(ReflectionClass $cls)
{
$methods = $cls->getMethods();
$result = 'methods : {';
$started = false;
foreach ($methods as $method) {
if (! $method->isPublic() || !$method->isUserDefined()) {
continue;
}
if ($started) {
$result .= ", ";
}
$started = true;
$result .= " " .$method->getName(). ': function(';
if ('__construct' != $method->getName()) {
$parameters = $method->getParameters();
$paramCount = count($parameters);
$argsStarted = false;
$argNames = "var argNames = [";
foreach ($parameters as $param) {
if ($argsStarted) {
$result .= ', ';