'"', "\'" => "'", '\\\\' => '\\'); /** * * @param mixed $value * @return string */ public static function detectType($value) { if (is_int($value)) { return ExpType::$INT; } elseif (is_float($value)) { return ExpType::$FLOAT; } elseif (is_string($value)) { return ExpType::$STRING; } elseif (is_bool($value)) { return ExpType::$BOOL; } elseif (is_null($value)) { return ExpType::$NULL; } elseif (is_array($value)) { return ExpType::$ARRAY; } elseif (is_object($value)) { return ExpType::$OBJECT; } else { throw new ExpParserException("Un-recogonized variable type of identity: " . $value); } } /** * @param Token $token * @return Token */ public static function coerceToNumber($token) { $INTEGER_PATTERN = "/^[0-9]+$/"; $type = $token->type; if (in_array($type, array(ExpType::$INT, ExpType::$FLOAT))) return $token; if (in_array($type, array(ExpType::$BOOL, ExpType::$NULL)) || in_array($type, array(ExpType::$RAW, ExpType::$STRING)) && preg_match($INTEGER_PATTERN, $token->value) == 1) { $int = new Token(ExpType::$INT, (int)($token->value)); return $int; } if (in_array($type, array(ExpType::$RAW, ExpType::$STRING))) { $float = new Token(ExpType::$FLOAT, (float)($token->value)); return $float; } throw new ExpTypeException("Unable to coerce token " . print_r($token, true) . " to number"); } /** * @param Token $token * @return Token */ public static function coerceToString($token) { $PRIMITIVE_TYPES = array(ExpType::$INT, ExpType::$FLOAT, ExpType::$STRING, ExpType::$BOOL, ExpType::$NULL); $COMPOSITE_TYPES = array(ExpType::$ARRAY, ExpType::$OBJECT); $type = $token->type; if ($type == ExpType::$STRING) return $token; $string = new Token(ExpType::$STRING); if ($type == ExpType::$RAW) { $string->value = strtr($token->value, ExpType::$ESCAPE_CHARS); } elseif ($type == ExpType::$BOOL) { $string->value = ($token->value) ? 'true' : 'false'; } elseif ($type == ExpType::$NULL) { $string->value = 'null'; } elseif (in_array($type, $PRIMITIVE_TYPES)) { $string->value = (string)($token->value); } elseif (in_array($type, $COMPOSITE_TYPES)) { $string->value = print_r($token->value, true); // maybe call .toString()? } else { throw new ExpTypeException("Unable to coerce token" . print_r($token, true) . " to string"); } return $string; } /** * @param Token $token * @return Token */ public static function coerceToBool($token) { $PRIMITIVE_TYPES = array(ExpType::$INT, ExpType::$FLOAT, ExpType::$STRING, ExpType::$BOOL, ExpType::$NULL); $COMPOSITE_TYPES = array(ExpType::$ARRAY, ExpType::$OBJECT); $type = $token->type; if ($type == ExpType::$BOOL) return $token; $bool = new Token(ExpType::$BOOL); if ($type == ExpType::$RAW) { $bool->value = strtolower($token->value) == 'true' ? true : false; } elseif (in_array($type, $PRIMITIVE_TYPES)) { $bool->value = (bool)($token->value); } elseif (in_array($type, $COMPOSITE_TYPES)) { $bool->value = $token->value != null ? true : false; } else { throw new ExpTypeException("Unable to coerce token" . print_r($token, true) . " to bool"); } return $bool; } /** * @param Token $token * @return Token */ public static function coerceToNull($token) { $COMPOSITE_TYPES = array(ExpType::$ARRAY, ExpType::$OBJECT); $type = $token->type; if ($type == ExpType::$NULL) return $token; $null = new Token(ExpType::$NULL); $value = $token->value; if ($type == ExpType::$RAW && strtolower($value) == 'null' || in_array($type, $COMPOSITE_TYPES) && $token->value == null) { $null->value = null; } else { throw new ExpTypeException("Unable to coerce token" . print_r($token, true) . " to null"); } return $null; } }