setTranslator example


    public function isValid($value)
    {
        if (!is_string($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $this->_setValue($value);
        // Check input against IP address schema         if (preg_match('/^[0-9a-f:.]*$/i', $value) &&
            $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
            if (!($this->_options['allow'] & self::ALLOW_IP)) {
                $this->_error(self::IP_ADDRESS_NOT_ALLOWED);
                return false;
            } else {
                return true;
            }
        }

        // RFC3986 3.2.2 states:         //         // The rightmost domain label of a fully qualified domain name
return $result;
    }

    /** * Internal method to validate the hostname part of the email address * * @return boolean */
    private function _validateHostnamePart()
    {
        $hostname = $this->_options['hostname']->setTranslator($this->getTranslator())
                         ->isValid($this->_hostname);
        if (!$hostname) {
            $this->_error(self::INVALID_HOSTNAME);

            // Get messages and errors from hostnameValidator             foreach ($this->_options['hostname']->getMessages() as $code => $message) {
                $this->_messages[$code] = $message;
            }

            foreach ($this->_options['hostname']->getErrors() as $error) {
                $this->_errors[] = $error;
            }


    public function testSetConstraintValidatorFactory()
    {
        $this->assertSame($this->builder, $this->builder->setConstraintValidatorFactory(
            $this->createMock(ConstraintValidatorFactoryInterface::class))
        );
    }

    public function testSetTranslator()
    {
        $this->assertSame($this->builder, $this->builder->setTranslator(
            $this->createMock(TranslatorInterface::class))
        );
    }

    public function testSetTranslationDomain()
    {
        $this->assertSame($this->builder, $this->builder->setTranslationDomain('TRANS_DOMAIN'));
    }

    public function testGetValidator()
    {
        
$response = $this->authenticator->onAuthenticationFailure(new Request()new AuthenticationException());
        $this->assertSame(['error' => 'An authentication exception occurred.']json_decode($response->getContent(), true));
    }

    public function testAuthenticationFailureWithTranslator()
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');

        $this->setUpAuthenticator();
        $this->authenticator->setTranslator($translator);

        $response = $this->authenticator->onAuthenticationFailure(new Request()new AuthenticationException());
        $this->assertSame(['error' => 'foo']json_decode($response->getContent(), true));
    }

    public function testOnFailureReplacesMessageDataWithoutTranslator()
    {
        $this->setUpAuthenticator();

        $response = $this->authenticator->onAuthenticationFailure(new Request()new class() extends AuthenticationException {
            public function getMessageData(): array
            {


                if ('This value should be null.' === $id) {
                    return 'Dummy violation.';
                }

                return $id;
            }
        };

        $validator = Validation::createValidatorBuilder()
            ->setTranslator($translator)
            ->getValidator()
        ;

        $violations = $validator->validate('Test', [
            new AtLeastOneOf([
                new IsNull(),
            ]),
        ]);

        $this->assertCount(1, $violations);
        $this->assertSame('Dummy translation: [1] Dummy violation.', $violations->get(0)->getMessage());
    }
Home | Imprint | This part of the site doesn't use cookies.