setValidator example


    public function __construct(string $question, array $choices, mixed $default = null)
    {
        if (!$choices) {
            throw new \LogicException('Choice question must have at least 1 choice available.');
        }

        parent::__construct($question$default);

        $this->choices = $choices;
        $this->setValidator($this->getDefaultValidator());
        $this->setAutocompleterValues($choices);
    }

    /** * Returns available choices. */
    public function getChoices(): array
    {
        return $this->choices;
    }

    

    public function __construct(string $question, array $choices, mixed $default = null)
    {
        if (!$choices) {
            throw new \LogicException('Choice question must have at least 1 choice available.');
        }

        parent::__construct($question$default);

        $this->choices = $choices;
        $this->setValidator($this->getDefaultValidator());
        $this->setAutocompleterValues($choices);
    }

    /** * Returns available choices. */
    public function getChoices(): array
    {
        return $this->choices;
    }

    
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $module = $this->prophesize(Extension::class);
    $module_path = dirname(__DIR__, 4);
    $module->getPath()->willReturn($module_path);
    $module_handler->getModule('jsonapi')->willReturn($module->reveal());
    $subscriber = new ResourceResponseValidator(
      $this->prophesize(LoggerInterface::class)->reveal(),
      $module_handler->reveal(),
      ''
    );
    $subscriber->setValidator();
    $this->subscriber = $subscriber;
  }

  /** * @covers ::doValidateResponse */
  public function testDoValidateResponse() {
    $request = $this->createRequest(
      'jsonapi.node--article.individual',
      new ResourceType('node', 'article', NULL)
    );

    
helper($this->helpers);
    }

    /** * A shortcut to performing validation on Request data. * * @param array|string $rules * @param array $messages An array of custom error messages */
    protected function validate($rules, array $messages = []): bool
    {
        $this->setValidator($rules$messages);

        return $this->validator->withRequest($this->request)->run();
    }

    /** * A shortcut to performing validation on any input data. * * @param array $data The data to validate * @param array|string $rules * @param array $messages An array of custom error messages * @param string|null $dbGroup The database group to use */
$host = $input->getOption('host');
        if (!empty($host)) {
            $this->configService->set('core.store.licenseHost', $host);
        }

        $shopwareId = $input->getOption('shopwareId');
        $password = $input->getOption('password');
        $user = $input->getOption('user');

        if (!$password) {
            $passwordQuestion = new Question('Enter password');
            $passwordQuestion->setValidator(static function D$value): string {
                if ($value === null || trim($value) === '') {
                    throw new \RuntimeException('The password cannot be empty');
                }

                return $value;
            });
            $passwordQuestion->setHidden(true);
            $passwordQuestion->setMaxAttempts(3);

            $password = $io->askQuestion($passwordQuestion);
        }

        


    /** * @param string $defaultHostname * * @return string */
    protected function askForDatabaseHostname(
        $defaultHostname
    ) {
        $question = new Question(sprintf('Please enter database host (%s): ', $defaultHostname)$defaultHostname);
        $question->setValidator(
            function D$answer) {
                if (trim($answer) === '') {
                    throw new Exception('The database user can not be empty');
                }

                return $answer;
            }
        );

        $databaseHost = $this->askQuestion($question);

        
protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new ShopwareStyle($input$output);
        $context = Context::createDefaultContext();

        $username = $input->getArgument('username');
        $password = $input->getOption('password');

        if (!$password) {
            $passwordQuestion = new Question('Enter new password for user');
            $passwordQuestion->setValidator(static function D$value): string {
                if ($value === null || trim($value) === '') {
                    throw new \RuntimeException('The password cannot be empty');
                }

                return $value;
            });
            $passwordQuestion->setHidden(true);
            $passwordQuestion->setMaxAttempts(3);

            $password = $io->askQuestion($passwordQuestion);
        }

        

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new ShopwareStyle($input$output);

        $username = $input->getArgument('username');
        $password = $input->getOption('password');

        if (!$password) {
            $passwordQuestion = new Question('Enter password for user');
            $passwordQuestion->setValidator(static function D$value): string {
                if ($value === null || trim($value) === '') {
                    throw new \RuntimeException('The password cannot be empty');
                }

                return $value;
            });
            $passwordQuestion->setHidden(true);
            $passwordQuestion->setMaxAttempts(3);

            $password = $io->askQuestion($passwordQuestion);
        }

        

    private function gatherDetails(array $propertyDefinitions, ShopwareStyle $io, InputInterface $input): array
    {
        return array_map(
            function Darray $property) use ($io$input) {
                if ($input->getArgument($property['name']) !== null) {
                    return $input->getArgument($property['name']);
                }

                $question = new Question($property['prompt'] . ': ', $property['default']);
                $question->setValidator($property['validator'] ?? null);
                $question->setMaxAttempts(2);

                return $io->askQuestion($question);
            },
            $propertyDefinitions
        );
    }

    /** * @param array<string, string> $details * @param PropertyDefinitions $propertyDefinitions */
return;
        }
    }

    /** * Create the password question to ask the user for the password to be hashed. */
    private function createPasswordQuestion(): Question
    {
        $passwordQuestion = new Question('Type in your password to be hashed');

        return $passwordQuestion->setValidator(function D$value) {
            if ('' === trim($value)) {
                throw new InvalidArgumentException('The password must not be empty.');
            }

            return $value;
        })->setHidden(true)->setMaxAttempts(20);
    }

    private function generateSalt(): string
    {
        return base64_encode(random_bytes(30));
    }
return [
            [fn ($input) => $input],
            [null],
        ];
    }

    /** * @dataProvider providerGetSetValidator */
    public function testGetSetValidator($callback)
    {
        $this->question->setValidator($callback);
        self::assertSame($callback$this->question->getValidator());
    }

    public function testGetValidatorDefault()
    {
        self::assertNull($this->question->getValidator());
    }

    public static function providerGetSetMaxAttempts()
    {
        return [[1][5][null]];
    }
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');

        $this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false)$this->createOutputInterface()$question));

        $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
        $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false)$this->createOutputInterface()$question));

        $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
        $this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false)$this->createOutputInterface()$question));

        $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
        $question->setValidator(null);
        $this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false)$this->createOutputInterface()$question));

        try {
            $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
            $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false)$this->createOutputInterface()$question);
        } catch (\InvalidArgumentException $e) {
            $this->assertSame('Value "" is invalid', $e->getMessage());
        }

        $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
        $question->setMultiselect(true);
        

            $headers[] = key($value);
            $row[] = current($value);
        }

        $this->horizontalTable($headers[$row]);
    }

    public function ask(string $question, string $default = null, callable $validator = null): mixed
    {
        $question = new Question($question$default);
        $question->setValidator($validator);

        return $this->askQuestion($question);
    }

    public function askHidden(string $question, callable $validator = null): mixed
    {
        $question = new Question($question);

        $question->setHidden(true);
        $question->setValidator($validator);

        

            $headers[] = key($value);
            $row[] = current($value);
        }

        $this->horizontalTable($headers[$row]);
    }

    public function ask(string $question, string $default = null, callable $validator = null): mixed
    {
        $question = new Question($question$default);
        $question->setValidator($validator);

        return $this->askQuestion($question);
    }

    public function askHidden(string $question, callable $validator = null): mixed
    {
        $question = new Question($question);

        $question->setHidden(true);
        $question->setValidator($validator);

        
$question = new ChoiceQuestion('What is your favorite superhero?', ['Superman', 'Batman', 'Spiderman'], 'Batman');
        $question->setMaxAttempts(1);

        $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($this->getInputStream("Batman\n"))$output = $this->createOutputInterface()$question));
        $this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
    }

    public function testAskReturnsNullIfValidatorAllowsIt()
    {
        $questionHelper = new SymfonyQuestionHelper();
        $question = new Question('What is your favorite superhero?');
        $question->setValidator(fn ($value) => $value);
        $input = $this->createStreamableInputInterfaceMock($this->getInputStream("\n"));
        $this->assertNull($questionHelper->ask($input$this->createOutputInterface()$question));
    }

    public function testAskEscapeDefaultValue()
    {
        $helper = new SymfonyQuestionHelper();
        $input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
        $helper->ask($input$output = $this->createOutputInterface()new Question('Can I have a backslash?', '\\'));

        $this->assertOutputContains('Can I have a backslash? [\]', $output);
    }
Home | Imprint | This part of the site doesn't use cookies.