-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTyped.php
More file actions
60 lines (50 loc) · 1.53 KB
/
Copy pathTyped.php
File metadata and controls
60 lines (50 loc) · 1.53 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
<?php
declare(strict_types=1);
namespace Respect\Data\Collections;
use Respect\Data\EntityFactory;
use function is_array;
use function is_string;
final class Typed extends Collection
{
/**
* @param list<Collection> $with
* @param array<scalar, mixed>|scalar|null $filter
*/
public function __construct(
string $name,
public private(set) readonly string $type = '',
array $with = [],
array|int|float|string|bool|null $filter = null,
bool $required = false,
) {
parent::__construct($name, $with, $filter, $required);
}
/**
* @param object|array<string, mixed> $row
*
* @return class-string
*/
public function resolveEntityClass(EntityFactory $factory, object|array $row): string
{
$name = is_array($row) ? ($row[$this->type] ?? null) : $factory->get($row, $this->type);
return $factory->resolveClass(is_string($name) ? $name : (string) $this->name);
}
/**
* @param list<Collection> $with
*
* @return array<string, mixed>
*/
protected function deriveArgs(
array $with = [],
array|int|float|string|bool|null $filter = null,
bool|null $required = null,
): array {
$base = parent::deriveArgs($with, $filter, $required);
return ['type' => $this->type] + $base;
}
/** @param array<int, string> $arguments */
public static function __callStatic(string $name, array $arguments): static
{
return new static($name, ...$arguments);
}
}