Skip to content

Commit

Permalink
Allow unregistering adapters in the EvaluatorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Ben87 committed Mar 28, 2024
1 parent 7cbde62 commit 7768202
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
73 changes: 59 additions & 14 deletions src/EvaluatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
final class EvaluatorBuilder
{
/**
* @var list<AdapterInterface>
* @var array<class-string<AdapterInterface>, \Closure(ExpressionLanguage): AdapterInterface>
*/
private array $adapters = [];
private array $adapterFactories = [];

private string $culture = 'en_US';

Expand All @@ -34,11 +34,61 @@ final class EvaluatorBuilder
public function __construct()
{
$this->inflector = InflectorFactory::create()->build();
$this->registerAdapterFactory(
ConstantAdapter::class,
static fn(ExpressionLanguage $expressionLanguage): AdapterInterface
=> new ConstantAdapter($expressionLanguage),
);
$this->registerAdapterFactory(
DateTimeAdapter::class,
static fn(ExpressionLanguage $expressionLanguage): AdapterInterface
=> new DateTimeAdapter($expressionLanguage),
);
$this->registerAdapterFactory(
EnumAdapter::class,
static fn(ExpressionLanguage $expressionLanguage): AdapterInterface
=> new EnumAdapter($expressionLanguage),
);
$this->registerAdapterFactory(
FactoryAdapter::class,
static fn(ExpressionLanguage $expressionLanguage): AdapterInterface
=> new FactoryAdapter($expressionLanguage),
);
$this->registerAdapterFactory(JsonAdapter::class, static fn(): AdapterInterface => new JsonAdapter());
$this->registerAdapterFactory(NthAdapter::class, static fn(): AdapterInterface => new NthAdapter());
$this->registerAdapterFactory(ScalarAdapter::class, static fn(): AdapterInterface => new ScalarAdapter());
$this->registerAdapterFactory(UnescapeAdapter::class, static fn(): AdapterInterface => new UnescapeAdapter());
}

/**
* @deprecated
*
* Use @see self::registerAdapterFactory() instead.
*/
public function registerAdapter(AdapterInterface $adapter): self
{
$this->adapters[] = $adapter;
$this->adapterFactories[$adapter::class] = static fn(): AdapterInterface => $adapter;

return $this;
}

/**
* @param class-string<AdapterInterface> $class
* @param \Closure(ExpressionLanguage): AdapterInterface $factory
*/
public function registerAdapterFactory(string $class, \Closure $factory): self
{
$this->adapterFactories[$class] = $factory;

return $this;
}

/**
* @param class-string<AdapterInterface> $class
*/
public function unregisterAdapterFactory(string $class): self
{
unset($this->adapterFactories[$class]);

return $this;
}
Expand Down Expand Up @@ -72,17 +122,12 @@ public function build(): Evaluator
);

return new Evaluator(
[
new ConstantAdapter($expressionLanguage),
new DateTimeAdapter($expressionLanguage),
new EnumAdapter($expressionLanguage),
new FactoryAdapter($expressionLanguage),
new JsonAdapter(),
new NthAdapter(),
new ScalarAdapter(),
new UnescapeAdapter(),
...$this->adapters,
],
array_values(
array_map(
static fn(\Closure $factory): AdapterInterface => $factory($expressionLanguage),
$this->adapterFactories,
),
),
);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Builder/EvaluatorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(mixed $value): array
$builder->withCulture($culture);
$builder->withFactoryNamespace($factoryNamespace);
$builder->withInflector($inflector);
$builder->registerAdapter($adapter);
$builder->registerAdapterFactory($adapter::class, static fn(): AdapterInterface => $adapter);

$evaluate = $builder->build();

Expand Down

0 comments on commit 7768202

Please sign in to comment.