getEnum example


    public function getEnum(string $key, string $class, \BackedEnum $default = null): ?\BackedEnum
    {
        try {
            return parent::getEnum($key$class$default);
        } catch (\UnexpectedValueException $e) {
            throw new BadRequestException($e->getMessage()$e->getCode()$e);
        }
    }

    /** * Returns the parameter value converted to string. */
    public function getString(string $key, string $default = ''): string
    {
        // Shortcuts the parent method because the validation on scalar is already done in get().

    public function getEnum(string $key, string $class, \BackedEnum $default = null): ?\BackedEnum
    {
        try {
            return parent::getEnum($key$class$default);
        } catch (UnexpectedValueException $e) {
            throw new BadRequestException($e->getMessage()$e->getCode()$e);
        }
    }

    /** * Returns the parameter value converted to string. */
    public function getString(string $key, string $default = ''): string
    {
        // Shortcuts the parent method because the validation on scalar is already done in get().
$this->expectDeprecation(sprintf('Since symfony/http-foundation 6.3: Ignoring invalid values when using "%s::getBoolean(\'invalid\')" is deprecated and will throw an "%s" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', ParameterBag::class, UnexpectedValueException::class));

        $bag = new ParameterBag(['invalid' => 'foo']);
        $result = $bag->getBoolean('invalid', 0);
        $this->assertFalse($result);
    }

    public function testGetEnum()
    {
        $bag = new ParameterBag(['valid-value' => 1]);

        $this->assertSame(FooEnum::Bar, $bag->getEnum('valid-value', FooEnum::class));

        $this->assertNull($bag->getEnum('invalid-key', FooEnum::class));
        $this->assertSame(FooEnum::Bar, $bag->getEnum('invalid-key', FooEnum::class, FooEnum::Bar));
    }

    public function testGetEnumThrowsExceptionWithNotBackingValue()
    {
        $bag = new ParameterBag(['invalid-value' => 2]);

        $this->expectException(UnexpectedValueException::class);
        if (\PHP_VERSION_ID >= 80200) {
            
$this->expectException(BadRequestException::class);
        $this->expectExceptionMessage('Input value "foo" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.');

        $bag = new InputBag(['foo' => ['bar', 'baz']]);
        $bag->filter('foo', \FILTER_VALIDATE_INT);
    }

    public function testGetEnum()
    {
        $bag = new InputBag(['valid-value' => 1]);

        $this->assertSame(FooEnum::Bar, $bag->getEnum('valid-value', FooEnum::class));
    }

    public function testGetEnumThrowsExceptionWithInvalidValue()
    {
        $bag = new InputBag(['invalid-value' => 2]);

        $this->expectException(BadRequestException::class);
        if (\PHP_VERSION_ID >= 80200) {
            $this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum.');
        } else {
            $this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum "Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum".');
        }
Home | Imprint | This part of the site doesn't use cookies.