-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCasts.php
47 lines (38 loc) · 993 Bytes
/
Casts.php
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
<?php
/*
* This file is part of fab2s/dt0.
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/
namespace fab2s\Dt0\Attribute;
use Attribute;
#[Attribute(Attribute::TARGET_CLASS)]
class Casts
{
/**
* @var array<string, Cast>
*/
protected array $casters = [];
public function __construct(
Cast ...$casters,
) {
foreach ($casters as $name => $caster) {
if (is_int($name)) {
if (! $caster->propName) {
continue;
}
$name = $caster->propName;
}
$this->casters[$name] = $caster;
}
}
public function hasCast($name): bool
{
return isset($this->casters[$name]);
}
public function getCast($name): ?Cast
{
return $this->casters[$name] ?? null;
}
}