Skip to content

Commit f2bded5

Browse files
Merge pull request #4 from dynamik-dev/task/refactor-2
add helper, readme update
2 parents d729a4b + 7a99e8e commit f2bded5

6 files changed

Lines changed: 97 additions & 17 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
# Cloak
22

3+
![Cloak PHP Example](cloak-php.png)
4+
35
A simple, extensible PHP package for redacting sensitive data from strings and revealing them later.
46

57
```php
6-
use DynamikDev\Cloak\Cloak;
7-
8-
$cloak = Cloak::make();
9-
10-
$cloaked = $cloak->cloak('Email me at john@example.com');
8+
$cloaked = cloak('Email me at john@example.com');
119
// "Email me at {{EMAIL_x7k2m9_1}}"
1210

13-
$original = $cloak->uncloak($cloaked);
11+
$original = uncloak($cloaked);
1412
// "Email me at john@example.com"
1513
```
1614

@@ -27,18 +25,14 @@ composer require dynamik-dev/cloak-php
2725

2826
## Quick Start
2927

30-
### Basic Usage
28+
### Using Helper Functions
3129

3230
```php
33-
use DynamikDev\Cloak\Cloak;
34-
35-
$cloak = Cloak::make();
36-
3731
$text = 'Contact: john@example.com, Phone: 555-123-4567';
38-
$cloaked = $cloak->cloak($text);
32+
$cloaked = cloak($text);
3933
// "Contact: {{EMAIL_x7k2m9_1}}, Phone: {{PHONE_x7k2m9_1}}"
4034

41-
$original = $cloak->uncloak($cloaked);
35+
$original = uncloak($cloaked);
4236
// "Contact: john@example.com, Phone: 555-123-4567"
4337
```
4438

@@ -48,16 +42,29 @@ $original = $cloak->uncloak($cloaked);
4842
use DynamikDev\Cloak\Detector;
4943

5044
// Only detect emails
51-
$cloaked = $cloak->cloak($text, [Detector::email()]);
45+
$cloaked = cloak($text, [Detector::email()]);
5246

5347
// Multiple detectors
54-
$cloaked = $cloak->cloak($text, [
48+
$cloaked = cloak($text, [
5549
Detector::email(),
5650
Detector::phone('US'),
5751
Detector::ssn(),
5852
]);
5953
```
6054

55+
### Using the Cloak Class
56+
57+
For more control, use the `Cloak` class directly:
58+
59+
```php
60+
use DynamikDev\Cloak\Cloak;
61+
62+
$cloak = Cloak::make();
63+
64+
$cloaked = $cloak->cloak($text);
65+
$original = $cloak->uncloak($cloaked);
66+
```
67+
6168
### Configuring with Builder Methods
6269

6370
```php
@@ -67,7 +74,6 @@ use DynamikDev\Cloak\Encryptors\OpenSslEncryptor;
6774

6875
$cloak = Cloak::make()
6976
->withDetectors([Detector::email()])
70-
->withTtl(7200)
7177
->encrypt(OpenSslEncryptor::generateKey());
7278

7379
$cloaked = $cloak->cloak('Sensitive: john@example.com');

cloak-php.png

426 KB
Loading

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"autoload": {
77
"psr-4": {
88
"DynamikDev\\Cloak\\": "src/"
9-
}
9+
},
10+
"files": [
11+
"src/helpers.php"
12+
]
1013
},
1114
"autoload-dev": {
1215
"psr-4": {

src/helpers.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DynamikDev\Cloak\Cloak;
6+
use DynamikDev\Cloak\Contracts\StoreInterface;
7+
8+
if (! function_exists('cloak')) {
9+
/**
10+
* Cloak sensitive data in text using the provided detectors.
11+
*
12+
* @param string $text The text to cloak
13+
* @param array<int, \DynamikDev\Cloak\Contracts\DetectorInterface>|null $detectors Optional detectors to use
14+
* @param StoreInterface|null $store Optional store instance
15+
* @return string The cloaked text with placeholders
16+
*/
17+
function cloak(string $text, ?array $detectors = null, ?StoreInterface $store = null): string
18+
{
19+
return Cloak::make($store)->cloak($text, $detectors);
20+
}
21+
}
22+
23+
if (! function_exists('uncloak')) {
24+
/**
25+
* Uncloak text by replacing placeholders with original values.
26+
*
27+
* @param string $text The text containing placeholders
28+
* @param StoreInterface|null $store Optional store instance
29+
* @return string The uncloaked text with original values
30+
*/
31+
function uncloak(string $text, ?StoreInterface $store = null): string
32+
{
33+
return Cloak::make($store)->uncloak($text);
34+
}
35+
}

tests/HelpersTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DynamikDev\Cloak\Detector;
6+
7+
it('cloak helper function works', function () {
8+
$text = 'Email: test@example.com';
9+
$result = cloak($text);
10+
11+
expect($result)->toMatch('/Email: \{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
12+
});
13+
14+
it('uncloak helper function works', function () {
15+
$text = 'Email: test@example.com';
16+
$cloaked = cloak($text);
17+
$uncloaked = uncloak($cloaked);
18+
19+
expect($uncloaked)->toBe($text);
20+
});
21+
22+
it('cloak helper accepts custom detectors', function () {
23+
$text = 'Email: test@example.com SSN: 123-45-6789';
24+
$result = cloak($text, [Detector::email()]);
25+
26+
expect($result)->toMatch('/\{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
27+
expect($result)->toContain('123-45-6789');
28+
});
29+
30+
it('helpers use default ArrayStore', function () {
31+
$text = 'test@example.com';
32+
$cloaked = cloak($text);
33+
34+
expect($cloaked)->toMatch('/\{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
35+
expect(uncloak($cloaked))->toBe($text);
36+
});

0 commit comments

Comments
 (0)