Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting minimum and maximum count of selected #96

Merged
merged 12 commits into from
Oct 30, 2023
Merged
38 changes: 19 additions & 19 deletions src/Field/Definition/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Torr\Storyblok\Field\Definition;

use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
Expand Down Expand Up @@ -95,31 +94,30 @@

private function validateSingleSelect (ComponentContext $context, array $contentPath, mixed $data) : void
{
$data = $context->normalizeOptionalString($data);

Check failure on line 97 in src/Field/Definition/ChoiceField.php

View workflow job for this annotation

GitHub Actions / build-test (8.1)

Parameter #1 $value of method Torr\Storyblok\Context\ComponentContext::normalizeOptionalString() expects string|null, mixed given.

Check failure on line 97 in src/Field/Definition/ChoiceField.php

View workflow job for this annotation

GitHub Actions / build-test (8.2)

Parameter #1 $value of method Torr\Storyblok\Context\ComponentContext::normalizeOptionalString() expects string|null, mixed given.
keichinger marked this conversation as resolved.
Show resolved Hide resolved

if (null === $data)
{
return;
}

\assert(\is_string($data));
keichinger marked this conversation as resolved.
Show resolved Hide resolved

$context->ensureDataIsValid(
$contentPath,
$this,
$data,
[
new AtLeastOneOf([
new Type("string"),
new Type("int"),
]),
new Type("string"),
$this->required
? new NotBlank()
keichinger marked this conversation as resolved.
Show resolved Hide resolved
: null,
],
);

\assert(\is_int($data) || \is_string($data));

if (\is_string($data))
{
$data = $context->normalizeOptionalString($data);
}

$choicesConstraints = $this->choices->getValidationConstraints(false);

if (null !== $data && !empty($choicesConstraints))
if (!empty($choicesConstraints))
{
$context->ensureDataIsValid(
$contentPath,
Expand All @@ -138,6 +136,13 @@
return;
}

\assert(\is_array($data));

$data = \array_map(
$context->normalizeOptionalString(...),
$data,
);

$context->ensureDataIsValid(
$contentPath,
$this,
Expand All @@ -146,16 +151,11 @@
new Type("array"),
keichinger marked this conversation as resolved.
Show resolved Hide resolved
new All([
new NotNull(),
new AtLeastOneOf([
new Type("string"),
new Type("int"),
]),
new Type("string"),
]),
],
);

\assert(\is_array($data));

if ($this->required || null !== $this->minimumNumberOfOptions || null !== $this->maximumNumberOfOptions)
{
$context->ensureDataIsValid(
Expand Down
Loading