Skip to content

Commit 172f084

Browse files
committed
cs
1 parent 2f76173 commit 172f084

9 files changed

Lines changed: 65 additions & 40 deletions

src/Duration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ private function __construct(
3434
}
3535

3636
public static function from(
37-
mixed $data
37+
mixed $data,
38+
mixed ...$params
3839
): Duration {
3940
if ($data instanceof self) {
4041
return $data;

src/ExtractableTraits/ArrayExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ abstract public function __construct(
1919
);
2020

2121
final public static function from(
22-
mixed $data
22+
mixed $data,
23+
mixed ...$params
2324
): self {
2425
if ($data instanceof self) {
2526
return $data;

src/ExtractableTraits/EnumExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ abstract public static function get(
2323
* @return static
2424
*/
2525
final public static function from(
26-
mixed $data
26+
mixed $data,
27+
mixed ...$params
2728
): static {
2829
if ($data instanceof self) {
2930
return $data;

src/ExtractableTraits/ExtractableTrait.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ trait ExtractableTrait
1313
{
1414

1515
abstract public static function from(
16-
mixed $data
16+
mixed $data,
17+
mixed ...$params
1718
): self;
1819

1920
/**
@@ -23,7 +24,8 @@ abstract public static function from(
2324
*/
2425
public static function extract(
2526
array | \ArrayAccess $data,
26-
string | int $key
27+
string | int $key,
28+
mixed ...$params
2729
): static {
2830
$value = ExtractableHelpers::extractValue($data, $key);
2931

@@ -32,7 +34,7 @@ public static function extract(
3234
}
3335

3436
try {
35-
return self::from($value);
37+
return self::from($value, ...$params);
3638
} catch (InvalidTypeException $e) {
3739
throw $e->wrap($key);
3840
}
@@ -43,15 +45,16 @@ public static function extract(
4345
*/
4446
public static function fromOrNull(
4547
mixed $value,
46-
bool $getNullIfInvalid = false
48+
bool $getNullIfInvalid = false,
49+
mixed ...$params
4750
): self | null
4851
{
4952
if ($value === null) {
5053
return null;
5154
}
5255

5356
try {
54-
return self::from($value);
57+
return self::from($value, ...$params);
5558
} catch (InvalidTypeException $e) {
5659
if ($getNullIfInvalid) {
5760
return null;
@@ -68,7 +71,8 @@ public static function fromOrNull(
6871
public static function extractOrNull(
6972
array | \ArrayAccess $data,
7073
string | int $key,
71-
bool $nullIfInvalid = false
74+
bool $nullIfInvalid = false,
75+
mixed ...$params
7276
): self | null
7377
{
7478
if (!\is_array($data)) {
@@ -86,7 +90,8 @@ public static function extractOrNull(
8690
return self::tryToExtract(
8791
$data,
8892
$key,
89-
$nullIfInvalid
93+
$nullIfInvalid,
94+
...$params
9095
);
9196
}
9297

@@ -194,11 +199,12 @@ public static function extractArrayOfSkipInvalid(
194199
private static function tryToExtract(
195200
mixed $data,
196201
string | int $key,
197-
bool $nullIfInvalid
202+
bool $nullIfInvalid,
203+
mixed ...$params
198204
): self | null
199205
{
200206
try {
201-
return self::extract($data, $key);
207+
return self::extract($data, $key, ...$params);
202208
} catch (InvalidTypeException $e) {
203209
if ($nullIfInvalid) {
204210
return null;

src/ExtractableTraits/FloatExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;

src/ExtractableTraits/IntExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;

src/ExtractableTraits/StringExtractableTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ abstract public function __construct(
1616
);
1717

1818
final public static function from(
19-
mixed $data
19+
mixed $data,
20+
mixed ...$params
2021
): self {
2122
if ($data instanceof self) {
2223
return $data;

src/JsonString.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ private function __construct(
3232
* @throws \SmartEmailing\Types\InvalidTypeException
3333
*/
3434
public static function from(
35-
mixed $data
35+
mixed $data,
36+
mixed ...$params
3637
): JsonString
3738
{
3839
if ($data instanceof self) {

tests/DurationTest.phpt

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ final class DurationTest extends TestCase
2424

2525
Assert::throws(
2626
static function (): void {
27-
Duration::from([
28-
'value' => 0,
29-
'unit' => 'week',
30-
]);
27+
Duration::from(
28+
[
29+
'value' => 0,
30+
'unit' => 'week',
31+
]
32+
);
3133
},
3234
InvalidTypeException::class
3335
);
@@ -66,10 +68,12 @@ final class DurationTest extends TestCase
6668

6769
public function testCreate(): void
6870
{
69-
Duration::from([
70-
'value' => 1,
71-
'unit' => TimeUnit::HOURS,
72-
]);
71+
Duration::from(
72+
[
73+
'value' => 1,
74+
'unit' => TimeUnit::HOURS,
75+
]
76+
);
7377

7478
Duration::extract([
7579
'duration' => [
@@ -87,21 +91,25 @@ final class DurationTest extends TestCase
8791

8892
public function testGetUnit(): void
8993
{
90-
$duration = Duration::from([
91-
'value' => 1,
92-
'unit' => TimeUnit::YEARS,
93-
]);
94+
$duration = Duration::from(
95+
[
96+
'value' => 1,
97+
'unit' => TimeUnit::YEARS,
98+
]
99+
);
94100

95101
Assert::type(Duration::class, $duration);
96102
Assert::type(TimeUnit::class, $duration->getUnit());
97103
}
98104

99105
public function testGetValue(): void
100106
{
101-
$duration = Duration::from([
102-
'value' => 1,
103-
'unit' => TimeUnit::DAYS,
104-
]);
107+
$duration = Duration::from(
108+
[
109+
'value' => 1,
110+
'unit' => TimeUnit::DAYS,
111+
]
112+
);
105113

106114
Assert::type(Duration::class, $duration);
107115
Assert::type('int', $duration->getValue());
@@ -110,17 +118,21 @@ final class DurationTest extends TestCase
110118

111119
public function testLengthInSeconds(): void
112120
{
113-
$duration = Duration::from([
114-
'value' => 3,
115-
'unit' => TimeUnit::HOURS,
116-
]);
121+
$duration = Duration::from(
122+
[
123+
'value' => 3,
124+
'unit' => TimeUnit::HOURS,
125+
]
126+
);
117127
Assert::type(Duration::class, $duration);
118128
Assert::equal(10800, $duration->getLengthInSeconds());
119129

120-
$duration = Duration::from([
121-
'value' => 10,
122-
'unit' => TimeUnit::MINUTES,
123-
]);
130+
$duration = Duration::from(
131+
[
132+
'value' => 10,
133+
'unit' => TimeUnit::MINUTES,
134+
]
135+
);
124136
Assert::type(Duration::class, $duration);
125137
Assert::equal(600, $duration->getLengthInSeconds());
126138
}

0 commit comments

Comments
 (0)