Skip to content

Commit

Permalink
Merge pull request #239 from TransbankDevelopers/fix/warning-when-inp…
Browse files Browse the repository at this point in the history
…ut-is-null

fix: warning when input is null
  • Loading branch information
mastudillot authored Jun 3, 2024
2 parents ef14801 + 8b10ce5 commit 0dfc8e7
Showing 1 changed file with 76 additions and 52 deletions.
128 changes: 76 additions & 52 deletions plugin/src/Helpers/MaskData.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace Transbank\WooCommerce\WebpayRest\Helpers;

use Transbank\Webpay\Options;

class MaskData
{

Expand Down Expand Up @@ -30,7 +32,8 @@ class MaskData
protected $isIntegration;
protected $log;

public function __construct($environment){
public function __construct($environment)
{
$this->isIntegration = $environment == Options::ENVIRONMENT_INTEGRATION;
$this->log = TbkFactory::createLogger();
}
Expand All @@ -42,7 +45,8 @@ public function __construct($environment){
* @param string $input a string to be masked.
* @return string a string with substrings masked.
*/
private function maskWithFormat($input){
private function maskWithFormat($input)
{
return preg_replace_callback('/(?<=-).+?(?=-)/', function ($matches) {
return str_repeat('x', strlen($matches[0]));
}, $input);
Expand All @@ -56,18 +60,23 @@ private function maskWithFormat($input){
* @param int $charsToKeep number of original chars to keep at start and end.
* @return string a string masked.
*/
private function mask($input, $pattern = null, $charsToKeep = 4 ){
private function mask($input, $pattern = null, $charsToKeep = 4)
{
if(is_null($input)) {
return '';
}

$len = strlen($input);

if ( $pattern != null ) {
$patternPos = strpos($input, $pattern);
if ( $patternPos === 0 ) {
if ($pattern != null) {
$patternPos = strpos($input, $pattern);
if ($patternPos === 0) {
$startString = $pattern;
}
else {
} else {
$endString = $pattern;
}
}

$startString = $startString ?? substr($input, 0, $charsToKeep);
$endString = $endString ?? substr($input, -$charsToKeep, $charsToKeep);
$charsToReplace = $len - (strlen($startString) + strlen($endString));
Expand All @@ -81,7 +90,8 @@ private function mask($input, $pattern = null, $charsToKeep = 4 ){
* @param string $email An email to be masked.
* @return string email masked.
*/
private function maskEmail($email){
private function maskEmail($email)
{
return preg_replace_callback('/^(.{1,4})[^@]*(@.*)$/', function ($match) {
return $match[1] . str_repeat('x', strlen($match[0]) - strlen($match[1]) - strlen($match[2])) . $match[2];
}, $email);
Expand All @@ -94,14 +104,16 @@ private function maskEmail($email){
* @param string $pattern A pattern to maintain, like `child` or `sessionId`.
* @return string input masked.
*/
private function maskWithPattern($input, $pattern){
private function maskWithPattern($input, $pattern)
{
$regexPattern = "/(wc:($pattern:)?\w{2})\w+:(\w{2})/";
return preg_replace_callback($regexPattern, function($matches) use ($input){
$prefix = $matches[1];
$suffix = $matches[3];
$maskLength = strlen($input) - strlen($prefix) - strlen($suffix) - 1;
return $prefix . str_repeat('x', $maskLength) . $suffix;
}, $input);

return preg_replace_callback($regexPattern, function ($matches) use ($input) {
$prefix = $matches[1];
$suffix = $matches[3];
$maskLength = strlen($input) - strlen($prefix) - strlen($suffix) - 1;
return $prefix . str_repeat('x', $maskLength) . $suffix;
}, $input);
}

/**
Expand All @@ -111,10 +123,12 @@ private function maskWithPattern($input, $pattern){
* @param string $buyOrder An string with buy order to mask.
* @return string buy order masked.
*/
public function maskBuyOrder($buyOrder){
public function maskBuyOrder($buyOrder)
{
if ($this->isIntegration) {
return $buyOrder;
}

$pattern = 'child';
return $this->maskWithPattern($buyOrder, $pattern);
}
Expand All @@ -126,10 +140,12 @@ public function maskBuyOrder($buyOrder){
* @param string $sessionId An string with session id to mask.
* @return string session id masked.
*/
public function maskSessionId($sessionId){
if($this->isIntegration){
public function maskSessionId($sessionId)
{
if ($this->isIntegration) {
return $sessionId;
}

$sessionIdPattern = 'sessionId';
return $this->maskWithPattern($sessionId, $sessionIdPattern);
}
Expand All @@ -140,7 +156,8 @@ public function maskSessionId($sessionId){
* @param array $array an array to evaluate
* @return boolean `true` if is associative, `false` otherwise
*/
private function isAssociative($array) {
private function isAssociative($array)
{
return (bool)count(array_filter(array_keys($array), 'is_string'));
}

Expand All @@ -151,14 +168,15 @@ private function isAssociative($array) {
* @param array $data An array containing data to mask.
* @return array copy of input, with fields masked.
*/
public function maskData($data){
try{
if ($this->isIntegration){
public function maskData($data)
{
try {
if ($this->isIntegration) {
return $data;
}
$newData = $this->copyWithSubArray($data);
foreach ($newData as $key => $value) {
switch($this->getValueType($value)){
switch ($this->getValueType($value)) {
case $this::OBJECT:
$newData[$key] = $this->maskObject($value);
break;
Expand All @@ -175,8 +193,7 @@ public function maskData($data){
}
}
return $newData;
}
catch (\Exception $e){
} catch (\Exception $e) {
$this->log->logError('Error on Mask Data: ' . $e->getMessage());
return $data;
}
Expand All @@ -189,17 +206,19 @@ public function maskData($data){
* @param array $array An array to be copied.
* @return array copy of input array.
*/
private function copyWithSubArray($array){
private function copyWithSubArray($array)
{
$newArray = null;
foreach ($array as $key => $value) {
if (is_array($value)) {
$clonedValue = array_map(function ($item) {
return is_object($item) ? clone $item : $item;
},
$value);
$clonedValue = array_map(
function ($item) {
return is_object($item) ? clone $item : $item;
},
$value
);
$newArray[$key] = $clonedValue;
}
else {
} else {
$newArray[$key] = is_object($value) ? clone $value : $value;
}
}
Expand All @@ -213,11 +232,14 @@ private function copyWithSubArray($array){
* @param mixed $value the value to be masked.
* @return mixed masked value if key exists, `false` otherwise.
*/
private function getMaskedValue($key, $value){
private function getMaskedValue($key, $value)
{
$keyExists = array_key_exists($key, $this->keysToMask);
if($keyExists){

if ($keyExists) {
return call_user_func([$this, $this->keysToMask[$key]], $value);
}

return $value;
}

Expand All @@ -227,16 +249,18 @@ private function getMaskedValue($key, $value){
* @param string $value to evaluate.
* @return int a constant representing the type of element
*/
private function getValueType($value){
private function getValueType($value)
{
$valueType = $this::NO_ITERABLE;
if(is_object($value)){

if (is_object($value)) {
$valueType = $this::OBJECT;
}
if (is_array($value)){
if ($this->isAssociative($value)){

if (is_array($value)) {
if ($this->isAssociative($value)) {
$valueType = $this::ASSOCIATIVE_ARRAY;
}
else {
} else {
$valueType = $this::INDEXED_ARRAY;
}
}
Expand All @@ -250,8 +274,9 @@ private function getValueType($value){
* @param object $object a reference to an object to mask
* @return object the object with masked attributes
*/
private function maskObject($object){
foreach($object as $detailKey => $detailValue) {
private function maskObject($object)
{
foreach ($object as $detailKey => $detailValue) {
$maskedValue = $this->getMaskedValue($detailKey, $detailValue);
$object->$detailKey = $maskedValue;
}
Expand All @@ -264,8 +289,9 @@ private function maskObject($object){
* @param array $array the array to mask.
* @return array the array with masked values.
*/
private function maskAssociativeArray($array){
foreach($array as $detailKey => $detailValue) {
private function maskAssociativeArray($array)
{
foreach ($array as $detailKey => $detailValue) {
$maskedValue = $this->getMaskedValue($detailKey, $detailValue);
$array[$detailKey] = $maskedValue;
}
Expand All @@ -278,17 +304,15 @@ private function maskAssociativeArray($array){
* @param array $array the array to mask.
* @return array the array with masked values.
*/
private function maskIndexedArray($array){
foreach($array as $detail) {
if(is_object($detail)) {
private function maskIndexedArray($array)
{
foreach ($array as $detail) {
if (is_object($detail)) {
$detail = $this->maskObject($detail);
}
else {
} else {
$detail = $this->maskAssociativeArray($array);
}

}
return $array;
}

}

0 comments on commit 0dfc8e7

Please sign in to comment.