Skip to content

Commit

Permalink
Merge pull request #13 from php-etl/feature/transform-into-string
Browse files Browse the repository at this point in the history
Added a new function to transform an integer into a string
  • Loading branch information
gplanchat authored Jun 23, 2023
2 parents 144504a + 5fa1755 commit 46aa946
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-6.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 6
on: push
jobs:
phpstan:
phpstan6:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-7.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 7
on: push
jobs:
phpstan:
phpstan7:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-8.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 8
on: push
jobs:
phpstan:
phpstan8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 4 additions & 2 deletions src/AsFloat.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

Expand All @@ -18,7 +20,7 @@ public function __construct($name)
private function compile(string $value): string
{
return <<<PHP
((float) $value)
((float) {$value})
PHP;
}

Expand Down
6 changes: 4 additions & 2 deletions src/AsInteger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

Expand All @@ -18,7 +20,7 @@ public function __construct($name)
private function compile(string $value): string
{
return <<<PHP
((int) $value)
((int) {$value})
PHP;
}

Expand Down
31 changes: 31 additions & 0 deletions src/AsString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class AsString 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
((string) {$value})
PHP;
}

private function evaluate(array $context, int|float|string $value): string
{
return (string) $value;
}
}
2 changes: 1 addition & 1 deletion src/ConvertCharCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($name)
private function compile(string $text, string $sourceCharCode, string $destinationCharCode): string
{
return <<<"PHP"
iconv($sourceCharCode, $destinationCharCode, $text)
iconv({$sourceCharCode}, {$destinationCharCode}, {$text})
PHP;
}

Expand Down
1 change: 1 addition & 0 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function getFunctions(): array
new ConvertCharCode('convertCharCode'),
new AsFloat('asFloat'),
new AsInteger('asInteger'),
new AsString('asString'),
];
}
}

0 comments on commit 46aa946

Please sign in to comment.