Skip to content

Commit

Permalink
Merge pull request #4 from kielabokkie/hotfix/check-key-early
Browse files Browse the repository at this point in the history
Conceal data early when key is found
  • Loading branch information
kielabokkie authored May 21, 2020
2 parents 860ca1d + 0477a40 commit 611d8ce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/Concealer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kielabokkie\LaravelConceal;

use Illuminate\Support\Collection;
use Kielabokkie\LaravelConceal\Exceptions\NotSupportedException;

class Concealer
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public function conceal($input, array $keys = [])
return $output;
}

throw new \Exception('Only Collections or Arrays are supported');
throw new NotSupportedException;
}

/**
Expand All @@ -47,8 +48,8 @@ public function conceal($input, array $keys = [])
private function handleCollection($input)
{
$output = $input->map(function ($item, $key) {
if (is_string($item) === true) {
return $this->handleString($key, $item);
if (in_array($key, $this->keys) === true) {
return $input[$key] = '********';
}

if ($item instanceof Collection) {
Expand All @@ -74,36 +75,22 @@ private function handleCollection($input)
private function handleArray($input)
{
foreach ($input as $key => $item) {
if (is_string($item) === true) {
$input[$key] = $this->handleString($key, $item);
if (in_array($key, $this->keys) === true) {
$input[$key] = '********';
continue;
}

if (is_array($item) === true) {
$input[$key] = $this->handleArray($item);
continue;
}

if ($item instanceof Collection) {
$input[$key] = $this->handleCollection($item);
continue;
}
}

return $input;
}

/**
* Return concealed string if the key matches one of keys to be concealed.
*
* @param string $key
* @param string $value
* @return string
*/
private function handleString($key, $value)
{
// If the key is
if (in_array($key, $this->keys) === true) {
return '********';
}

return $value;
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/NotSupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Kielabokkie\LaravelConceal\Exceptions;

use Exception;
use Throwable;

class NotSupportedException extends Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$message = 'Only Collections or Arrays are supported';

parent::__construct($message, $code, $previous);
}
}
43 changes: 43 additions & 0 deletions tests/ConcealerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Kielabokkie\LaravelConceal\Concealer;
use Kielabokkie\LaravelConceal\Exceptions\NotSupportedException;
use Kielabokkie\LaravelConceal\Tests\TestCase;

/**
Expand Down Expand Up @@ -173,6 +174,40 @@ public function it_conceals_mixed_objecs(): void
$this->assertEquals('********', $output['collection']['collection']['password']);
}

/** @test */
public function it_handles_numeric_keys_for_arrays(): void
{
$data = [
'username' => 'wouter',
'password' => [
'test',
'secret',
]
];

$output = $this->concealer->conceal($data);

$this->assertEquals('wouter', $output['username']);
$this->assertEquals('********', $output['password']);
}

/** @test */
public function it_handles_numeric_keys_for_collections(): void
{
$data = new Collection([
'username' => 'wouter',
'password' => new Collection([
'test',
'secret',
])
]);

$output = $this->concealer->conceal($data);

$this->assertEquals('wouter', $output['username']);
$this->assertEquals('********', $output['password']);
}

/** @test */
public function it_returns_an_array_if_array_is_given(): void
{
Expand All @@ -198,4 +233,12 @@ public function it_returns_a_collection_if_collection_is_given(): void
// Input was a collection so output should be too
$this->assertInstanceOf(Collection::class, $output);
}

/** @test */
public function it_throws_an_exception_for_non_supported_types(): void
{
$this->expectException(NotSupportedException::class);

$this->concealer->conceal('password');
}
}

0 comments on commit 611d8ce

Please sign in to comment.