-
Notifications
You must be signed in to change notification settings - Fork 48
/
Hash.php
44 lines (35 loc) · 1.17 KB
/
Hash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Transformer;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
class Hash implements ConverterInterface
{
private string $algorithm;
/**
* @inheritdoc
*/
public function setParameters(array $parameters): void
{
$input = (new ParameterProcessor())
->addParameter('algorithm', Parameter::TYPE_STRING, true, 'sha1')
->process($parameters);
$this->algorithm = $input->get('algorithm');
$allowed = hash_algos();
if (!in_array($this->algorithm, $allowed, true)) {
throw new ValidationException(
sprintf('Invalid algorithm "%s". Allowed values: %s.', $this->algorithm, implode(', ', $allowed))
);
}
}
/**
* @inheritdoc
*/
public function convert(mixed $value, array $context = []): string
{
$value = (string) $value;
return $value !== '' ? hash($this->algorithm, $value) : $value;
}
}