ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 if ($this->todo === '') {
42 return 0;
43 }
44 return ord($this->todo[0]);
45 }
46
47 public function accept(): Result
48 {
49 return new Ok((new self(
50 substr($this->todo, 1),
51 $this->accepted()
52 ))->push([new Character(substr($this->todo, 0, 1))]));
53 }
54
55 public function push(array $values): self
56 {
57 return new self(
59 array_merge($this->accepted, $values)
60 );
61 }
62
63 public function reject(): Result
64 {
65 return new Error('Rejected.');
66 }
67
68 public function accepted(): array
69 {
70 return $this->accepted;
71 }
72
73 public function done(): bool
74 {
75 return '' === $this->todo;
76 }
77
78 public function onlyTodo(): self
79 {
80 return new self($this->todo);
81 }
82
92 public function transform(Closure $transform): Result
93 {
94 $string = '';
95 foreach ($this->accepted as $data) {
96 if (!$data instanceof Character) {
97 return $transform($this->accepted);
98 }
99 $string .= $data->value();
100 }
101
102 return $transform($string);
103 }
104}
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