Skip to content

Commit 87c889f

Browse files
committed
Add support for Symfony 8
1 parent 89f0c1d commit 87c889f

6 files changed

Lines changed: 179 additions & 19 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: 'CI'
33
on:
44
push:
55
branches:
6-
- main
6+
- '2.0'
77
pull_request:
88
branches:
9-
- main
9+
- '2.0'
1010

1111
jobs:
1212
cs-fixer:
@@ -22,7 +22,7 @@ jobs:
2222
steps:
2323
-
2424
name: 'Check out'
25-
uses: 'actions/checkout@v2'
25+
uses: 'actions/checkout@v6'
2626

2727
-
2828
name: 'Set up PHP'
@@ -38,7 +38,7 @@ jobs:
3838

3939
-
4040
name: 'Cache dependencies'
41-
uses: 'actions/cache@v2'
41+
uses: 'actions/cache@v4'
4242
with:
4343
path: '${{ steps.composer-cache.outputs.cache-dir }}'
4444
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
@@ -65,7 +65,7 @@ jobs:
6565
steps:
6666
-
6767
name: 'Check out'
68-
uses: 'actions/checkout@v2'
68+
uses: 'actions/checkout@v6'
6969

7070
-
7171
name: 'Set up PHP'
@@ -81,7 +81,7 @@ jobs:
8181

8282
-
8383
name: 'Cache dependencies'
84-
uses: 'actions/cache@v2'
84+
uses: 'actions/cache@v4'
8585
with:
8686
path: '${{ steps.composer-cache.outputs.cache-dir }}'
8787
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
@@ -106,21 +106,22 @@ jobs:
106106
-
107107
php-version: '8.2'
108108
composer-options: '--prefer-stable'
109-
symfony-version: '6.3'
109+
symfony-version: '^6.4'
110+
110111
-
111112
php-version: '8.2'
112113
composer-options: '--prefer-stable'
113-
symfony-version: '^6.4'
114+
symfony-version: '^7.4'
114115

115116
-
116-
php-version: '8.2'
117+
php-version: '8.4'
117118
composer-options: '--prefer-stable'
118-
symfony-version: '^7.0'
119+
symfony-version: '^8.0'
119120

120121
steps:
121122
-
122123
name: 'Check out'
123-
uses: 'actions/checkout@v2'
124+
uses: 'actions/checkout@v6'
124125

125126
-
126127
name: 'Set up PHP'
@@ -136,7 +137,7 @@ jobs:
136137

137138
-
138139
name: 'Cache dependencies'
139-
uses: 'actions/cache@v2'
140+
uses: 'actions/cache@v4'
140141
with:
141142
path: '${{ steps.composer-cache.outputs.cache-dir }}'
142143
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
symfony.lock
77
.phpunit.result.cache
88
.php-cs-fixer.cache
9+
.phpunit.cache/

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">=8.2",
23-
"symfony/config": "^6.0 || ^7.0",
22+
"php": "^8.2",
23+
"symfony/config": "^6.4 || ^7.4 || ^8.0",
2424
"symfony/polyfill-mbstring": "^1.5.0",
25-
"symfony/translation": "^6.0 || ^7.0",
26-
"symfony/validator": "^6.0 || ^7.0"
25+
"symfony/translation": "^6.4 || ^7.4 || ^8.0",
26+
"symfony/validator": "^6.4 || ^7.4 || ^8.0"
2727
},
2828
"require-dev": {
2929
"phpstan/phpstan": "^1.10",
3030
"phpstan/phpstan-phpunit": "^1.1",
3131
"phpstan/phpstan-symfony": "^1.2",
3232
"phpunit/phpunit": "^9.5",
33-
"symfony/phpunit-bridge": "^6.0 || ^7.0"
33+
"symfony/phpunit-bridge": "^7.4 || ^8.0"
3434
},
3535
"minimum-stability": "dev",
3636
"prefer-stable": true,
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}

src/Validator/Constraints/PasswordRequirements.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
/**
1717
* @Annotation
18-
*
1918
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
2019
*/
2120
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2221
class PasswordRequirements extends Constraint
2322
{
23+
use ConstraintCompatTrait;
24+
2425
public string $tooShortMessage = 'Your password must be at least {{length}} characters long.';
2526
public string $missingLettersMessage = 'Your password must include at least one letter.';
2627
public string $requireCaseDiffMessage = 'Your password must include both upper and lower case letters.';
@@ -49,6 +50,7 @@ public function __construct(
4950
?string $missingSpecialCharacterMessage = null
5051
) {
5152
parent::__construct($options ?? [], $groups, $payload);
53+
$this->initOptions($options, $groups, $payload);
5254

5355
$this->tooShortMessage = $tooShortMessage ?? $this->tooShortMessage;
5456
$this->missingLettersMessage = $missingLettersMessage ?? $this->missingLettersMessage;

src/Validator/Constraints/PasswordStrength.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
/**
1717
* @Annotation
18-
*
1918
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
2019
*/
2120
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2221
class PasswordStrength extends Constraint
2322
{
23+
use ConstraintCompatTrait;
24+
2425
public string $tooShortMessage = 'Your password must be at least {{length}} characters long.';
2526
public string $message = 'password_too_weak';
2627
public int $minLength = 6;
@@ -51,6 +52,7 @@ public function __construct(
5152
}
5253

5354
parent::__construct($finalOptions, $groups, $payload);
55+
$this->initOptions($finalOptions, $groups, $payload);
5456

5557
$this->minLength = $minLength ?? $this->minLength;
5658
$this->unicodeEquality = $unicodeEquality ?? $this->unicodeEquality;

0 commit comments

Comments
 (0)