Skip to content

Feature/slugify #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
PHP;
}

private function evaluate(array $context, int|float|string $value): string
private function evaluate(array $context, float|int|string $value): string

Check failure on line 27 in src/AsString.php

View workflow job for this annotation

GitHub Actions / phpstan7

Method Kiboko\Component\StringExpressionLanguage\AsString::evaluate() has parameter $context with no value type specified in iterable type array.

Check failure on line 27 in src/AsString.php

View workflow job for this annotation

GitHub Actions / phpstan8

Method Kiboko\Component\StringExpressionLanguage\AsString::evaluate() has parameter $context with no value type specified in iterable type array.

Check failure on line 27 in src/AsString.php

View workflow job for this annotation

GitHub Actions / phpstan6

Method Kiboko\Component\StringExpressionLanguage\AsString::evaluate() has parameter $context with no value type specified in iterable type array.
{
return (string) $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function compile(string $date, string $format, string $timezone = 'null'
PHP;
}

private function evaluate(array $context, string $date, string $format, string $timezone = null)
private function evaluate(array $context, string $date, string $format, ?string $timezone = null)
{
return \DateTimeImmutable::createFromFormat($format, $date, null !== $timezone ? new \DateTimeZone($timezone) : null);
}
Expand Down
48 changes: 48 additions & 0 deletions src/FileExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class FileExtension extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $value): string
{
return <<<PHP
(function () use (\$input) : ?string {
\$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'pdf', 'mp4', 'webm', 'mp3'];

\$extension = \\pathinfo({$value})['extension'] ?? null;
if (!\\in_array(\$extension, \$validExtensions, true)) {
return null;
}

return \$extension;
})()
PHP;
}

private function evaluate(array $context, string $file): ?string
{
$validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg'];

$extension = pathinfo($file)['extension'] ?? null;

if (!\in_array($extension, $validExtensions, true)) {
return null;
}

return $extension;
}
}
4 changes: 2 additions & 2 deletions src/Now.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public function __construct($name)
);
}

private function compile(string $timezone = null): string
private function compile(?string $timezone = null): string
{
return <<<PHP
(new \\DateTime('now', {$timezone} !== null ? new \\DateTimeZone({$timezone}) : null))
PHP;
}

private function evaluate(array $context, string $timezone = null)
private function evaluate(array $context, ?string $timezone = null)
{
return new \DateTime('now', null !== $timezone ? new \DateTimeZone($timezone) : null);
}
Expand Down
55 changes: 55 additions & 0 deletions src/Slugify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class Slugify extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $value, string $divider = '-'): string
{
return <<<PHP
(function () use (\$input) : string {
\$text = preg_replace('~[^\\pL\\d]+~u', {$divider}, {$value});
\$text = iconv('utf-8', 'us-ascii//TRANSLIT', \$text);
\$text = preg_replace('~[^-\\w]+~', '', \$text);
\$text = trim(\$text, {$divider});
\$text = preg_replace('~-+~', {$divider}, \$text);
\$text = strtolower(\$text);

if (strlen(\$text) <= 0) {
return '';
}

return \$text;
})()
PHP;
}

private function evaluate(array $context, string $value, string $divider = '-'): string
{
$text = preg_replace('~[^\pL\d]+~u', $divider, $value);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^\-\w]+~', '', $text);
$text = trim($text, $divider);
$text = preg_replace('~-+~', $divider, $text);
$text = strtolower($text);

if (\strlen($text) <= 0) {
return 'n-a';
}

return $text;
}
}
2 changes: 2 additions & 0 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function getFunctions(): array
new AsFloat('asFloat'),
new AsInteger('asInteger'),
new AsString('asString'),
new Slugify('slugify'),
new FileExtension('fileExtension'),
];
}
}
Loading