Skip to content

Commit 778c359

Browse files
committed
Added experimental class RequiredArguments
1 parent 0c593df commit 778c359

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Request/RequiredArguments.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Microwin7\PHPUtils\Request;
4+
5+
use Microwin7\PHPUtils\Exceptions\RequiredArgumentMissingException;
6+
7+
class RequiredArguments
8+
{
9+
private array $arguments;
10+
public function __construct()
11+
{
12+
}
13+
public function __get($name): string|array
14+
{
15+
return $this->arguments[$name];
16+
}
17+
public static function validate(array $requiredArguments, ?array $where): static
18+
{
19+
if ((!empty($requiredArguments) && !empty($where)) && count($requiredArguments) > count($where)) {
20+
$class = new static();
21+
foreach ($requiredArguments as $argument) {
22+
$class->with($argument, $where[$argument] ?? new RequiredArgumentMissingException($requiredArguments));
23+
}
24+
} else throw new RequiredArgumentMissingException($requiredArguments);
25+
return $class;
26+
}
27+
private function with(string $property, string|array $value): void
28+
{
29+
$this->arguments[$property] = $value;
30+
}
31+
}

0 commit comments

Comments
 (0)