/** * {@inheritdoc} * * @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); list($quantifier, $fixtureSet) = [$resolvedSet->getValue(), $resolvedSet->getSet()]; if (false === is_int($quantifier) && false === is_string($quantifier)) { throw UnresolvableValueExceptionFactory::createForInvalidResolvedQuantifierTypeForOptionalValue($value, $quantifier); } } $realValue = mt_rand(0, 100) <= $quantifier ? $value->getFirstMember() : $value->getSecondMember(); if ($realValue instanceof ValueInterface) { return $this->resolver->resolve($realValue, $fixture, $fixtureSet, $scope, $context); } return new ResolvedValueWithFixtureSet($realValue, $fixtureSet); }
/** * @param FixtureInterface $scope * @param FlagBag $flags * @param mixed|ValueInterface $value * * @throws InvalidScopeException * * @return ValueInterface */ private function generateValue(FixtureInterface $scope, FlagBag $flags, $value) : ValueInterface { $uniqueId = sprintf('%s#%s', $scope->getClassName(), $flags->getKey()); if ('temporary_id' === substr($scope->getId(), 0, 12)) { throw DenormalizerExceptionFactory::createForInvalidScopeForUniqueValue(); } if ($value instanceof DynamicArrayValue) { $uniqueId = uniqid($uniqueId . '::', true); return new DynamicArrayValue($value->getQuantifier(), new UniqueValue($uniqueId, $value->getElement())); } if ($value instanceof ArrayValue) { $uniqueId = uniqid($uniqueId . '::', true); $elements = $value->getValue(); foreach ($elements as $key => $element) { // The key must be the same for each argument: the unique ID is bound to the array, not the argument // number. $elements[$key] = new UniqueValue($uniqueId, $element); } return new ArrayValue($elements); } return new UniqueValue($uniqueId, $value); }
/** * {@inheritdoc} * * @param DynamicArrayValue $value */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $this->checkResolver(__METHOD__); $quantifier = $value->getQuantifier(); if ($quantifier instanceof ValueInterface) { $result = $this->resolver->resolve($quantifier, $fixture, $fixtureSet, $scope, $context); list($quantifier, $fixtureSet) = [$result->getValue(), $result->getSet()]; } if ($quantifier < 0) { throw InvalidArgumentExceptionFactory::createForInvalidDynamicArrayQuantifier($fixture, $quantifier); } $element = $value->getElement(); if (false === $element instanceof ValueInterface) { $array = array_fill(0, $quantifier, $element); return new ResolvedValueWithFixtureSet($array, $fixtureSet); } $array = []; for ($i = 0; $i < $quantifier; $i++) { $result = $this->resolver->resolve($element, $fixture, $fixtureSet, $scope, $context); $array[] = $result->getValue(); $fixtureSet = $result->getSet(); } return new ResolvedValueWithFixtureSet($array, $fixtureSet); }