Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Cel\Extension\Core\BinaryOperator\Handler\InOperator;

use Cel\Exception\InternalException;
use Cel\Operator\BinaryOperatorOverloadHandlerInterface;
use Cel\Syntax\Binary\BinaryExpression;
use Cel\Util\OperandUtil;
use Cel\Value\BooleanValue;
use Cel\Value\MapValue;
use Cel\Value\StringValue;
use Cel\Value\Value;
use Override;
use Psl\Iter;

final readonly class StringMapHandler implements BinaryOperatorOverloadHandlerInterface
{
/**
* @param BinaryExpression $expression The binary expression being evaluated.
* @param Value $left The evaluated left operand.
* @param Value $right The evaluated right operand.
*
* @return Value The result of the binary operation.
*
* @throws InternalException If operand type assertion fails.
*/
#[Override]
public function __invoke(BinaryExpression $expression, Value $left, Value $right): Value
{
$left = OperandUtil::assertLeft($left, StringValue::class);
$right = OperandUtil::assertRight($right, MapValue::class);

return new BooleanValue(Iter\contains_key($right->value, $left->value));
}
}
2 changes: 2 additions & 0 deletions src/Extension/Core/BinaryOperator/InOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\MessageListHandler;
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\NullListHandler;
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\StringListHandler;
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\StringMapHandler;
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\TimestampListHandler;
use Cel\Extension\Core\BinaryOperator\Handler\InOperator\UnsignedIntegerListHandler;
use Cel\Operator\BinaryOperatorOverloadInterface;
Expand Down Expand Up @@ -44,5 +45,6 @@ public function getOverloads(): iterable
yield [ValueKind::Message, ValueKind::List] => new MessageListHandler();
yield [ValueKind::Timestamp, ValueKind::List] => new TimestampListHandler();
yield [ValueKind::Duration, ValueKind::List] => new DurationListHandler();
yield [ValueKind::String, ValueKind::Map] => new StringMapHandler();
}
}
19 changes: 19 additions & 0 deletions tests/Runtime/RuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ public static function provideEvaluationCases(): iterable
new BooleanValue(true),
];

yield 'Form: field in answers' => [
'\'field\' in answers',
[
'answers' => [
'field' => 'available',
],
],
new BooleanValue(true),
];
yield 'Form: field not in answers' => [
'\'field\' in answers',
[
'answers' => [
'foo' => 'bar',
],
],
new BooleanValue(false),
];

yield 'RBAC: owner access' => [
'user.id == resource.owner_id',
[
Expand Down
Loading