ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
Request.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ILIAS\Data\URI;
26 
27 class Request implements RequestInterface
28 {
29  protected Verb $verb;
30  protected URI $base_url;
31  protected array $arguments = [];
32 
33  public function __construct(
34  URI $base_url,
35  Verb $verb
36  ) {
37  $this->base_url = $base_url;
38  $this->verb = $verb;
39  }
40 
41  public function verb(): Verb
42  {
43  return $this->verb;
44  }
45 
46  public function baseURL(): URI
47  {
48  return $this->base_url;
49  }
50 
51  public function withArgument(Argument $key, string $value): RequestInterface
52  {
53  $clone = clone $this;
54  $clone->arguments[$key->value] = $value;
55  return $clone;
56  }
57 
58  public function argumentValue(Argument $argument): string
59  {
60  return $this->arguments[$argument->value] ?? '';
61  }
62 
63  public function hasArgument(Argument $argument): bool
64  {
65  return array_key_exists($argument->value, $this->arguments);
66  }
72  public function hasCorrectArguments(
73  array $required,
74  array $optional,
75  array $exclusive
76  ): bool {
77  foreach ($exclusive as $argument) {
78  if ($this->hasArgument($argument)) {
79  return $this->countArguments() === 1;
80  }
81  }
82  foreach ($required as $argument) {
83  if (!$this->hasArgument($argument)) {
84  return false;
85  }
86  }
87  $expected_count = count($required);
88  foreach ($optional as $argument) {
89  if ($this->hasArgument($argument)) {
90  $expected_count += 1;
91  }
92  }
93  return $this->countArguments() === $expected_count;
94  }
95 
96  protected function countArguments(): int
97  {
98  return count($this->arguments);
99  }
100 
101  public function argumentKeys(): \Generator
102  {
103  foreach ($this->arguments as $key => $value) {
104  yield Argument::from($key);
105  }
106  }
107 }
withArgument(Argument $key, string $value)
Definition: Request.php:51
__construct(URI $base_url, Verb $verb)
Definition: Request.php:33
hasCorrectArguments(array $required, array $optional, array $exclusive)
Definition: Request.php:72