Skip to content
Open
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
101 changes: 87 additions & 14 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,103 @@

readonly class Configuration
{
/**
* Create a new parser/refiner configuration.
*
* @param array<int, Parser> $parsers
* @param array<int, Refiner> $refiners
*/
public static function make(array $parsers = [], array $refiners = []): self
{
return new self($parsers, $refiners);
}

/**
* Create a parser/refiner configuration.
*
* @param array<int, Parser> $parsers
* @param array<int, Refiner> $refiners
*/
public function __construct(
public readonly array $parsers = [],
protected readonly array $parsers = [],

public readonly array $refiners = [],
protected readonly array $refiners = [],
) {}

/**
* Get the configured parsers.
*
* @return array<int, Parser>
*/
public function parsers(): array
{
return $this->parsers;
}

/**
* Get the configured refiners.
*
* @return array<int, Refiner>
*/
public function refiners(): array
{
return $this->refiners;
}

/**
* Determine whether the configuration has the given parser.
*
* @param class-string<Parser> $parser
*/
public function hasParser(string $parser): bool
{
foreach ($this->parsers as $configuredParser) {
if ($configuredParser instanceof $parser) {
return true;
}
}

return false;
}

/**
* Determine whether the configuration has the given refiner.
*
* @param class-string<Refiner> $refiner
*/
public function hasRefiner(string $refiner): bool
{
foreach ($this->refiners as $configuredRefiner) {
if ($configuredRefiner instanceof $refiner) {
return true;
}
}

return false;
}

/**
* Return a configuration with the given parser added.
*/
public function withParser(Parser $parser, bool $prepend = false): self
public function addParser(Parser $parser): self
{
return new self(
$prepend ? [$parser, ...$this->parsers] : [...$this->parsers, $parser],
$this->refiners,
);
return new self([...$this->parsers, $parser], $this->refiners);
}

/**
* Return a configuration with the given parser added to the beginning.
*/
public function prependParser(Parser $parser): self
{
return new self([$parser, ...$this->parsers], $this->refiners);
}

/**
* Return a configuration without parsers matching the given class name.
*
* @param class-string<Parser> $parser
*/
public function withoutParser(string $parser): self
public function removeParser(string $parser): self
{
return new self(
array_values(array_filter(
Expand All @@ -46,20 +114,25 @@ public function withoutParser(string $parser): self
/**
* Return a configuration with the given refiner added.
*/
public function withRefiner(Refiner $refiner, bool $prepend = false): self
public function addRefiner(Refiner $refiner): self
{
return new self(
$this->parsers,
$prepend ? [$refiner, ...$this->refiners] : [...$this->refiners, $refiner],
);
return new self($this->parsers, [...$this->refiners, $refiner]);
}

/**
* Return a configuration with the given refiner added to the beginning.
*/
public function prependRefiner(Refiner $refiner): self
{
return new self($this->parsers, [$refiner, ...$this->refiners]);
}

/**
* Return a configuration without refiners matching the given class name.
*
* @param class-string<Refiner> $refiner
*/
public function withoutRefiner(string $refiner): self
public function removeRefiner(string $refiner): self
{
return new self(
$this->parsers,
Expand Down
24 changes: 16 additions & 8 deletions src/ConfiguredChronoEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function parse(string $text, Reference $reference, Options $options): arr
{
$results = array_merge(...array_map(
fn (Parser $parser) => $parser->parse($text, $reference, $options),
$this->configuration->parsers,
$this->configuration->parsers(),
));

$results = $this->attachReference($results, $reference);
Expand All @@ -35,7 +35,7 @@ public function parse(string $text, Reference $reference, Options $options): arr

$results = array_map(fn (array $result): ParsedResult => $result[0], $results);

foreach ($this->configuration->refiners as $refiner) {
foreach ($this->configuration->refiners() as $refiner) {
$results = $this->attachReference(
$refiner->refine($text, $results, $reference, $options),
$reference,
Expand All @@ -51,8 +51,8 @@ public function parse(string $text, Reference $reference, Options $options): arr
public function clone(): self
{
return $this->newInstance(new Configuration(
parsers: [...$this->configuration->parsers],
refiners: [...$this->configuration->refiners],
parsers: [...$this->configuration->parsers()],
refiners: [...$this->configuration->refiners()],
));
}

Expand All @@ -61,7 +61,11 @@ public function clone(): self
*/
public function withParser(Parser $parser, bool $prepend = false): self
{
return $this->newInstance($this->configuration->withParser($parser, $prepend));
return $this->newInstance(
$prepend
? $this->configuration->prependParser($parser)
: $this->configuration->addParser($parser),
);
}

/**
Expand All @@ -71,15 +75,19 @@ public function withParser(Parser $parser, bool $prepend = false): self
*/
public function withoutParser(string $parser): self
{
return $this->newInstance($this->configuration->withoutParser($parser));
return $this->newInstance($this->configuration->removeParser($parser));
}

/**
* Return an engine instance with the given refiner added.
*/
public function withRefiner(Refiner $refiner, bool $prepend = false): self
{
return $this->newInstance($this->configuration->withRefiner($refiner, $prepend));
return $this->newInstance(
$prepend
? $this->configuration->prependRefiner($refiner)
: $this->configuration->addRefiner($refiner),
);
}

/**
Expand All @@ -89,7 +97,7 @@ public function withRefiner(Refiner $refiner, bool $prepend = false): self
*/
public function withoutRefiner(string $refiner): self
{
return $this->newInstance($this->configuration->withoutRefiner($refiner));
return $this->newInstance($this->configuration->removeRefiner($refiner));
}

/**
Expand Down
Loading
Loading