-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayDemo.php
More file actions
70 lines (59 loc) · 1.64 KB
/
ArrayDemo.php
File metadata and controls
70 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use Ray\Di\Injector;
use Ray\InputQuery\Attribute\Input;
use Ray\InputQuery\InputQuery;
// User input class
final class User
{
public function __construct(
#[Input]
public readonly string $id,
#[Input]
public readonly string $name
) {
}
}
// Controller with array handling
final class UserController
{
public function listUsers(
#[Input(item: User::class)]
array $users
): void {
echo "Array of users:\n";
foreach ($users as $index => $user) {
echo " [$index] ID: {$user->id}, Name: {$user->name}\n";
}
}
public function listUsersAsArrayObject(
#[Input(item: User::class)]
ArrayObject $users
): void {
echo "\nArrayObject of users:\n";
foreach ($users as $index => $user) {
echo " [$index] ID: {$user->id}, Name: {$user->name}\n";
}
}
}
// Demo
$injector = new Injector();
$inputQuery = new InputQuery($injector);
// Sample query data (like from $_POST)
$query = [
'users' => [
['id' => '1', 'name' => 'jingu'],
['id' => '2', 'name' => 'horikawa'],
['id' => '3', 'name' => 'tanaka']
]
];
$controller = new UserController();
// Array example
$method = new ReflectionMethod($controller, 'listUsers');
$args = $inputQuery->getArguments($method, $query);
$controller->listUsers(...$args);
// ArrayObject example
$method = new ReflectionMethod($controller, 'listUsersAsArrayObject');
$args = $inputQuery->getArguments($method, $query);
$controller->listUsersAsArrayObject(...$args);