ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Intermediate.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 use 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 }
transform(Closure $transform)
Calls the given closure with all accepted values.
__construct(string $todo, array $accepted=[])
Class for internal usage of Intermediate.
Definition: Character.php:26
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30