Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

UserPasswordValidator.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Publish\Core\Repository\Validator;
10
11 use eZ\Publish\Core\FieldType\ValidationError;
12
18 class UserPasswordValidator
19 {
20 private const AT_LEAST_ONE_LOWER_CASE_CHARACTER_REGEX = '/\p{Ll}/u';
21 private const AT_LEAST_ONE_UPPER_CASE_CHARACTER_REGEX = '/\p{Lu}/u';
22 private const AT_LEAST_ONE_NUMERIC_CHARACTER_REGEX = '/\pN/u';
23 private const AT_LEAST_ONE_NON_ALPHANUMERIC_CHARACTER_REGEX = '/[^\p{Ll}\p{Lu}\pL\pN]/u';
24
26 private $constraints;
27
31 public function __construct(array $constraints)
32 {
33 $this->constraints = $constraints;
34 }
35
43 public function validate(string $password): array
44 {
45 $errors = [];
46
47 if (!$this->isLongEnough($password)) {
48 $errors[] = $this->createValidationError('User password must be at least %length% characters long', [
49 '%length%' => $this->constraints['minLength'],
50 ]);
51 }
52
53 if (!$this->containsAtLeastOneLowerCaseCharacter($password)) {
54 $errors[] = $this->createValidationError('User password must include at least one lower case letter');
55 }
56
57 if (!$this->containsAtLeastOneUpperCaseCharacter($password)) {
58 $errors[] = $this->createValidationError('User password must include at least one upper case letter');
59 }
60
61 if (!$this->containsAtLeastOneNumericCharacter($password)) {
62 $errors[] = $this->createValidationError('User password must include at least one number');
63 }
64
65 if (!$this->containsAtLeastOneNonAlphanumericCharacter($password)) {
66 $errors[] = $this->createValidationError('User password must include at least one special character');
67 }
68
69 return $errors;
70 }
71
79 private function isLongEnough(string $password): bool
80 {
81 if ((int) $this->constraints['minLength'] > 0) {
82 return mb_strlen($password) >= (int) $this->constraints['minLength'];
83 }
84
85 return true;
86 }
87
95 private function containsAtLeastOneLowerCaseCharacter(string $password): bool
96 {
97 if ($this->constraints['requireAtLeastOneLowerCaseCharacter']) {
98 return (bool)preg_match(self::AT_LEAST_ONE_LOWER_CASE_CHARACTER_REGEX, $password);
99 }
100
101 return true;
102 }
103
111 private function containsAtLeastOneUpperCaseCharacter(string $password): bool
112 {
113 if ($this->constraints['requireAtLeastOneUpperCaseCharacter']) {
114 return (bool)preg_match(self::AT_LEAST_ONE_UPPER_CASE_CHARACTER_REGEX, $password);
115 }
116
117 return true;
118 }
119
127 private function containsAtLeastOneNumericCharacter(string $password): bool
128 {
129 if ($this->constraints['requireAtLeastOneNumericCharacter']) {
130 return (bool)preg_match(self::AT_LEAST_ONE_NUMERIC_CHARACTER_REGEX, $password);
131 }
132
133 return true;
134 }
135
143 private function containsAtLeastOneNonAlphanumericCharacter(string $password): bool
144 {
145 if ($this->constraints['requireAtLeastOneNonAlphanumericCharacter']) {
146 return (bool)preg_match(self::AT_LEAST_ONE_NON_ALPHANUMERIC_CHARACTER_REGEX, $password);
147 }
148
149 return true;
150 }
151
160 private function createValidationError(string $message, array $values = []): ValidationError
161 {
162 return new ValidationError($message, null, $values, 'password');
163 }
164 }
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\Repository\Validator\UserPasswordValidator
Internal service to user password validation against specified constraints.
Definition: UserPasswordValidator.php:19
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\AT_LEAST_ONE_NON_ALPHANUMERIC_CHARACTER_REGEX
const AT_LEAST_ONE_NON_ALPHANUMERIC_CHARACTER_REGEX
Definition: UserPasswordValidator.php:23
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\$constraints
$constraints
Definition: UserPasswordValidator.php:26
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\containsAtLeastOneUpperCaseCharacter
containsAtLeastOneUpperCaseCharacter(string $password)
Checks if given $password contains at least one upper case character (if rule is applicable).
Definition: UserPasswordValidator.php:111
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\containsAtLeastOneNonAlphanumericCharacter
containsAtLeastOneNonAlphanumericCharacter(string $password)
Checks if given $password contains at least one non alphanumeric character (if rule is applicable).
Definition: UserPasswordValidator.php:143
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\AT_LEAST_ONE_UPPER_CASE_CHARACTER_REGEX
const AT_LEAST_ONE_UPPER_CASE_CHARACTER_REGEX
Definition: UserPasswordValidator.php:21
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\__construct
__construct(array $constraints)
Definition: UserPasswordValidator.php:31
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\isLongEnough
isLongEnough(string $password)
Checks if given $password satisfies length requirements.
Definition: UserPasswordValidator.php:79
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\containsAtLeastOneLowerCaseCharacter
containsAtLeastOneLowerCaseCharacter(string $password)
Checks if given $password contains at least one lower case character (if rule is applicable).
Definition: UserPasswordValidator.php:95
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\validate
validate(string $password)
Validates given $password.
Definition: UserPasswordValidator.php:43
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\containsAtLeastOneNumericCharacter
containsAtLeastOneNumericCharacter(string $password)
Checks if given $password contains at least one numeric character (if rule is applicable).
Definition: UserPasswordValidator.php:127
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\AT_LEAST_ONE_LOWER_CASE_CHARACTER_REGEX
const AT_LEAST_ONE_LOWER_CASE_CHARACTER_REGEX
Definition: UserPasswordValidator.php:20
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\createValidationError
createValidationError(string $message, array $values=[])
Creates a validation error with given messages and placeholders.
Definition: UserPasswordValidator.php:160
eZ\Publish\Core\Repository\Validator\UserPasswordValidator\AT_LEAST_ONE_NUMERIC_CHARACTER_REGEX
const AT_LEAST_ONE_NUMERIC_CHARACTER_REGEX
Definition: UserPasswordValidator.php:22
eZ\Publish\Core\Repository\Validator
Definition: UserPasswordValidator.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.