11<?php
22/**
33 * @see https://github.com/zendframework/zend-authentication for the canonical source repository
4- * @copyright Copyright (c) 2013-2018 Zend Technologies USA Inc. (https://www.zend.com)
4+ * @copyright Copyright (c) 2013-2019 Zend Technologies USA Inc. (https://www.zend.com)
55 * @license https://github.com/zendframework/zend-authentication/blob/master/LICENSE.md New BSD License
66 */
77
1515use Zend \Stdlib \ArrayUtils ;
1616use Zend \Validator \AbstractValidator ;
1717
18+ use function is_array ;
19+ use function is_string ;
20+
1821/**
1922 * Authentication Validator
2023 */
@@ -41,6 +44,12 @@ class Authentication extends AbstractValidator
4144 Result::FAILURE_UNCATEGORIZED => self ::UNCATEGORIZED ,
4245 ];
4346
47+ /**
48+ * Authentication\Result codes mapping configurable overrides
49+ * @var string[]
50+ */
51+ protected $ codeMap = [];
52+
4453 /**
4554 * Error Messages
4655 * @var array
@@ -89,18 +98,31 @@ public function __construct($options = null)
8998 }
9099
91100 if (is_array ($ options )) {
92- if (array_key_exists ( 'adapter ' , $ options )) {
101+ if (isset ( $ options [ 'adapter ' ] )) {
93102 $ this ->setAdapter ($ options ['adapter ' ]);
94103 }
95- if (array_key_exists ( 'identity ' , $ options )) {
104+ if (isset ( $ options [ 'identity ' ] )) {
96105 $ this ->setIdentity ($ options ['identity ' ]);
97106 }
98- if (array_key_exists ( 'credential ' , $ options )) {
107+ if (isset ( $ options [ 'credential ' ] )) {
99108 $ this ->setCredential ($ options ['credential ' ]);
100109 }
101- if (array_key_exists ( 'service ' , $ options )) {
110+ if (isset ( $ options [ 'service ' ] )) {
102111 $ this ->setService ($ options ['service ' ]);
103112 }
113+ if (isset ($ options ['code_map ' ])) {
114+ foreach ($ options ['code_map ' ] as $ code => $ template ) {
115+ if (empty ($ template ) || ! is_string ($ template )) {
116+ throw new Exception \InvalidArgumentException (
117+ 'Message key in code_map option must be a non-empty string '
118+ );
119+ }
120+ if (! isset ($ this ->messageTemplates [$ template ])) {
121+ $ this ->messageTemplates [$ template ] = $ this ->messageTemplates [static ::GENERAL ];
122+ }
123+ $ this ->codeMap [(int ) $ code ] = $ template ;
124+ }
125+ }
104126 }
105127 parent ::__construct ($ options );
106128 }
@@ -244,15 +266,27 @@ public function isValid($value = null, $context = null)
244266 return true ;
245267 }
246268
247- $ code = self ::GENERAL ;
248- if (array_key_exists ($ result ->getCode (), self ::CODE_MAP )) {
249- $ code = self ::CODE_MAP [$ result ->getCode ()];
250- }
251- $ this ->error ($ code );
269+ $ messageKey = $ this ->mapResultCodeToMessageKey ($ result ->getCode ());
270+ $ this ->error ($ messageKey );
252271
253272 return false ;
254273 }
255274
275+ /**
276+ * @param int $code Authentication result code
277+ * @return string Message key that should be used for the code
278+ */
279+ protected function mapResultCodeToMessageKey ($ code )
280+ {
281+ if (isset ($ this ->codeMap [$ code ])) {
282+ return $ this ->codeMap [$ code ];
283+ }
284+ if (array_key_exists ($ code , static ::CODE_MAP )) {
285+ return static ::CODE_MAP [$ code ];
286+ }
287+ return self ::GENERAL ;
288+ }
289+
256290 /**
257291 * @return ValidatableAdapterInterface
258292 * @throws Exception\RuntimeException if no adapter present in
0 commit comments