Skip to content

Commit

Permalink
Merge pull request #15 from php-etl/feature/utf-8-functions
Browse files Browse the repository at this point in the history
replace standard php functions with utf8-compatible functions
  • Loading branch information
clemzarch authored Jun 28, 2023
2 parents 46aa946 + 13095a8 commit eb3f2bd
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/Capitalize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile(string $input): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
mb_convert_case(mb_substr({$input}, 0, 1), \\MB_CASE_UPPER) . mb_substr({$input}, 1, -1)
)
)
PHP;
}

private function evaluate(array $context, string $input)
{
return !\is_string($input) ?
null :
(
mb_convert_case(mb_substr($input, 0, 1), \MB_CASE_UPPER).mb_substr($input, 1, -1)
);
}
}
41 changes: 41 additions & 0 deletions src/CapitalizeWords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile(string $input): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
mb_convert_case({$input}, \\MB_CASE_TITLE)
)
)
PHP;
}

private function evaluate(array $context, string $input)
{
return !\is_string($input) ?
null :
(
mb_convert_case($input, \MB_CASE_TITLE)
);
}
}
9 changes: 5 additions & 4 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ public function getFunctions(): array
return [
ExpressionFunction::fromPhp('sprintf', 'format'),
ExpressionFunction::fromPhp('trim', 'trim'),
ExpressionFunction::fromPhp('ucfirst', 'capitalize'),
ExpressionFunction::fromPhp('strtolower', 'toLowerCase'),
new Capitalize('capitalize'),
new ToLowerCase('toLowerCase'),
ExpressionFunction::fromPhp('mb_substr', 'search'),
ExpressionFunction::fromPhp('strtoupper', 'toUpperCase'),
new ToUpperCase('toUpperCase'),
ExpressionFunction::fromPhp('number_format', 'formatNumber'),
ExpressionFunction::fromPhp('strpos', 'indexOf'),
ExpressionFunction::fromPhp('str_replace', 'replace'),
ExpressionFunction::fromPhp('strip_tags', 'stripHtml'),
ExpressionFunction::fromPhp('json_decode', 'decode'),
ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'),
ExpressionFunction::fromPhp('ucwords', 'capitalizeWords'),
new CapitalizeWords('capitalizeWords'),
ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'),
ExpressionFunction::fromPhp('explode', 'splitIntoArray'),
new Truncate('truncate'),
new FileName('fileName'),
new DateTime('dateTime'),
new FormatDate('formatDate'),
Expand Down
41 changes: 41 additions & 0 deletions src/ToLowerCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile(string $input): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
mb_convert_case({$input}, \\MB_CASE_LOWER)
)
)
PHP;
}

private function evaluate(array $context, string $input)
{
return !\is_string($input) ?
null :
(
mb_convert_case($input, \MB_CASE_LOWER)
);
}
}
41 changes: 41 additions & 0 deletions src/ToUpperCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile(string $input): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
mb_convert_case({$input}, \\MB_CASE_UPPER)
)
)
PHP;
}

private function evaluate(array $context, string $input)
{
return !\is_string($input) ?
null :
(
mb_convert_case($input, \MB_CASE_UPPER)
);
}
}
41 changes: 41 additions & 0 deletions src/Truncate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile(string $input, string $limit): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
rtrim(mb_substr({$input}, 0, {$limit} - 1)) . '…'
)
)
PHP;
}

private function evaluate(array $context, string $input, int $limit)
{
return !\is_string($input) ?
null :
(
rtrim(mb_substr($input, 0, $limit - 1)).''
);
}
}

0 comments on commit eb3f2bd

Please sign in to comment.