Upgrade to 4.0.0
This commit is contained in:
@@ -36,10 +36,10 @@ class Sane
|
||||
* All registered handlers
|
||||
*/
|
||||
public static array $handlers = [
|
||||
'html' => 'Kirby\Sane\Html',
|
||||
'svg' => 'Kirby\Sane\Svg',
|
||||
'svgz' => 'Kirby\Sane\Svgz',
|
||||
'xml' => 'Kirby\Sane\Xml',
|
||||
'html' => Html::class,
|
||||
'svg' => Svg::class,
|
||||
'svgz' => Svgz::class,
|
||||
'xml' => Xml::class,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -49,16 +49,19 @@ class Sane
|
||||
*
|
||||
* @throws \Kirby\Exception\NotFoundException If no handler was found and `$lazy` was set to `false`
|
||||
*/
|
||||
public static function handler(string $type, bool $lazy = false): Handler|null
|
||||
{
|
||||
public static function handler(
|
||||
string $type,
|
||||
bool $lazy = false
|
||||
): Handler|null {
|
||||
// normalize the type
|
||||
$type = mb_strtolower($type);
|
||||
|
||||
// find a handler or alias
|
||||
$alias = static::$aliases[$type] ?? null;
|
||||
$handler =
|
||||
static::$handlers[$type] ??
|
||||
($alias ? static::$handlers[$alias] ?? null : null);
|
||||
$handler = static::$handlers[$type] ?? null;
|
||||
|
||||
if ($alias = static::$aliases[$type] ?? null) {
|
||||
$handler ??= static::$handlers[$alias] ?? null;
|
||||
}
|
||||
|
||||
if (empty($handler) === false && class_exists($handler) === true) {
|
||||
return new $handler();
|
||||
@@ -74,10 +77,13 @@ class Sane
|
||||
/**
|
||||
* Sanitizes the given string with the specified handler
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @param bool $isExternal Whether the string is from an external file
|
||||
* that may be accessed directly
|
||||
*/
|
||||
public static function sanitize(string $string, string $type): string
|
||||
public static function sanitize(string $string, string $type, bool $isExternal = false): string
|
||||
{
|
||||
return static::handler($type)->sanitize($string);
|
||||
return static::handler($type)->sanitize($string, $isExternal);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,8 +102,10 @@ class Sane
|
||||
* @throws \Kirby\Exception\NotFoundException If the handler was not found
|
||||
* @throws \Kirby\Exception\Exception On other errors
|
||||
*/
|
||||
public static function sanitizeFile(string $file, string|bool $typeLazy = false): void
|
||||
{
|
||||
public static function sanitizeFile(
|
||||
string $file,
|
||||
string|bool $typeLazy = false
|
||||
): void {
|
||||
if (is_string($typeLazy) === true) {
|
||||
static::handler($typeLazy)->sanitizeFile($file);
|
||||
return;
|
||||
@@ -126,13 +134,16 @@ class Sane
|
||||
/**
|
||||
* Validates file contents with the specified handler
|
||||
*
|
||||
* @param bool $isExternal Whether the string is from an external file
|
||||
* that may be accessed directly
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the file didn't pass validation
|
||||
* @throws \Kirby\Exception\NotFoundException If the handler was not found
|
||||
* @throws \Kirby\Exception\Exception On other errors
|
||||
*/
|
||||
public static function validate(string $string, string $type): void
|
||||
public static function validate(string $string, string $type, bool $isExternal = false): void
|
||||
{
|
||||
static::handler($type)->validate($string);
|
||||
static::handler($type)->validate($string, $isExternal);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,14 +159,18 @@ class Sane
|
||||
* @throws \Kirby\Exception\NotFoundException If the handler was not found
|
||||
* @throws \Kirby\Exception\Exception On other errors
|
||||
*/
|
||||
public static function validateFile(string $file, string|bool $typeLazy = false): void
|
||||
{
|
||||
public static function validateFile(
|
||||
string $file,
|
||||
string|bool $typeLazy = false
|
||||
): void {
|
||||
if (is_string($typeLazy) === true) {
|
||||
static::handler($typeLazy)->validateFile($file);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (static::handlersForFile($file, $typeLazy === true) as $handler) {
|
||||
$handlers = static::handlersForFile($file, $typeLazy === true);
|
||||
|
||||
foreach ($handlers as $handler) {
|
||||
$handler->validateFile($file);
|
||||
}
|
||||
}
|
||||
@@ -167,8 +182,10 @@ class Sane
|
||||
* @param bool $lazy If set to `true`, undefined handlers are skipped
|
||||
* @return array<\Kirby\Sane\Handler>
|
||||
*/
|
||||
protected static function handlersForFile(string $file, bool $lazy = false): array
|
||||
{
|
||||
protected static function handlersForFile(
|
||||
string $file,
|
||||
bool $lazy = false
|
||||
): array {
|
||||
$handlers = $handlerClasses = [];
|
||||
|
||||
// all values that can be used for the handler search;
|
||||
@@ -180,7 +197,10 @@ class Sane
|
||||
$handlerClass = $handler ? get_class($handler) : null;
|
||||
|
||||
// ensure that each handler class is only returned once
|
||||
if ($handler && in_array($handlerClass, $handlerClasses) === false) {
|
||||
if (
|
||||
$handler &&
|
||||
in_array($handlerClass, $handlerClasses) === false
|
||||
) {
|
||||
$handlers[] = $handler;
|
||||
$handlerClasses[] = $handlerClass;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user