TransformationFailedException example


    public function transform(mixed $dateTime): string
    {
        if (null === $dateTime) {
            return '';
        }

        if (!$dateTime instanceof \DateTimeInterface) {
            throw new TransformationFailedException('Expected a \DateTimeInterface.');
        }

        $value = $this->getIntlDateFormatter()->format($dateTime->getTimestamp());

        if (0 != intl_get_error_code()) {
            throw new TransformationFailedException(intl_get_error_message());
        }

        return $value;
    }

    

    public function transform(mixed $dateTime): string
    {
        if (null === $dateTime) {
            return '';
        }

        if (!$dateTime instanceof \DateTimeInterface) {
            throw new TransformationFailedException('Expected a \DateTimeInterface.');
        }

        if ($this->inputTimezone !== $this->outputTimezone) {
            $dateTime = \DateTimeImmutable::createFromInterface($dateTime);
            $dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
        }

        return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c'));
    }

    /** * Transforms a formatted string following RFC 3339 into a normalized date. * * @param string $rfc3339 Formatted string * * @throws TransformationFailedException If the given value is not a string, * if the value could not be transformed */

    public function transform(mixed $dateTime): ?int
    {
        if (null === $dateTime) {
            return null;
        }

        if (!$dateTime instanceof \DateTimeInterface) {
            throw new TransformationFailedException('Expected a \DateTimeInterface.');
        }

        return $dateTime->getTimestamp();
    }

    /** * Transforms a timestamp in the configured timezone into a DateTime object. * * @param string $value A timestamp * * @throws TransformationFailedException If the given value is not a timestamp * or if the given timestamp is invalid */

    public function transform(mixed $value): array
    {
        if (null === $value) {
            return ['year' => null, 'week' => null];
        }

        if (!\is_string($value)) {
            throw new TransformationFailedException(sprintf('Value is expected to be a string but was "%s".', get_debug_type($value)));
        }

        if (0 === preg_match('/^(?P<year>\d{4})-W(?P<week>\d{2})$/', $value$matches)) {
            throw new TransformationFailedException('Given data does not follow the date format "Y-\WW".');
        }

        return [
            'year' => (int) $matches['year'],
            'week' => (int) $matches['week'],
        ];
    }

    

    public function transform(mixed $value): string
    {
        if (null === $value) {
            return '';
        }

        if (!is_numeric($value)) {
            throw new TransformationFailedException('Expected a numeric.');
        }

        if (self::FRACTIONAL == $this->type) {
            $value *= 100;
        }

        $formatter = $this->getNumberFormatter();
        $value = $formatter->format($value);

        if (intl_is_failure($formatter->getErrorCode())) {
            throw new TransformationFailedException($formatter->getErrorMessage());
        }
/** * @throws TransformationFailedException if the given value is not an array */
    public function transform(mixed $array): array
    {
        if (null === $array) {
            return [];
        }

        if (!\is_array($array)) {
            throw new TransformationFailedException('Expected an array.');
        }

        return $this->choiceList->getValuesForChoices($array);
    }

    /** * @throws TransformationFailedException if the given value is not an array * or if no matching choice could be * found for some given value */
    public function reverseTransform(mixed $array): array
    {


    public function testDoNotAddInvalidMessageIfChildFormIsAlreadyNotSynchronized()
    {
        $formBuilder = $this->formFactory->createBuilder()
            ->add('field1')
            ->add('field2')
            ->addModelTransformer(new CallbackTransformer(
                function D) {
                },
                function D) {
                    throw new TransformationFailedException('This value is invalid.');
                }
            ));
        $formBuilder->get('field2')->addModelTransformer(new CallbackTransformer(
            function D) {
            },
            function D) {
                throw new TransformationFailedException('This value is invalid.');
            }
        ));
        $form = $formBuilder->getForm();

        
$this->multiple = $multiple;
    }

    public function transform(mixed $intlTimeZone): mixed
    {
        if (null === $intlTimeZone) {
            return null;
        }

        if ($this->multiple) {
            if (!\is_array($intlTimeZone)) {
                throw new TransformationFailedException('Expected an array of \IntlTimeZone objects.');
            }

            return array_map([new self(), 'transform']$intlTimeZone);
        }

        if (!$intlTimeZone instanceof \IntlTimeZone) {
            throw new TransformationFailedException('Expected a \IntlTimeZone object.');
        }

        return $intlTimeZone->getID();
    }

    
// Like above, but we just filter out empty strings.             $values = array_values(array_filter($valuesfn ($v) => '' !== (string) $v));

            // Convert values into right type             if (Type::hasType($type)) {
                $doctrineType = Type::getType($type);
                $platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
                foreach ($values as &$value) {
                    try {
                        $value = $doctrineType->convertToDatabaseValue($value$platform);
                    } catch (ConversionException $e) {
                        throw new TransformationFailedException(sprintf('Failed to transform "%s" into "%s".', $value$type), 0, $e);
                    }
                }
                unset($value);
            }
        } else {
            $parameterType = class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY;
        }
        if (!$values) {
            return [];
        }

        

    public function transform(mixed $value): ?string
    {
        if (null === $value) {
            return null;
        }

        if (!\is_bool($value)) {
            throw new TransformationFailedException('Expected a Boolean.');
        }

        return $value ? $this->trueValue : null;
    }

    /** * Transforms a string into a Boolean. * * @param string $value String value * * @throws TransformationFailedException if the given value is not a string */

    private array $mapping;

    public function __construct(array $mapping)
    {
        $this->mapping = $mapping;
    }

    public function transform($value): mixed
    {
        if (!\array_key_exists($value$this->mapping)) {
            throw new TransformationFailedException(sprintf('No mapping for value "%s"', $value));
        }

        return $this->mapping[$value];
    }

    public function reverseTransform($value): mixed
    {
        $result = array_search($value$this->mapping, true);

        if (false === $result) {
            throw new TransformationFailedException(sprintf('No reverse mapping for value "%s"', $value));
        }
$form = $this->getBuilder('name', '\stdClass', [
                'invalid_message' => 'invalid_message_key',
                // Invalid message parameters must be supported, because the                 // invalid message can be a translation key                 // see https://github.com/symfony/symfony/issues/5144                 'invalid_message_parameters' => ['{{ foo }}' => 'bar'],
            ])
            ->setData($object)
            ->addViewTransformer(new CallbackTransformer(
                static fn ($data) => $data,
                static fn () => throw new TransformationFailedException()
            ))
            ->getForm();

        // Launch transformer         $form->submit('foo');

        $this->assertTrue($form->isSubmitted());
        $this->assertFalse($form->isSynchronized());
        $this->expectNoValidate();

        $this->validator->validate($formnew Form());

        
        // Don't convert NULL to a string here in order to determine later         // whether an empty value has been submitted or whether no value has         // been submitted at all. This is important for processing checkboxes         // and radio buttons with empty values.         if (false === $submittedData) {
            $submittedData = null;
        } elseif (\is_scalar($submittedData)) {
            $submittedData = (string) $submittedData;
        } elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
            if (!$this->config->getOption('allow_file_upload')) {
                $submittedData = null;
                $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
            }
        } elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->getOption('multiple', false)) {
            $submittedData = null;
            $this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
        }

        $dispatcher = $this->config->getEventDispatcher();

        $modelData = null;
        $normData = null;
        $viewData = null;

        
$this->multiple = $multiple;
    }

    public function transform(mixed $dateTimeZone): mixed
    {
        if (null === $dateTimeZone) {
            return null;
        }

        if ($this->multiple) {
            if (!\is_array($dateTimeZone)) {
                throw new TransformationFailedException('Expected an array of \DateTimeZone objects.');
            }

            return array_map([new self(), 'transform']$dateTimeZone);
        }

        if (!$dateTimeZone instanceof \DateTimeZone) {
            throw new TransformationFailedException('Expected a \DateTimeZone object.');
        }

        return $dateTimeZone->getName();
    }

    

    public function transform(mixed $dateTime): string
    {
        if (null === $dateTime) {
            return '';
        }

        if (!$dateTime instanceof \DateTimeInterface) {
            throw new TransformationFailedException('Expected a \DateTimeInterface.');
        }

        if ($this->inputTimezone !== $this->outputTimezone) {
            $dateTime = \DateTimeImmutable::createFromInterface($dateTime);
            $dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
        }

        return $dateTime->format($this->withSeconds ? self::HTML5_FORMAT : self::HTML5_FORMAT_NO_SECONDS);
    }

    /** * Transforms a local date and time string into a \DateTime. * * When transforming back to DateTime the regex is slightly laxer, taking into * account rules for parsing a local date and time string * https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-local-date-and-time-string * * @param string $dateTimeLocal Formatted string * * @throws TransformationFailedException If the given value is not a string, * if the value could not be transformed */
Home | Imprint | This part of the site doesn't use cookies.