|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the RollerworksPasswordStrengthValidator package. |
| 7 | + * |
| 8 | + * (c) Sebastiaan Stok <s.stok@rollerscapes.net> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the file LICENSE. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rollerworks\Component\PasswordStrength\Validator\Constraints; |
| 15 | + |
| 16 | +use Symfony\Component\Validator\Constraint; |
| 17 | +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 18 | +use Symfony\Component\Validator\Exception\InvalidOptionsException; |
| 19 | +use Symfony\Component\Validator\Exception\MissingOptionsException; |
| 20 | + |
| 21 | +if (method_exists(Constraint::class, 'normalizeOptions')) { |
| 22 | + /** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | + trait ConstraintCompatTrait |
| 26 | + { |
| 27 | + protected function initOptions(?array $options, ?array $groups, mixed $payload): void |
| 28 | + { |
| 29 | + // Noop |
| 30 | + } |
| 31 | + } |
| 32 | +} else { |
| 33 | + |
| 34 | + /** |
| 35 | + * @internal |
| 36 | + */ |
| 37 | + trait ConstraintCompatTrait |
| 38 | + { |
| 39 | + protected function initOptions(?array $options, ?array $groups, mixed $payload): void |
| 40 | + { |
| 41 | + if ($options === null) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + trigger_deprecation('symfony/validator', '7.4', 'Support for evaluating options in the %1$s class is deprecated. Initialize properties in the constructor of %1$s instead.', static::class); |
| 46 | + |
| 47 | + $options = $this->normalizeOptions($options); |
| 48 | + |
| 49 | + if ($groups !== null) { |
| 50 | + $options['groups'] = $groups; |
| 51 | + } |
| 52 | + $options['payload'] = $payload ?? $options['payload'] ?? null; |
| 53 | + |
| 54 | + foreach ($options as $name => $value) { |
| 55 | + $this->{$name} = $value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @deprecated since Symfony 7.4 |
| 61 | + * |
| 62 | + * @return array<string, mixed> |
| 63 | + */ |
| 64 | + protected function normalizeOptions(mixed $options): array |
| 65 | + { |
| 66 | + $normalizedOptions = []; |
| 67 | + $defaultOption = $this->getDefaultOption(false); |
| 68 | + $invalidOptions = []; |
| 69 | + $missingOptions = array_flip($this->getRequiredOptions(false)); |
| 70 | + $knownOptions = get_class_vars(static::class); |
| 71 | + |
| 72 | + if (\is_array($options) && isset($options['value']) && ! property_exists($this, 'value')) { |
| 73 | + if ($defaultOption === null) { |
| 74 | + throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class)); |
| 75 | + } |
| 76 | + |
| 77 | + $options[$defaultOption] = $options['value']; |
| 78 | + unset($options['value']); |
| 79 | + } |
| 80 | + |
| 81 | + if (\is_array($options)) { |
| 82 | + reset($options); |
| 83 | + } |
| 84 | + |
| 85 | + if ($options && \is_array($options) && \is_string(key($options))) { |
| 86 | + foreach ($options as $option => $value) { |
| 87 | + if (\array_key_exists($option, $knownOptions)) { |
| 88 | + $normalizedOptions[$option] = $value; |
| 89 | + unset($missingOptions[$option]); |
| 90 | + } else { |
| 91 | + $invalidOptions[] = $option; |
| 92 | + } |
| 93 | + } |
| 94 | + } elseif ($options !== null && ! (\is_array($options) && \count($options) === 0)) { |
| 95 | + if ($defaultOption === null) { |
| 96 | + throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class)); |
| 97 | + } |
| 98 | + |
| 99 | + if (\array_key_exists($defaultOption, $knownOptions)) { |
| 100 | + $normalizedOptions[$defaultOption] = $options; |
| 101 | + unset($missingOptions[$defaultOption]); |
| 102 | + } else { |
| 103 | + $invalidOptions[] = $defaultOption; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (\count($invalidOptions) > 0) { |
| 108 | + throw new InvalidOptionsException(\sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), static::class), $invalidOptions); |
| 109 | + } |
| 110 | + |
| 111 | + if (\count($missingOptions) > 0) { |
| 112 | + throw new MissingOptionsException(\sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), static::class), array_keys($missingOptions)); |
| 113 | + } |
| 114 | + |
| 115 | + return $normalizedOptions; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns the name of the default option. |
| 120 | + * |
| 121 | + * Override this method to define a default option. |
| 122 | + * |
| 123 | + * @deprecated since Symfony 7.4 |
| 124 | + * @see __construct() |
| 125 | + */ |
| 126 | + public function getDefaultOption(): ?string |
| 127 | + { |
| 128 | + if (\func_num_args() === 0 || func_get_arg(0)) { |
| 129 | + trigger_deprecation('symfony/validator', '7.4', 'The %s() method is deprecated.', __METHOD__); |
| 130 | + } |
| 131 | + |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns the name of the required options. |
| 137 | + * |
| 138 | + * Override this method if you want to define required options. |
| 139 | + * |
| 140 | + * @return string[] |
| 141 | + * |
| 142 | + * @deprecated since Symfony 7.4 |
| 143 | + * @see __construct() |
| 144 | + */ |
| 145 | + public function getRequiredOptions(): array |
| 146 | + { |
| 147 | + if (\func_num_args() === 0 || func_get_arg(0)) { |
| 148 | + trigger_deprecation('symfony/validator', '7.4', 'The %s() method is deprecated.', __METHOD__); |
| 149 | + } |
| 150 | + |
| 151 | + return []; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments