Skip to content

Commit c3a6fa5

Browse files
committed
feat: improve interigify
1 parent 8e0d33a commit c3a6fa5

2 files changed

Lines changed: 66 additions & 27 deletions

File tree

src/Core/Fake/Faker.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Constructo\Support\Reflective\Factory\Target;
1919
use Constructo\Support\Reflective\Notation;
2020
use Constructo\Support\Set;
21+
use Constructo\Support\Value;
2122
use DateTime;
2223
use Faker\Factory;
2324
use Faker\Generator;
@@ -247,28 +248,9 @@ private function resolveParameters(array $parameters, Set $presets): Set
247248
$fromDefaultValue = $this->ignoreFromDefaultValue
248249
? null
249250
: new FromDefaultValue($this->notation, $this->formatters, $this->locale);
250-
$fromTypeAttributes = new FromTypeAttributes(
251-
$this->notation,
252-
$this->formatters,
253-
$this->locale,
254-
$this->ignoreFromDefaultValue
255-
);
256251
foreach ($parameters as $parameter) {
257252
$field = $this->casedField($parameter);
258-
$generated = (new FromDependency(
259-
$this->notation,
260-
$this->formatters,
261-
$this->locale,
262-
$this->ignoreFromDefaultValue
263-
))
264-
->then(new FromTypeDate($this->notation, $this->formatters, $this->locale))
265-
->then(new FromCollection($this->notation, $this->formatters, $this->locale))
266-
->then(new FromTypeBuiltin($this->notation, $this->formatters, $this->locale))
267-
->then($fromTypeAttributes)
268-
->then(new FromEnum($this->notation, $this->formatters, $this->locale))
269-
->then($fromDefaultValue)
270-
->then(new FromPreset($this->notation, $this->formatters, $this->locale))
271-
->resolve($parameter, $presets);
253+
$generated = $this->generateValue($parameter, $presets, $fromDefaultValue);
272254

273255
if ($generated === null) {
274256
continue;
@@ -288,4 +270,35 @@ private function locale(?string $locale): string
288270
};
289271
return $locale ?? $fallback();
290272
}
273+
274+
/**
275+
* @throws ReflectionException
276+
*/
277+
private function generateValue(
278+
ReflectionParameter $parameter,
279+
Set $presets,
280+
?FromDefaultValue $fromDefaultValue = null,
281+
): ?Value {
282+
return (new FromDependency(
283+
$this->notation,
284+
$this->formatters,
285+
$this->locale,
286+
$this->ignoreFromDefaultValue
287+
))
288+
->then(new FromTypeDate($this->notation, $this->formatters, $this->locale))
289+
->then(new FromCollection($this->notation, $this->formatters, $this->locale))
290+
->then(new FromTypeBuiltin($this->notation, $this->formatters, $this->locale))
291+
->then(
292+
new FromTypeAttributes(
293+
$this->notation,
294+
$this->formatters,
295+
$this->locale,
296+
$this->ignoreFromDefaultValue
297+
)
298+
)
299+
->then(new FromEnum($this->notation, $this->formatters, $this->locale))
300+
->then($fromDefaultValue)
301+
->then(new FromPreset($this->notation, $this->formatters, $this->locale))
302+
->resolve($parameter, $presets);
303+
}
291304
}

src/_/cast.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
use Stringable;
88

9+
use function function_exists;
10+
use function is_array;
11+
use function is_bool;
12+
use function is_float;
13+
use function is_int;
14+
use function is_numeric;
15+
use function is_object;
16+
use function is_scalar;
17+
use function is_string;
18+
use function method_exists;
19+
use function sprintf;
20+
921
if (! function_exists(__NAMESPACE__ . '\arrayify')) {
1022
/**
1123
* @template T of array-key
@@ -15,7 +27,9 @@
1527
*/
1628
function arrayify(mixed $value, array $default = []): array
1729
{
18-
return is_array($value) ? $value : $default;
30+
return is_array($value)
31+
? $value
32+
: $default;
1933
}
2034
}
2135

@@ -33,7 +47,9 @@ function mapify(mixed $data, array $default = []): array
3347
};
3448
$mapping = [];
3549
foreach ($data as $key => $datum) {
36-
$key = is_string($key) ? $key : sprintf('key_%s', $key);
50+
$key = is_string($key)
51+
? $key
52+
: sprintf('key_%s', $key);
3753
$mapping[$key] = $datum;
3854
}
3955
return $mapping;
@@ -56,22 +72,32 @@ function stringify(mixed $value, string $default = ''): string
5672
if (! function_exists(__NAMESPACE__ . '\integerify')) {
5773
function integerify(mixed $value, int $default = 0): int
5874
{
59-
$value = is_numeric($value) ? (int) $value : $value;
60-
return is_int($value) ? $value : $default;
75+
return match (true) {
76+
is_int($value) => $value,
77+
is_numeric($value) => (int) $value,
78+
is_bool($value) => (int) $value,
79+
default => $default,
80+
};
6181
}
6282
}
6383

6484
if (! function_exists(__NAMESPACE__ . '\floatify')) {
6585
function floatify(mixed $value, float $default = 0.0): float
6686
{
67-
$value = is_numeric($value) ? (float) $value : $value;
68-
return is_float($value) ? $value : $default;
87+
$value = is_numeric($value)
88+
? (float) $value
89+
: $value;
90+
return is_float($value)
91+
? $value
92+
: $default;
6993
}
7094
}
7195

7296
if (! function_exists(__NAMESPACE__ . '\boolify')) {
7397
function boolify(mixed $value, bool $default = false): bool
7498
{
75-
return is_bool($value) ? $value : $default;
99+
return is_bool($value)
100+
? $value
101+
: $default;
76102
}
77103
}

0 commit comments

Comments
 (0)