1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\OpenIdConnect\UserInfo\Claim; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
17
|
|
|
|
18
|
|
|
class ClaimManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Claim[] |
22
|
|
|
*/ |
23
|
|
|
private $claims = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Claim $claim |
27
|
|
|
* |
28
|
|
|
* @return ClaimManager |
29
|
|
|
*/ |
30
|
|
|
public function add(Claim $claim): self |
31
|
|
|
{ |
32
|
|
|
$this->claims[$claim->name()] = $claim; |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Claim[] |
39
|
|
|
*/ |
40
|
|
|
public function list(): array |
41
|
|
|
{ |
42
|
|
|
return array_keys($this->claims); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Claim[] |
47
|
|
|
*/ |
48
|
|
|
public function all(): array |
49
|
|
|
{ |
50
|
|
|
return $this->claims; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function has(string $claim): bool |
54
|
|
|
{ |
55
|
|
|
return array_key_exists($claim, $this->claims); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function get(string $claim): Claim |
59
|
|
|
{ |
60
|
|
|
if (!$this->has($claim)) { |
61
|
|
|
throw new \InvalidArgumentException(sprintf('Unsupported claim "%s".', $claim)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->claims[$claim]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array |
68
|
|
|
{ |
69
|
|
|
$result = []; |
70
|
|
|
$claimLocale[] = null; |
|
|
|
|
71
|
|
|
foreach ($claims as $claimName => $config) { |
72
|
|
|
if ($this->has($claimName)) { |
73
|
|
|
$claim = $this->get($claimName); |
74
|
|
|
foreach ($claimLocales as $claimLocale) { |
75
|
|
|
if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) { |
76
|
|
|
$value = $claim->getForUserAccount($userAccount, $claimLocale); |
77
|
|
|
switch (true) { |
78
|
|
|
case is_array($config) && array_key_exists('value', $config): |
79
|
|
|
if ($claim === $config['value']) { |
80
|
|
|
$result[$claimName] = $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
break; |
84
|
|
|
case is_array($config) && array_key_exists('values', $config) && is_array($config['values']): |
85
|
|
|
if (in_array($claim, $config['values'])) { |
86
|
|
|
$result[$claimName] = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
break; |
90
|
|
|
default: |
91
|
|
|
$result[$claimName] = $value; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.