Skip to content

Commit

Permalink
Added facade
Browse files Browse the repository at this point in the history
  • Loading branch information
kielabokkie committed Jan 29, 2020
1 parent 319ea4b commit d2a20c7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
[![Packagist Version](https://img.shields.io/packagist/v/kielabokkie/laravel-conceal.svg?style=flat-square)](https://packagist.org/packages/kielabokkie/laravel-conceal)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)

This package allows you to conceal sensitive data in arrays and collections. It works with Laravel 5.7 or higher.
This package allows you to conceal sensitive data in arrays and collections. This is particularly useful when writing possibly sensitive data to log files.

Once installed you can do things like this:

```php
$data = [
'username' => 'wouter',
'password' => 'secret'
'api_key' => 'secret'
];

$hide = ['password'];
$hide = ['api_key'];

$output = conceal($data, $hide);
print_r($output);

// Outputs: ['username' => 'wouter', 'api_key' => '********']
```

Which will return the array with the password concealed:
## Requirements

```php
[
'username' => 'wouter',
'password' => '********'
]
```
* PHP >= 7.1
* Laravel 5.7+ | 6

## Installation

Expand Down Expand Up @@ -61,7 +60,7 @@ return [

## Usage

Use the defaults configuration to conceal the password:
Use the default configuration to conceal the password:

```php
$data = [
Expand Down Expand Up @@ -89,3 +88,20 @@ print_r($output);

// Outputs: ['api_key' => '********']
```

### Facade

The examples above use the `conceal()` helper function. If Facades are more your thing you can use that instead:

```php
use Kielabokkie\LaravelConceal\Facades\Concealer;

$data = [
'password' => 'secret'
];

$output = Concealer::conceal($data);
print_r($output);

// Outputs: ['password' => '********']
```
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
"laravel": {
"providers": [
"Kielabokkie\\LaravelConceal\\ConcealServiceProvider"
]
],
"aliases": {
"Concealer": "Kielabokkie\\LaravelConceal\\Facades\\Concealer"
}
}
}
}
7 changes: 7 additions & 0 deletions src/ConcealServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kielabokkie\LaravelConceal;

use Illuminate\Support\ServiceProvider;
use Kielabokkie\LaravelConceal\Concealer;

class ConcealServiceProvider extends ServiceProvider
{
Expand All @@ -27,6 +28,12 @@ public function boot()
*/
public function register()
{
// Merge the custom config with the package one
$this->mergeConfigFrom(__DIR__.'/../config/conceal.php', 'conceal');

// Setup the facade
$this->app->bind('concealer', function ($app) {
return new Concealer;
});
}
}
18 changes: 18 additions & 0 deletions src/Facades/Concealer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Kielabokkie\LaravelConceal\Facades;

use Illuminate\Support\Facades\Facade;

class Concealer extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'concealer';
}
}
26 changes: 26 additions & 0 deletions tests/FacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Kielabokkie\LaravelConceal\Tests;

use Kielabokkie\LaravelConceal\Facades\Concealer;
use Kielabokkie\LaravelConceal\Tests\TestCase;

/**
* phpcs:disable PSR1.Methods.CamelCapsMethodName
*/
final class FacadeTest extends TestCase
{
/** @test */
public function it_can_conceal_data_using_the_helper(): void
{
$data = [
'username' => 'wouter',
'password' => 'secret'
];

$output = Concealer::conceal($data);

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

0 comments on commit d2a20c7

Please sign in to comment.