-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute.php
More file actions
94 lines (85 loc) · 2.48 KB
/
Copy pathAttribute.php
File metadata and controls
94 lines (85 loc) · 2.48 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
namespace TinyBlocks\Http;
/**
* Typed wrapper around a scalar or array value extracted from an HTTP message.
*
* Provides coercion methods that convert the wrapped value to a requested primitive type,
* falling back to a safe zero-value when conversion is not possible.
*/
final readonly class Attribute
{
private function __construct(private mixed $value)
{
}
/**
* Creates an Attribute wrapping the given value.
*
* @param mixed $value The value carried by the Attribute.
* @return Attribute An Attribute wrapping the supplied value.
*/
public static function from(mixed $value): Attribute
{
return new Attribute(value: $value);
}
/**
* Returns the Attribute as an array.
*
* @return array<int|string, mixed> The wrapped value when it is an array, otherwise an empty array.
*/
public function toArray(): array
{
return match (true) {
is_array($this->value) => $this->value,
default => []
};
}
/**
* Returns the Attribute as a float.
*
* @return float The wrapped value coerced to a float, or <code>0.00</code> when it is not scalar.
*/
public function toFloat(): float
{
return match (true) {
is_scalar($this->value) => (float)$this->value,
default => 0.00
};
}
/**
* Returns the Attribute as a string.
*
* @return string The wrapped value coerced to a string, or an empty string when it is not scalar.
*/
public function toString(): string
{
return match (true) {
is_scalar($this->value) => (string)$this->value,
default => ''
};
}
/**
* Returns the Attribute as a boolean.
*
* @return bool The wrapped value coerced to a boolean, or <code>false</code> when it is not scalar.
*/
public function toBoolean(): bool
{
return match (true) {
is_scalar($this->value) => (bool)$this->value,
default => false
};
}
/**
* Returns the Attribute as an integer.
*
* @return int The wrapped value coerced to an integer, or <code>0</code> when it is not scalar.
*/
public function toInteger(): int
{
return match (true) {
is_scalar($this->value) => (int)$this->value,
default => 0
};
}
}