* @param string strarg * @return boolean */ public static function isUri($strarg) { $returnValue = (bool) false; $uri = trim($strarg); if (!empty($uri)) { if ((preg_match("/^(http|https|file|ftp):\/\/[\/:.A-Za-z0-9_-]+#[A-Za-z0-9_-]+$/", $uri) && strpos($uri, '#') > 0) || strpos($uri, "#") === 0) { $returnValue = true; } } return (bool) $returnValue; } public static function toResource($value) { if (is_array($value)) { $returnValue = []; foreach ($value as $val) { $returnValue[] = self::toResource($val); } return $returnValue; } else { if (common_Utils::isUri($value)) { return new core_kernel_classes_Resource($value); } else { return new core_kernel_classes_Literal($value); } } } /** * Removes starting/ending spaces, strip html tags out, remove any \r and \n * * @access public * @author Joel Bout, * @param string strarg * @return string */ public static function fullTrim($strarg) { return strip_tags(trim($strarg)); } /** * Backward compatibility function for URI Provider * * @access public * @author Joel Bout, * @return string * @deprecated */ public static function getNewUri() { return ServiceManager::getServiceManager()->get(UriProvider::SERVICE_ID)->provide(); } /** * Returns the php code, that if evaluated * would return the value provided * * @access public * @author Joel Bout, * @param value * @return string */ public static function toPHPVariableString($value) { switch (gettype($value)) { case "string": $returnValue = '\'' . str_replace('\'', '\\\'', str_replace('\\', '\\\\', $value)) . '\''; break; case "boolean": $returnValue = $value ? 'true' : 'false'; break; case "integer": case "double": $returnValue = $value; break; case "array": $string = ""; foreach ($value as $key => $val) { $string .= self::toPHPVariableString($key) . " => " . self::toPHPVariableString($val) . ","; } $returnValue = "array(" . substr($string, 0, -1) . ")"; break; case "NULL": $returnValue = 'null'; break; case "object": if ($value instanceof PhpSerializable) { $returnValue = $value->__toPhpCode(); } else { $returnValue = 'unserialize(' . self::toPHPVariableString(serialize($value)) . ')'; } break; default: // resource and unexpected types throw new common_exception_Error("Could not convert variable of type " . gettype($value) . " to PHP variable string"); } return (string) $returnValue; } /** * Same as toPHPVariableString except the sting * is easier for humans to read * * @param mixed $value * @param number indentation * @return string */ public static function toHumanReadablePhpString($value, $indentation = 0) { if (is_array($value)) { $array = array_keys($value); $assocArray = ($array !== array_keys($array)); $string = ""; if ($assocArray) { foreach ($value as $key => $val) { $string .= PHP_EOL . str_repeat(' ', $indentation + 1) . self::toPHPVariableString($key) . " => " . self::toHumanReadablePhpString($val, $indentation + 1) . ","; } } else { foreach ($value as $val) { $string .= PHP_EOL . str_repeat(' ', $indentation + 1) . self::toHumanReadablePhpString($val, $indentation + 1) . ","; } } $returnValue = "array(" . substr($string, 0, -1) . PHP_EOL . str_repeat(' ', $indentation) . ")"; } elseif (is_string($value)) { $returnValue = self::toPHPVariableString($value); } else { $lines = explode(PHP_EOL, self::toPHPVariableString($value)); $returnValue = implode(PHP_EOL . str_repeat(' ', $indentation), $lines); } return (string) $returnValue; } }