-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathOptionalValueResolver.php
109 lines (92 loc) · 3.63 KB
/
OptionalValueResolver.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Nelmio\Alice\Generator\Resolver\Value\Chainable;
use Faker\Generator as FakerGenerator;
use Nelmio\Alice\Definition\Value\OptionalValue;
use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureInterface;
use Nelmio\Alice\Generator\GenerationContext;
use Nelmio\Alice\Generator\ResolvedFixtureSet;
use Nelmio\Alice\Generator\ResolvedValueWithFixtureSet;
use Nelmio\Alice\Generator\Resolver\Value\ChainableValueResolverInterface;
use Nelmio\Alice\Generator\ValueResolverAwareInterface;
use Nelmio\Alice\Generator\ValueResolverInterface;
use Nelmio\Alice\IsAServiceTrait;
use Nelmio\Alice\Throwable\Exception\Generator\Resolver\ResolverNotFoundExceptionFactory;
use Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException;
use Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueExceptionFactory;
use function mt_rand;
final class OptionalValueResolver implements ChainableValueResolverInterface, ValueResolverAwareInterface
{
use IsAServiceTrait;
/**
* @var ValueResolverInterface
*/
private $resolver;
/**
* @var FakerGenerator
*/
private $faker;
public function __construct(?ValueResolverInterface $resolver = null, ?FakerGenerator $faker = null)
{
$this->resolver = $resolver;
$this->faker = $faker;
}
public function withValueResolver(ValueResolverInterface $resolver): self
{
return new self($resolver, $this->faker);
}
public function canResolve(ValueInterface $value): bool
{
return $value instanceof OptionalValue;
}
/**
* @param OptionalValue $value
*
* @throws UnresolvableValueException
*/
public function resolve(
ValueInterface $value,
FixtureInterface $fixture,
ResolvedFixtureSet $fixtureSet,
array $scope,
GenerationContext $context
): ResolvedValueWithFixtureSet {
if (null === $this->resolver) {
throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
}
$quantifier = $value->getQuantifier();
if ($quantifier instanceof ValueInterface) {
$resolvedSet = $this->resolver->resolve($quantifier, $fixture, $fixtureSet, $scope, $context);
[$quantifier, $fixtureSet] = [$resolvedSet->getValue(), $resolvedSet->getSet()];
if (false === is_int($quantifier) && false === is_string($quantifier)) {
throw UnresolvableValueExceptionFactory::createForInvalidResolvedQuantifierTypeForOptionalValue($value, $quantifier);
}
}
$realValue = $this->resolveRealValue($value, (int) $quantifier);
if ($realValue instanceof ValueInterface) {
return $this->resolver->resolve($realValue, $fixture, $fixtureSet, $scope, $context);
}
return new ResolvedValueWithFixtureSet($realValue, $fixtureSet);
}
/**
* @return ValueInterface|mixed
*/
private function resolveRealValue(ValueInterface $value, int $quantifier)
{
// TODO: keeping mt_rand for BC purposes. The generator should be made
// non-nullable in 4.x and mt_rand usage removed
$random = null !== $this->faker ? $this->faker->numberBetween(0, 99) : random_int(0, 99);
return ($random < $quantifier)
? $value->getFirstMember() // @phpstan-ignore-line
: $value->getSecondMember(); // @phpstan-ignore-line
}
}