diff --git a/src/PasswordRules.php b/src/PasswordRules.php index 3a4a30c..f905c07 100644 --- a/src/PasswordRules.php +++ b/src/PasswordRules.php @@ -36,6 +36,23 @@ public static function changePassword($username, $oldPassword = null) return $rules; } + public static function optionallyChangePassword($username, $oldPassword = null) + { + $rules = self::changePassword($username, $oldPassword); + + $rules = array_merge($rules, [ + 'nullable', + ]); + + foreach ($rules as $key => $rule) { + if (is_string($rule) && $rule === 'required') { + unset($rules[$key]); + } + } + + return $rules; + } + public static function login() { return [ diff --git a/tests/Unit/PasswordRulesTest.php b/tests/Unit/PasswordRulesTest.php index 2756434..6121865 100644 --- a/tests/Unit/PasswordRulesTest.php +++ b/tests/Unit/PasswordRulesTest.php @@ -14,6 +14,8 @@ private function getPasswordRuleSets() PasswordRules::register('username'), PasswordRules::changePassword('username', 'oldPassword'), PasswordRules::changePassword('username'), + PasswordRules::optionallyChangePassword('username', 'oldPassword'), + PasswordRules::optionallyChangePassword('username'), PasswordRules::login(), ]; }