forked from Smile-SA/gdpr-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonData.php
59 lines (48 loc) · 1.54 KB
/
JsonData.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Proxy;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
use Smile\GdprDump\Util\ArrayHelper;
class JsonData implements ConverterInterface
{
/**
* @var ConverterInterface[]
*/
private $converters;
/**
* @param array $parameters
* @throws ValidationException
*/
public function __construct(array $parameters)
{
$input = (new ParameterProcessor())
->addParameter('converters', Parameter::TYPE_ARRAY, true)
->process($parameters);
$this->converters = $input->get('converters');
}
/**
* @inheritdoc
*/
public function convert($value, array $context = [])
{
$decoded = json_decode((string) $value, true);
if (!is_array($decoded)) {
return $value;
}
foreach ($this->converters as $path => $converter) {
// Get the value
$nestedValue = ArrayHelper::getPath($decoded, $path);
if ($nestedValue === null) {
continue;
}
// Format the value
$nestedValue = $converter->convert($nestedValue, $context);
// Replace the original value in the JSON by the converted value
ArrayHelper::setPath($decoded, $path, $nestedValue);
}
return json_encode($decoded);
}
}