Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here is an example configuration used in the unit tests:
```php
<?php

use ClntDev\Scrubber\Handlers\FakerHandler;
use ClntDev\Scrubber\DataHandlers\FakerHandler;

return [
'users' => [
Expand Down
13 changes: 4 additions & 9 deletions src/Contracts/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

interface DataHandler
{
public function __construct(
string $table,
string $field,
mixed $input,
?DatabaseKey $primaryKey = null,
?string $type = null,
string $seed = 'scrubber',
);
public static function makeFromConfig(
array $config
): self;

public function handle(); //phpcs:ignore
public function getValue(): mixed;
}
32 changes: 32 additions & 0 deletions src/Contracts/DatabaseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ClntDev\Scrubber\Contracts;

interface DatabaseHandler
{
/** @return mixed[] */
public function fetch(
string $table,
DatabaseKey $primaryKey
): array;

public function update(
string $table,
string $field,
mixed $value,
string|int|array $primaryKeyValue,
DatabaseKey $primaryKey
): bool;

public function delete(
string $table,
string $field,
string $value,
string|int|array $primaryKeyValue,
DatabaseKey $primaryKey
): bool;

public function truncate(
string $table
): bool;
}
17 changes: 0 additions & 17 deletions src/Contracts/DatabaseUpdate.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Contracts/DetectsApplicability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ClntDev\Scrubber\Contracts;

interface DetectsApplicability
{
public static function detect(mixed $value): bool;
}
29 changes: 29 additions & 0 deletions src/Contracts/ScrubHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ClntDev\Scrubber\Contracts;

interface ScrubHandler
{
public function __construct(
string $table,
string $field,
DatabaseKey $primaryKey,
DataHandler $dataHandler,
?string $type = 'pid'
);

public function scrub(
mixed $primaryKeyValues,
DatabaseHandler $database
): void;

public function getTable(): string;

public function getField(): string;

public function getPrimaryKey(): DatabaseKey;

public function getDataHandler(): DataHandler;

public function getType(): ?string;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class ArrayHandler extends Handler
{
public function handle(): string|bool
public function getValue(): string|bool
{
if (is_array($this->input) === false) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class CallableHandler extends Handler
{
public function handle(): ?string
public function getValue(): ?string
{
if (is_callable($this->input) === false) {
throw new RuntimeException('CallableHandler input is not callable');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class FakerHandler extends Handler
{
public function handle(): ?string
public function getValue(): ?string
{
if (str_contains($this->input, 'faker.') === false) {
throw new RuntimeException('FakerHandler invalid faker input format given');
Expand Down
48 changes: 48 additions & 0 deletions src/DataHandlers/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ClntDev\Scrubber\DataHandlers;

use ClntDev\Scrubber\Contracts\DatabaseKey;
use ClntDev\Scrubber\Contracts\DataHandler;
use ClntDev\Scrubber\Contracts\DetectsApplicability;
use ClntDev\Scrubber\DatabaseKey as ScrubberDatabaseKey;
use Faker\Factory as Faker;
use Faker\Generator;

abstract class Handler implements DataHandler, DetectsApplicability
{
public mixed $input;

public ?string $type;

protected Generator $faker;

public static function makeFromConfig(array $config): DataHandler
{
return new static(
$config['value'] ?? null,
$config['type'] ?? null,
$config['seed'] ?? 'scrubber'
);
}

public function __construct(
mixed $input,
?string $type,
string $seed,
) {
$this->input = $input;
$this->type = $type;
$this->faker = Faker::create();
$this->faker->seed($seed);
}

public function getType(): ?string
{
return $this->type;
}

abstract public function getValue(): mixed;

abstract public static function detect(mixed $value): bool;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class IntegerHandler extends Handler
{
public function handle(): ?int
public function getValue(): ?int
{
if (is_numeric($this->input) === false) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class ObjectHandler extends Handler
{
public function handle(): ?string
public function getValue(): ?string
{
if (is_object($this->input) === false) {
throw new RuntimeException('CallableHandler input is not an object');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ClntDev\Scrubber\Handlers;
namespace ClntDev\Scrubber\DataHandlers;

use RuntimeException;

class StringHandler extends Handler
{
public function handle(): ?string
public function getValue(): ?string
{
if (is_string($this->input) === false && is_int($this->input) === false) {
throw new RuntimeException(
Expand Down
23 changes: 23 additions & 0 deletions src/Exceptions/InvalidHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ClntDev\Scrubber\Exceptions;

use RuntimeException;
use Throwable;

class InvalidHandler extends RuntimeException
{
public mixed $value = '';

public function __construct(
mixed $value,
string $message = '',
int $code = 0,
?Throwable $previous = null
) {
$this->value = $value;
$message = $message !== '' ? $message : 'Invalid handler.';

parent::__construct($message, $code, $previous);
}
}
26 changes: 0 additions & 26 deletions src/Exceptions/ValueNotFound.php

This file was deleted.

63 changes: 0 additions & 63 deletions src/Handlers/Handler.php

This file was deleted.

Loading