1
1
<?php
2
2
3
3
namespace Transbank \WooCommerce \WebpayRest \Helpers ;
4
+
4
5
use Transbank \Webpay \Options ;
6
+
5
7
class MaskData
6
8
{
7
9
@@ -30,7 +32,8 @@ class MaskData
30
32
protected $ isIntegration ;
31
33
protected $ log ;
32
34
33
- public function __construct ($ environment ){
35
+ public function __construct ($ environment )
36
+ {
34
37
$ this ->isIntegration = $ environment == Options::ENVIRONMENT_INTEGRATION ;
35
38
$ this ->log = TbkFactory::createLogger ();
36
39
}
@@ -42,7 +45,8 @@ public function __construct($environment){
42
45
* @param string $input a string to be masked.
43
46
* @return string a string with substrings masked.
44
47
*/
45
- private function maskWithFormat ($ input ){
48
+ private function maskWithFormat ($ input )
49
+ {
46
50
return preg_replace_callback ('/(?<=-).+?(?=-)/ ' , function ($ matches ) {
47
51
return str_repeat ('x ' , strlen ($ matches [0 ]));
48
52
}, $ input );
@@ -56,19 +60,20 @@ private function maskWithFormat($input){
56
60
* @param int $charsToKeep number of original chars to keep at start and end.
57
61
* @return string a string masked.
58
62
*/
59
- private function mask ($ input , $ pattern = null , $ charsToKeep = 4 ){
63
+ private function mask ($ input , $ pattern = null , $ charsToKeep = 4 )
64
+ {
60
65
$ cleanInput = $ input ?? '' ;
61
66
$ len = strlen ($ cleanInput );
62
67
63
- if ( $ pattern != null ) {
64
- $ patternPos = strpos ($ cleanInput , $ pattern );
65
- if ( $ patternPos === 0 ) {
68
+ if ($ pattern != null ) {
69
+ $ patternPos = strpos ($ cleanInput , $ pattern );
70
+ if ($ patternPos === 0 ) {
66
71
$ startString = $ pattern ;
67
- }
68
- else {
72
+ } else {
69
73
$ endString = $ pattern ;
70
74
}
71
75
}
76
+
72
77
$ startString = $ startString ?? substr ($ cleanInput , 0 , $ charsToKeep );
73
78
$ endString = $ endString ?? substr ($ cleanInput , -$ charsToKeep , $ charsToKeep );
74
79
$ charsToReplace = $ len - (strlen ($ startString ) + strlen ($ endString ));
@@ -82,7 +87,8 @@ private function mask($input, $pattern = null, $charsToKeep = 4 ){
82
87
* @param string $email An email to be masked.
83
88
* @return string email masked.
84
89
*/
85
- private function maskEmail ($ email ){
90
+ private function maskEmail ($ email )
91
+ {
86
92
return preg_replace_callback ('/^(.{1,4})[^@]*(@.*)$/ ' , function ($ match ) {
87
93
return $ match [1 ] . str_repeat ('x ' , strlen ($ match [0 ]) - strlen ($ match [1 ]) - strlen ($ match [2 ])) . $ match [2 ];
88
94
}, $ email );
@@ -95,14 +101,16 @@ private function maskEmail($email){
95
101
* @param string $pattern A pattern to maintain, like `child` or `sessionId`.
96
102
* @return string input masked.
97
103
*/
98
- private function maskWithPattern ($ input , $ pattern ){
104
+ private function maskWithPattern ($ input , $ pattern )
105
+ {
99
106
$ regexPattern = "/(wc:( $ pattern:)?\w{2})\w+:(\w{2})/ " ;
100
- return preg_replace_callback ($ regexPattern , function ($ matches ) use ($ input ){
101
- $ prefix = $ matches [1 ];
102
- $ suffix = $ matches [3 ];
103
- $ maskLength = strlen ($ input ) - strlen ($ prefix ) - strlen ($ suffix ) - 1 ;
104
- return $ prefix . str_repeat ('x ' , $ maskLength ) . $ suffix ;
105
- }, $ input );
107
+
108
+ return preg_replace_callback ($ regexPattern , function ($ matches ) use ($ input ) {
109
+ $ prefix = $ matches [1 ];
110
+ $ suffix = $ matches [3 ];
111
+ $ maskLength = strlen ($ input ) - strlen ($ prefix ) - strlen ($ suffix ) - 1 ;
112
+ return $ prefix . str_repeat ('x ' , $ maskLength ) . $ suffix ;
113
+ }, $ input );
106
114
}
107
115
108
116
/**
@@ -112,10 +120,12 @@ private function maskWithPattern($input, $pattern){
112
120
* @param string $buyOrder An string with buy order to mask.
113
121
* @return string buy order masked.
114
122
*/
115
- public function maskBuyOrder ($ buyOrder ){
123
+ public function maskBuyOrder ($ buyOrder )
124
+ {
116
125
if ($ this ->isIntegration ) {
117
126
return $ buyOrder ;
118
127
}
128
+
119
129
$ pattern = 'child ' ;
120
130
return $ this ->maskWithPattern ($ buyOrder , $ pattern );
121
131
}
@@ -127,10 +137,12 @@ public function maskBuyOrder($buyOrder){
127
137
* @param string $sessionId An string with session id to mask.
128
138
* @return string session id masked.
129
139
*/
130
- public function maskSessionId ($ sessionId ){
131
- if ($ this ->isIntegration ){
140
+ public function maskSessionId ($ sessionId )
141
+ {
142
+ if ($ this ->isIntegration ) {
132
143
return $ sessionId ;
133
144
}
145
+
134
146
$ sessionIdPattern = 'sessionId ' ;
135
147
return $ this ->maskWithPattern ($ sessionId , $ sessionIdPattern );
136
148
}
@@ -141,7 +153,8 @@ public function maskSessionId($sessionId){
141
153
* @param array $array an array to evaluate
142
154
* @return boolean `true` if is associative, `false` otherwise
143
155
*/
144
- private function isAssociative ($ array ) {
156
+ private function isAssociative ($ array )
157
+ {
145
158
return (bool )count (array_filter (array_keys ($ array ), 'is_string ' ));
146
159
}
147
160
@@ -152,14 +165,15 @@ private function isAssociative($array) {
152
165
* @param array $data An array containing data to mask.
153
166
* @return array copy of input, with fields masked.
154
167
*/
155
- public function maskData ($ data ){
156
- try {
157
- if ($ this ->isIntegration ){
168
+ public function maskData ($ data )
169
+ {
170
+ try {
171
+ if ($ this ->isIntegration ) {
158
172
return $ data ;
159
173
}
160
174
$ newData = $ this ->copyWithSubArray ($ data );
161
175
foreach ($ newData as $ key => $ value ) {
162
- switch ($ this ->getValueType ($ value )){
176
+ switch ($ this ->getValueType ($ value )) {
163
177
case $ this ::OBJECT :
164
178
$ newData [$ key ] = $ this ->maskObject ($ value );
165
179
break ;
@@ -176,8 +190,7 @@ public function maskData($data){
176
190
}
177
191
}
178
192
return $ newData ;
179
- }
180
- catch (\Exception $ e ){
193
+ } catch (\Exception $ e ) {
181
194
$ this ->log ->logError ('Error on Mask Data: ' . $ e ->getMessage ());
182
195
return $ data ;
183
196
}
@@ -190,17 +203,19 @@ public function maskData($data){
190
203
* @param array $array An array to be copied.
191
204
* @return array copy of input array.
192
205
*/
193
- private function copyWithSubArray ($ array ){
206
+ private function copyWithSubArray ($ array )
207
+ {
194
208
$ newArray = null ;
195
209
foreach ($ array as $ key => $ value ) {
196
210
if (is_array ($ value )) {
197
- $ clonedValue = array_map (function ($ item ) {
198
- return is_object ($ item ) ? clone $ item : $ item ;
199
- },
200
- $ value );
211
+ $ clonedValue = array_map (
212
+ function ($ item ) {
213
+ return is_object ($ item ) ? clone $ item : $ item ;
214
+ },
215
+ $ value
216
+ );
201
217
$ newArray [$ key ] = $ clonedValue ;
202
- }
203
- else {
218
+ } else {
204
219
$ newArray [$ key ] = is_object ($ value ) ? clone $ value : $ value ;
205
220
}
206
221
}
@@ -214,11 +229,14 @@ private function copyWithSubArray($array){
214
229
* @param mixed $value the value to be masked.
215
230
* @return mixed masked value if key exists, `false` otherwise.
216
231
*/
217
- private function getMaskedValue ($ key , $ value ){
232
+ private function getMaskedValue ($ key , $ value )
233
+ {
218
234
$ keyExists = array_key_exists ($ key , $ this ->keysToMask );
219
- if ($ keyExists ){
235
+
236
+ if ($ keyExists ) {
220
237
return call_user_func ([$ this , $ this ->keysToMask [$ key ]], $ value );
221
238
}
239
+
222
240
return $ value ;
223
241
}
224
242
@@ -228,16 +246,18 @@ private function getMaskedValue($key, $value){
228
246
* @param string $value to evaluate.
229
247
* @return int a constant representing the type of element
230
248
*/
231
- private function getValueType ($ value ){
249
+ private function getValueType ($ value )
250
+ {
232
251
$ valueType = $ this ::NO_ITERABLE ;
233
- if (is_object ($ value )){
252
+
253
+ if (is_object ($ value )) {
234
254
$ valueType = $ this ::OBJECT ;
235
255
}
236
- if (is_array ($ value )){
237
- if ($ this ->isAssociative ($ value )){
256
+
257
+ if (is_array ($ value )) {
258
+ if ($ this ->isAssociative ($ value )) {
238
259
$ valueType = $ this ::ASSOCIATIVE_ARRAY ;
239
- }
240
- else {
260
+ } else {
241
261
$ valueType = $ this ::INDEXED_ARRAY ;
242
262
}
243
263
}
@@ -251,8 +271,9 @@ private function getValueType($value){
251
271
* @param object $object a reference to an object to mask
252
272
* @return object the object with masked attributes
253
273
*/
254
- private function maskObject ($ object ){
255
- foreach ($ object as $ detailKey => $ detailValue ) {
274
+ private function maskObject ($ object )
275
+ {
276
+ foreach ($ object as $ detailKey => $ detailValue ) {
256
277
$ maskedValue = $ this ->getMaskedValue ($ detailKey , $ detailValue );
257
278
$ object ->$ detailKey = $ maskedValue ;
258
279
}
@@ -265,8 +286,9 @@ private function maskObject($object){
265
286
* @param array $array the array to mask.
266
287
* @return array the array with masked values.
267
288
*/
268
- private function maskAssociativeArray ($ array ){
269
- foreach ($ array as $ detailKey => $ detailValue ) {
289
+ private function maskAssociativeArray ($ array )
290
+ {
291
+ foreach ($ array as $ detailKey => $ detailValue ) {
270
292
$ maskedValue = $ this ->getMaskedValue ($ detailKey , $ detailValue );
271
293
$ array [$ detailKey ] = $ maskedValue ;
272
294
}
@@ -279,17 +301,15 @@ private function maskAssociativeArray($array){
279
301
* @param array $array the array to mask.
280
302
* @return array the array with masked values.
281
303
*/
282
- private function maskIndexedArray ($ array ){
283
- foreach ($ array as $ detail ) {
284
- if (is_object ($ detail )) {
304
+ private function maskIndexedArray ($ array )
305
+ {
306
+ foreach ($ array as $ detail ) {
307
+ if (is_object ($ detail )) {
285
308
$ detail = $ this ->maskObject ($ detail );
286
- }
287
- else {
309
+ } else {
288
310
$ detail = $ this ->maskAssociativeArray ($ array );
289
311
}
290
-
291
312
}
292
313
return $ array ;
293
314
}
294
-
295
315
}
0 commit comments