Skip to content

Commit

Permalink
Filters: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 9, 2024
1 parent a02a574 commit 5cee535
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/Latte/Essential/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public function getFilters(): array
'bytes' => [Filters::class, 'bytes'],
'capitalize' => extension_loaded('mbstring')
? [Filters::class, 'capitalize']
: function () { throw new RuntimeException('Filter |capitalize requires mbstring extension.'); },
: fn() => throw new RuntimeException('Filter |capitalize requires mbstring extension.'),
'ceil' => [Filters::class, 'ceil'],
'checkUrl' => [Latte\Runtime\Filters::class, 'safeUrl'],
'clamp' => [Filters::class, 'clamp'],
'dataStream' => [Filters::class, 'dataStream'],
'datastream' => [Filters::class, 'dataStream'],
Expand All @@ -130,17 +131,16 @@ public function getFilters(): array
'first' => [Filters::class, 'first'],
'firstUpper' => extension_loaded('mbstring')
? [Filters::class, 'firstUpper']
: function () { throw new RuntimeException('Filter |firstUpper requires mbstring extension.'); },
: fn() => throw new RuntimeException('Filter |firstUpper requires mbstring extension.'),
'floor' => [Filters::class, 'floor'],
'checkUrl' => [Latte\Runtime\Filters::class, 'safeUrl'],
'implode' => [Filters::class, 'implode'],
'indent' => [Filters::class, 'indent'],
'join' => [Filters::class, 'implode'],
'last' => [Filters::class, 'last'],
'length' => [Filters::class, 'length'],
'lower' => extension_loaded('mbstring')
? [Filters::class, 'lower']
: function () { throw new RuntimeException('Filter |lower requires mbstring extension.'); },
: fn() => throw new RuntimeException('Filter |lower requires mbstring extension.'),
'number' => 'number_format',
'padLeft' => [Filters::class, 'padLeft'],
'padRight' => [Filters::class, 'padRight'],
Expand All @@ -166,10 +166,10 @@ public function getFilters(): array
'truncate' => [Filters::class, 'truncate'],
'upper' => extension_loaded('mbstring')
? [Filters::class, 'upper']
: function () { throw new RuntimeException('Filter |upper requires mbstring extension.'); },
: fn() => throw new RuntimeException('Filter |upper requires mbstring extension.'),
'webalize' => class_exists(Nette\Utils\Strings::class)
? [Nette\Utils\Strings::class, 'webalize']
: function () { throw new RuntimeException('Filter |webalize requires nette/utils package.'); },
: fn() => throw new RuntimeException('Filter |webalize requires nette/utils package.'),
];
}

Expand Down
42 changes: 15 additions & 27 deletions src/Latte/Essential/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,12 @@ public static function date(string|int|\DateTimeInterface|\DateInterval|null $ti
return null;
}

if (!isset($format)) {
$format = Latte\Runtime\Filters::$dateFormat;
}

$format ??= Latte\Runtime\Filters::$dateFormat;
if ($time instanceof \DateInterval) {
return $time->format($format);

} elseif (is_numeric($time)) {
$time = new \DateTime('@' . $time);
$time->setTimeZone(new \DateTimeZone(date_default_timezone_get()));

$time = (new \DateTime)->setTimestamp((int) $time);
} elseif (!$time instanceof \DateTimeInterface) {
$time = new \DateTime($time);
}
Expand All @@ -200,7 +195,7 @@ public static function date(string|int|\DateTimeInterface|\DateInterval|null $ti


/**
* Converts to human readable file size.
* Converts to human-readable file size.
*/
public static function bytes(float $bytes, int $precision = 2): string
{
Expand Down Expand Up @@ -262,10 +257,7 @@ public static function replaceRe(string $subject, string $pattern, string $repla
*/
public static function dataStream(string $data, ?string $type = null): string
{
if ($type === null) {
$type = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
}

$type ??= finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
return 'data:' . ($type ? "$type;" : '') . 'base64,' . base64_encode($data);
}

Expand All @@ -283,15 +275,11 @@ public static function breaklines(string|Stringable|null $s): Html
public static function substring(string|Stringable|null $s, int $start, ?int $length = null): string
{
$s = (string) $s;
if ($length === null) {
$length = self::strLength($s);
}

if (function_exists('mb_substr')) {
return mb_substr($s, $start, $length, 'UTF-8'); // MB is much faster
}

return iconv_substr($s, $start, $length, 'UTF-8');
return match (true) {
extension_loaded('mbstring') => mb_substr($s, $start, $length, 'UTF-8'),
extension_loaded('iconv') => iconv_substr($s, $start, $length, 'UTF-8'),
default => throw new Latte\RuntimeException("Filter |substr requires 'mbstring' or 'iconv' extension."),
};
}


Expand Down Expand Up @@ -422,7 +410,7 @@ public static function padRight($s, int $length, string $append = ' '): string
/**
* Reverses string or array.
*/
public static function reverse(string|array|\Traversable $val, bool $preserveKeys = false): string|array
public static function reverse(string|iterable $val, bool $preserveKeys = false): string|array
{
if (is_array($val)) {
return array_reverse($val, $preserveKeys);
Expand All @@ -437,7 +425,7 @@ public static function reverse(string|array|\Traversable $val, bool $preserveKey
/**
* Chunks items by returning an array of arrays with the given number of items.
*/
public static function batch(array|\Traversable $list, int $length, $rest = null): \Generator
public static function batch(iterable $list, int $length, $rest = null): \Generator
{
$batch = [];
foreach ($list as $key => $value) {
Expand Down Expand Up @@ -524,23 +512,23 @@ public static function even(int $value): bool


/**
* Returns the first item from the array or null if array is empty.
* Returns the first element in an array or character in a string, or null if none.
*/
public static function first(string|array $value): mixed
{
return is_array($value)
? (count($value) ? reset($value) : null)
? ($value[array_key_first($value)] ?? null)
: self::substring($value, 0, 1);
}


/**
* Returns the last item from the array or null if array is empty.
* Returns the last element in an array or character in a string, or null if none.
*/
public static function last(string|array $value): mixed
{
return is_array($value)
? (count($value) ? end($value) : null)
? ($value[array_key_last($value)] ?? null)
: self::substring($value, -1);
}

Expand Down

0 comments on commit 5cee535

Please sign in to comment.