first commit.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Libs\Sanitizer;
|
||||
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
|
||||
/**
|
||||
* Library for HTML sanitization of arrays.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @licence MIT
|
||||
*/
|
||||
class SanitizerArrayProxy implements ArrayAccess, Iterator, Countable
|
||||
{
|
||||
public function __construct(
|
||||
protected array &$data
|
||||
) {
|
||||
}
|
||||
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return isset($this->data[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return SanitizerProxy::apply($this->data[$offset] ?? null);
|
||||
}
|
||||
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->data[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
unset($this->data[$offset]);
|
||||
}
|
||||
|
||||
public function current(): mixed
|
||||
{
|
||||
return SanitizerProxy::apply(current($this->data));
|
||||
}
|
||||
|
||||
public function key(): mixed
|
||||
{
|
||||
return key($this->data);
|
||||
}
|
||||
|
||||
public function next(): void
|
||||
{
|
||||
next($this->data);
|
||||
}
|
||||
|
||||
public function rewind(): void
|
||||
{
|
||||
reset($this->data);
|
||||
}
|
||||
|
||||
public function valid(): bool
|
||||
{
|
||||
$key = key($this->data);
|
||||
return $key !== null && isset($this->data[$key]);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Libs\Sanitizer;
|
||||
|
||||
use UnitEnum;
|
||||
|
||||
/**
|
||||
* HTML sanitization library.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @licence MIT
|
||||
*/
|
||||
class SanitizerProxy
|
||||
{
|
||||
public function __construct(
|
||||
protected mixed &$data
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to sanitize if is a string, ignore if is a enum, number boolean or null
|
||||
* or in other case, create a proxy.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function apply(mixed $data = null): mixed
|
||||
{
|
||||
if ($data instanceof UnitEnum) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return match (gettype($data)) {
|
||||
'string' => htmlspecialchars($data),
|
||||
'integer', 'double', 'boolean', 'NULL' => $data,
|
||||
'array' => new SanitizerArrayProxy($data),
|
||||
default => new static($data)
|
||||
};
|
||||
}
|
||||
|
||||
public function __set(string $name, mixed $value): void
|
||||
{
|
||||
$this->data->{$name} = $value;
|
||||
}
|
||||
|
||||
public function __unset(string $name): void
|
||||
{
|
||||
unset($this->data->{$name});
|
||||
}
|
||||
|
||||
public function __get(string $key): mixed
|
||||
{
|
||||
return $this->apply($this->data->{$key});
|
||||
}
|
||||
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return isset($this->data->{$key});
|
||||
}
|
||||
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
return $this->apply($this->data->{$name}(...$arguments));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user