Skip to content

Commit

Permalink
Added a new Empty function
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Oct 2, 2023
1 parent 8be9480 commit f8d45fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ArrayExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getFunctions(): array
new List_('list'),
new FilterList('filterList'),
new MapValues('mapValues'),
new NotEmpty('notEmpty')
];
}
}
31 changes: 31 additions & 0 deletions src/NotEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ArrayExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

private function compile()
{
return <<<PHP
fn (\$carry, \$item) => \$carry && \$item !== null
PHP;
}

private function evaluate(array $context, string $separator): callable
{
return fn ($carry, $item) => $carry && $item !== null;
}
}

0 comments on commit f8d45fa

Please sign in to comment.