Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 11 additions & 53 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: 'CI'
on:
push:
branches:
- main
- '2.0'
pull_request:
branches:
- main
- '2.0'

jobs:
cs-fixer:
Expand All @@ -22,7 +22,7 @@ jobs:
steps:
-
name: 'Check out'
uses: 'actions/checkout@v2'
uses: 'actions/checkout@v6'

-
name: 'Set up PHP'
Expand All @@ -38,7 +38,7 @@ jobs:

-
name: 'Cache dependencies'
uses: 'actions/cache@v2'
uses: 'actions/cache@v4'
with:
path: '${{ steps.composer-cache.outputs.cache-dir }}'
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
Expand All @@ -52,49 +52,6 @@ jobs:
name: 'Check the code style'
run: 'make cs-full'

phpstan:
name: 'PhpStan'

runs-on: 'ubuntu-latest'

strategy:
matrix:
php-version:
- '8.2'

steps:
-
name: 'Check out'
uses: 'actions/checkout@v2'

-
name: 'Set up PHP'
uses: 'shivammathur/setup-php@v2'
with:
php-version: '${{ matrix.php-version }}'
coverage: 'none'

-
name: 'Get Composer cache directory'
id: 'composer-cache'
run: 'echo "::set-output name=cache-dir::$(composer config cache-files-dir)"'

-
name: 'Cache dependencies'
uses: 'actions/cache@v2'
with:
path: '${{ steps.composer-cache.outputs.cache-dir }}'
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: 'php-${{ matrix.php-version }}-composer-locked-'

-
name: 'Install dependencies'
run: 'composer update --no-progress --prefer-stable'

-
name: 'Run PhpStan'
run: 'vendor/bin/phpstan analyze --no-progress'

tests:
name: 'PHPUnit'

Expand All @@ -106,21 +63,22 @@ jobs:
-
php-version: '8.2'
composer-options: '--prefer-stable'
symfony-version: '6.3'
symfony-version: '^6.4'

-
php-version: '8.2'
composer-options: '--prefer-stable'
symfony-version: '^6.4'
symfony-version: '^7.4'

-
php-version: '8.2'
php-version: '8.4'
composer-options: '--prefer-stable'
symfony-version: '^7.0'
symfony-version: '^8.0'

steps:
-
name: 'Check out'
uses: 'actions/checkout@v2'
uses: 'actions/checkout@v6'

-
name: 'Set up PHP'
Expand All @@ -136,7 +94,7 @@ jobs:

-
name: 'Cache dependencies'
uses: 'actions/cache@v2'
uses: 'actions/cache@v4'
with:
path: '${{ steps.composer-cache.outputs.cache-dir }}'
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
symfony.lock
.phpunit.result.cache
.php-cs-fixer.cache
.phpunit.cache/
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
}
],
"require": {
"php": ">=8.2",
"symfony/config": "^6.0 || ^7.0",
"php": "^8.2",
"symfony/config": "^6.4 || ^7.4 || ^8.0",
"symfony/polyfill-mbstring": "^1.5.0",
"symfony/translation": "^6.0 || ^7.0",
"symfony/validator": "^6.0 || ^7.0"
"symfony/translation": "^6.4 || ^7.4 || ^8.0",
"symfony/validator": "^6.4 || ^7.4 || ^8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-symfony": "^1.2",
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^6.0 || ^7.0"
"symfony/phpunit-bridge": "^7.4 || ^8.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ini name="memory_limit" value="-1"/>
<ini name="default_charset" value="UTF-8"/>

<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled=1" />
</php>

<testsuites>
Expand Down
154 changes: 154 additions & 0 deletions src/Validator/Constraints/ConstraintCompatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksPasswordStrengthValidator package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\PasswordStrength\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\MissingOptionsException;

if (method_exists(Constraint::class, 'normalizeOptions')) {
/**
* @internal
*/
trait ConstraintCompatTrait
{
protected function initOptions(?array $options, ?array $groups, mixed $payload): void
{
// Noop
}
}
} else {

/**
* @internal
*/
trait ConstraintCompatTrait
{
protected function initOptions(?array $options, ?array $groups, mixed $payload): void
{
if ($options === null) {
return;
}

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);

$options = $this->normalizeOptions($options);

if ($groups !== null) {
$options['groups'] = $groups;
}
$options['payload'] = $payload ?? $options['payload'] ?? null;

foreach ($options as $name => $value) {
$this->{$name} = $value;
}
}

/**
* @deprecated since Symfony 7.4
*
* @return array<string, mixed>
*/
protected function normalizeOptions(mixed $options): array
{
$normalizedOptions = [];
$defaultOption = $this->getDefaultOption(false);
$invalidOptions = [];
$missingOptions = array_flip($this->getRequiredOptions(false));
$knownOptions = get_class_vars(static::class);

if (\is_array($options) && isset($options['value']) && ! property_exists($this, 'value')) {
if ($defaultOption === null) {
throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class));
}

$options[$defaultOption] = $options['value'];
unset($options['value']);
}

if (\is_array($options)) {
reset($options);
}

if ($options && \is_array($options) && \is_string(key($options))) {
foreach ($options as $option => $value) {
if (\array_key_exists($option, $knownOptions)) {
$normalizedOptions[$option] = $value;
unset($missingOptions[$option]);
} else {
$invalidOptions[] = $option;
}
}
} elseif ($options !== null && ! (\is_array($options) && \count($options) === 0)) {
if ($defaultOption === null) {
throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class));
}

if (\array_key_exists($defaultOption, $knownOptions)) {
$normalizedOptions[$defaultOption] = $options;
unset($missingOptions[$defaultOption]);
} else {
$invalidOptions[] = $defaultOption;
}
}

if (\count($invalidOptions) > 0) {
throw new InvalidOptionsException(\sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), static::class), $invalidOptions);
}

if (\count($missingOptions) > 0) {
throw new MissingOptionsException(\sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), static::class), array_keys($missingOptions));
}

return $normalizedOptions;
}

/**
* Returns the name of the default option.
*
* Override this method to define a default option.
*
* @deprecated since Symfony 7.4
* @see __construct()
*/
public function getDefaultOption(): ?string
{
if (\func_num_args() === 0 || func_get_arg(0)) {
trigger_deprecation('symfony/validator', '7.4', 'The %s() method is deprecated.', __METHOD__);
}

return null;
}

/**
* Returns the name of the required options.
*
* Override this method if you want to define required options.
*
* @return string[]
*
* @deprecated since Symfony 7.4
* @see __construct()
*/
public function getRequiredOptions(): array
{
if (\func_num_args() === 0 || func_get_arg(0)) {
trigger_deprecation('symfony/validator', '7.4', 'The %s() method is deprecated.', __METHOD__);
}

return [];
}
}
}
4 changes: 3 additions & 1 deletion src/Validator/Constraints/PasswordRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

/**
* @Annotation
*
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class PasswordRequirements extends Constraint
{
use ConstraintCompatTrait;

public string $tooShortMessage = 'Your password must be at least {{length}} characters long.';
public string $missingLettersMessage = 'Your password must include at least one letter.';
public string $requireCaseDiffMessage = 'Your password must include both upper and lower case letters.';
Expand Down Expand Up @@ -49,6 +50,7 @@ public function __construct(
?string $missingSpecialCharacterMessage = null
) {
parent::__construct($options ?? [], $groups, $payload);
$this->initOptions($options, $groups, $payload);

$this->tooShortMessage = $tooShortMessage ?? $this->tooShortMessage;
$this->missingLettersMessage = $missingLettersMessage ?? $this->missingLettersMessage;
Expand Down
4 changes: 3 additions & 1 deletion src/Validator/Constraints/PasswordStrength.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

/**
* @Annotation
*
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class PasswordStrength extends Constraint
{
use ConstraintCompatTrait;

public string $tooShortMessage = 'Your password must be at least {{length}} characters long.';
public string $message = 'password_too_weak';
public int $minLength = 6;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function __construct(
}

parent::__construct($finalOptions, $groups, $payload);
$this->initOptions($finalOptions, $groups, $payload);

$this->minLength = $minLength ?? $this->minLength;
$this->unicodeEquality = $unicodeEquality ?? $this->unicodeEquality;
Expand Down
Loading
Loading