ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Intermediate.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use Closure;
27
29{
30 private string $todo;
31 private array $accepted;
32
33 public function __construct(string $todo, array $accepted = [])
34 {
35 $this->todo = $todo;
36 $this->accepted = $accepted;
37 }
38
39 public function value(): int
40 {
41 return ord($this->todo);
42 }
43
44 public function accept(): Result
45 {
46 return new Ok((new self(
47 substr($this->todo, 1),
48 $this->accepted()
49 ))->push([new Character(substr($this->todo, 0, 1))]));
50 }
51
52 public function push(array $values): self
53 {
54 return new self(
56 array_merge($this->accepted, $values)
57 );
58 }
59
60 public function reject(): Result
61 {
62 return new Error('Rejected.');
63 }
64
65 public function accepted(): array
66 {
67 return $this->accepted;
68 }
69
70 public function done(): bool
71 {
72 return '' === $this->todo;
73 }
74
75 public function onlyTodo(): self
76 {
77 return new self($this->todo);
78 }
79
89 public function transform(Closure $transform): Result
90 {
91 $string = '';
92 foreach ($this->accepted as $data) {
93 if (!$data instanceof Character) {
94 return $transform($this->accepted);
95 }
96 $string .= $data->value();
97 }
98
99 return $transform($string);
100 }
101}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
Class for internal usage of Intermediate.
Definition: Character.php:27
transform(Closure $transform)
Calls the given closure with all accepted values.
__construct(string $todo, array $accepted=[])
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29