flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-logs
/
src
/
Processor
/
SensitiveDataProcessor.php
SensitiveDataProcessor.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Logger\Processor; |
| 4 | |
| 5 | use FSVendor\Monolog\Processor\ProcessorInterface; |
| 6 | /** |
| 7 | * Can replace data in log. |
| 8 | * Ie. sensitive data. |
| 9 | * |
| 10 | * @package WPDesk\Logger\Processor |
| 11 | */ |
| 12 | class SensitiveDataProcessor implements ProcessorInterface |
| 13 | { |
| 14 | /** |
| 15 | * Replace array. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | private array $replace; |
| 20 | public function __construct(array $replace) |
| 21 | { |
| 22 | $this->replace = $replace; |
| 23 | } |
| 24 | public function __invoke(array $record): array |
| 25 | { |
| 26 | return $this->replace_array($record); |
| 27 | } |
| 28 | private function replace_array(array $value): array |
| 29 | { |
| 30 | foreach ($value as $key => $item) { |
| 31 | if (is_array($item)) { |
| 32 | $value[$key] = $this->replace_array($item); |
| 33 | } |
| 34 | if (is_string($item)) { |
| 35 | $value[$key] = $this->replace($item); |
| 36 | } |
| 37 | } |
| 38 | return $value; |
| 39 | } |
| 40 | private function replace(string $value): string |
| 41 | { |
| 42 | foreach ($this->replace as $search => $replace) { |
| 43 | $value = str_replace($search, $replace, $value); |
| 44 | } |
| 45 | return $value; |
| 46 | } |
| 47 | } |
| 48 |