1: <?php
2: namespace Worldline\Connect\Sdk\Logging;
3:
4: /**
5: * Class ValueObfuscator
6: *
7: * @package Worldline\Connect\Sdk\Logging
8: */
9: class ValueObfuscator
10: {
11: /** */
12: const MASK_CHARACTER = '*';
13:
14: /**
15: * @param string $value
16: * @param int $numberOfCharactersToKeep
17: * @return string
18: */
19: public function obfuscateAllKeepEnd($value, $numberOfCharactersToKeep)
20: {
21: if ($numberOfCharactersToKeep <= 0) {
22: return $this->obfuscateAll($value);
23: }
24: if (mb_strlen($value, 'UTF-8') <= $numberOfCharactersToKeep) {
25: return $value;
26: }
27: return
28: str_repeat(static::MASK_CHARACTER, mb_strlen($value, 'UTF-8') - $numberOfCharactersToKeep) .
29: mb_substr($value, mb_strlen($value, 'UTF-8') - $numberOfCharactersToKeep, null, 'UTF-8')
30: ;
31: }
32:
33: /**
34: * @param string $value
35: * @param int $numberOfCharactersToKeep
36: * @return string
37: */
38: public function obfuscateAllKeepStart($value, $numberOfCharactersToKeep)
39: {
40: if ($numberOfCharactersToKeep <= 0) {
41: return $this->obfuscateAll($value);
42: }
43: if (mb_strlen($value, 'UTF-8') <= $numberOfCharactersToKeep) {
44: return $value;
45: }
46: return
47: mb_substr($value, 0, $numberOfCharactersToKeep, 'UTF-8') .
48: str_repeat(static::MASK_CHARACTER, mb_strlen($value, 'UTF-8') - $numberOfCharactersToKeep)
49: ;
50: }
51:
52: /**
53: * @param string $value
54: * @return string
55: */
56: public function obfuscateAll($value)
57: {
58: return str_repeat(static::MASK_CHARACTER, mb_strlen($value, 'UTF-8'));
59: }
60:
61: /**
62: * @param int $length
63: * @return string
64: */
65: public function obfuscateFixedLength($length)
66: {
67: if ($length <= 0) {
68: return '';
69: }
70: return str_repeat(static::MASK_CHARACTER, $length);
71: }
72: }
73: