8
8
use Compubapi \EncryptedData ;
9
9
use Attrpubapi \Attribute as ProtobufAttribute ;
10
10
use Yoti \Exception \AttributeException ;
11
+ use Yoti \Entity \MultiValue ;
11
12
12
13
class AttributeConverter
13
14
{
14
15
const CONTENT_TYPE_JSON = 5 ;
15
16
const CONTENT_TYPE_PNG = 4 ;
16
17
const CONTENT_TYPE_JPEG = 2 ;
17
18
const CONTENT_TYPE_DATE = 3 ;
19
+ const CONTENT_TYPE_MULTI_VALUE = 6 ;
18
20
19
21
/**
20
22
* @param ProtobufAttribute $attribute
@@ -25,13 +27,20 @@ class AttributeConverter
25
27
*/
26
28
private static function convertValueBasedOnAttributeName ($ value , $ attrName )
27
29
{
28
- self ::validateInput ($ value, $ attrName );
30
+ self ::validateInput ($ value );
29
31
30
- switch ($ attrName )
31
- {
32
+ switch ($ attrName ) {
32
33
case Profile::ATTR_DOCUMENT_DETAILS :
33
34
return new DocumentDetails ($ value );
34
35
36
+ case Profile::ATTR_DOCUMENT_IMAGES :
37
+ if (!($ value instanceof MultiValue)) {
38
+ return null ;
39
+ }
40
+ return $ value
41
+ ->filterInstance (Image::class)
42
+ ->getArrayCopy ();
43
+
35
44
default :
36
45
return $ value ;
37
46
}
@@ -45,16 +54,11 @@ private static function convertValueBasedOnAttributeName($value, $attrName)
45
54
*
46
55
* @throws AttributeException
47
56
*/
48
- private static function convertValueBasedOnContentType (ProtobufAttribute $ protobufAttribute )
57
+ private static function convertValueBasedOnContentType ($ value , $ contentType )
49
58
{
50
- $ value = $ protobufAttribute ->getValue ();
51
- $ contentType = $ protobufAttribute ->getContentType ();
52
- $ attrName = $ protobufAttribute ->getName ();
59
+ self ::validateInput ($ value );
53
60
54
- self ::validateInput ($ value , $ attrName );
55
-
56
- switch ($ contentType )
57
- {
61
+ switch ($ contentType ) {
58
62
case self ::CONTENT_TYPE_JPEG :
59
63
case self ::CONTENT_TYPE_PNG :
60
64
$ imageExtension = self ::imageTypeToExtension ($ contentType );
@@ -65,18 +69,50 @@ private static function convertValueBasedOnContentType(ProtobufAttribute $protob
65
69
// Convert JSON string to an array
66
70
$ value = json_decode ($ value , true );
67
71
if (json_last_error ()) {
68
- throw new AttributeException ("Error converting attr { $ attrName } to a JSON Object " );
72
+ throw new AttributeException ("Error converting attr to a JSON Object " );
69
73
}
70
74
break ;
71
75
72
76
case self ::CONTENT_TYPE_DATE :
73
77
$ value = self ::convertTimestampToDate ($ value );
74
78
break ;
79
+
80
+ case self ::CONTENT_TYPE_MULTI_VALUE :
81
+ $ value = self ::convertMultiValue ($ value );
82
+ break ;
75
83
}
76
84
77
85
return $ value ;
78
86
}
79
87
88
+ /**
89
+ * Convert attribute value to MultiValue.
90
+ *
91
+ * @param string $value
92
+ * @return MultiValue
93
+ */
94
+ private function convertMultiValue ($ value )
95
+ {
96
+ $ protoMultiValue = new \Attrpubapi \MultiValue ();
97
+ $ protoMultiValue ->mergeFromString ($ value );
98
+ $ items = [];
99
+ foreach ($ protoMultiValue ->getValues () as $ protoValue ) {
100
+ $ item = null ;
101
+ try {
102
+ $ item = self ::convertValueBasedOnContentType (
103
+ $ protoValue ->getData (),
104
+ $ protoValue ->getContentType ()
105
+ );
106
+ } catch (AttributeException $ e ) {
107
+ error_log ($ e ->getMessage () . " (MultiValue Value ContentType: {$ protoValue ->getContentType ()}) " , 0 );
108
+ } catch (\Exception $ e ) {
109
+ error_log ($ e ->getMessage (), 0 );
110
+ }
111
+ $ items [] = $ item ;
112
+ }
113
+ return new MultiValue ($ items );
114
+ }
115
+
80
116
/**
81
117
* Convert Protobuf Image type to an image extension.
82
118
*
@@ -88,8 +124,7 @@ private static function imageTypeToExtension($type)
88
124
{
89
125
$ type = (int )$ type ;
90
126
91
- switch ($ type )
92
- {
127
+ switch ($ type ) {
93
128
case 2 :
94
129
$ format = 'JPEG ' ;
95
130
break ;
@@ -129,14 +164,17 @@ public static function getEncryptedData($data)
129
164
*/
130
165
public static function convertToYotiAttribute (ProtobufAttribute $ protobufAttribute )
131
166
{
167
+ $ yotiAttribute = null ;
168
+
132
169
try {
133
170
$ yotiAnchorsMap = AnchorListConverter::convert (
134
171
$ protobufAttribute ->getAnchors ()
135
172
);
136
- $ attrName = $ protobufAttribute ->getName ();
137
173
$ attrValue = AttributeConverter::convertValueBasedOnContentType (
138
- $ protobufAttribute
174
+ $ protobufAttribute ->getValue (),
175
+ $ protobufAttribute ->getContentType ()
139
176
);
177
+ $ attrName = $ protobufAttribute ->getName ();
140
178
$ attrValue = AttributeConverter::convertValueBasedOnAttributeName (
141
179
$ attrValue ,
142
180
$ attrName
@@ -146,8 +184,9 @@ public static function convertToYotiAttribute(ProtobufAttribute $protobufAttribu
146
184
$ attrValue ,
147
185
$ yotiAnchorsMap
148
186
);
187
+ } catch (AttributeException $ e ) {
188
+ error_log ($ e ->getMessage () . " (Attribute: {$ protobufAttribute ->getName ()}) " , 0 );
149
189
} catch (\Exception $ e ) {
150
- $ yotiAttribute = NULL ;
151
190
error_log ($ e ->getMessage (), 0 );
152
191
}
153
192
@@ -166,14 +205,13 @@ public static function convertTimestampToDate($value)
166
205
167
206
/**
168
207
* @param string $value
169
- * @param string $attrName
170
208
*
171
209
* @throws AttributeException
172
210
*/
173
- private static function validateInput ($ value, $ attrName )
211
+ private static function validateInput ($ value )
174
212
{
175
213
if (empty ($ value )) {
176
- throw new AttributeException ("Warning: { $ attrName } value is NULL " );
214
+ throw new AttributeException ("Warning: Value is NULL " );
177
215
}
178
216
}
179
- }
217
+ }
0 commit comments