1
1
<?php
2
2
/**
3
3
* @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)
5
5
* @license https://github.com/zendframework/zend-authentication/blob/master/LICENSE.md New BSD License
6
6
*/
7
7
15
15
use Zend \Stdlib \ArrayUtils ;
16
16
use Zend \Validator \AbstractValidator ;
17
17
18
+ use function is_array ;
19
+ use function is_string ;
20
+
18
21
/**
19
22
* Authentication Validator
20
23
*/
@@ -41,6 +44,12 @@ class Authentication extends AbstractValidator
41
44
Result::FAILURE_UNCATEGORIZED => self ::UNCATEGORIZED ,
42
45
];
43
46
47
+ /**
48
+ * Authentication\Result codes mapping configurable overrides
49
+ * @var string[]
50
+ */
51
+ protected $ codeMap = [];
52
+
44
53
/**
45
54
* Error Messages
46
55
* @var array
@@ -89,18 +98,31 @@ public function __construct($options = null)
89
98
}
90
99
91
100
if (is_array ($ options )) {
92
- if (array_key_exists ( 'adapter ' , $ options )) {
101
+ if (isset ( $ options [ 'adapter ' ] )) {
93
102
$ this ->setAdapter ($ options ['adapter ' ]);
94
103
}
95
- if (array_key_exists ( 'identity ' , $ options )) {
104
+ if (isset ( $ options [ 'identity ' ] )) {
96
105
$ this ->setIdentity ($ options ['identity ' ]);
97
106
}
98
- if (array_key_exists ( 'credential ' , $ options )) {
107
+ if (isset ( $ options [ 'credential ' ] )) {
99
108
$ this ->setCredential ($ options ['credential ' ]);
100
109
}
101
- if (array_key_exists ( 'service ' , $ options )) {
110
+ if (isset ( $ options [ 'service ' ] )) {
102
111
$ this ->setService ($ options ['service ' ]);
103
112
}
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
+ }
104
126
}
105
127
parent ::__construct ($ options );
106
128
}
@@ -244,15 +266,27 @@ public function isValid($value = null, $context = null)
244
266
return true ;
245
267
}
246
268
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 );
252
271
253
272
return false ;
254
273
}
255
274
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
+
256
290
/**
257
291
* @return ValidatableAdapterInterface
258
292
* @throws Exception\RuntimeException if no adapter present in
0 commit comments