1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
7
|
|
|
* |
8
|
|
|
* PHP Version 7.2 |
9
|
|
|
* |
10
|
|
|
* @author Emmanuel Dyan |
11
|
|
|
* |
12
|
|
|
* @copyright 2020 Emmanuel Dyan |
13
|
|
|
* |
14
|
|
|
* @package edyan/neuralyzer |
15
|
|
|
* |
16
|
|
|
* @license GNU General Public License v2.0 |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/edyan/neuralyzer |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Edyan\Neuralyzer\Utils; |
22
|
|
|
|
23
|
|
|
use Psr\Container\ContainerInterface; |
24
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Expression |
28
|
|
|
* |
29
|
|
|
* @package edyan/neuralyzer |
30
|
|
|
*/ |
31
|
|
|
class Expression |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Container injected by autowiring |
35
|
|
|
* |
36
|
|
|
* @var ContainerInterface |
37
|
|
|
*/ |
38
|
|
|
private $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List of "things" to inject in expressions evaluation |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
50 |
|
*/ |
45
|
|
|
private $services = []; |
46
|
50 |
|
|
47
|
50 |
|
/** |
48
|
50 |
|
* Used for autowiring |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ContainerInterface $container) |
51
|
|
|
{ |
52
|
|
|
$this->container = $container; |
53
|
|
|
$this->configure(); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
/** |
57
|
|
|
* Return all registered services |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getServices(): array |
62
|
|
|
{ |
63
|
|
|
return $this->services; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
6 |
|
* Evaluate an expression that would be in the most general case |
68
|
|
|
* an action coming from an Anonymization config |
69
|
6 |
|
* |
70
|
|
|
* @return mixed |
71
|
6 |
|
*/ |
72
|
|
|
public function evaluateExpression(string $expression) |
73
|
|
|
{ |
74
|
|
|
$expressionLanguage = new ExpressionLanguage(); |
75
|
|
|
|
76
|
|
|
return $expressionLanguage->evaluate($expression, $this->services); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Evaluate a list of expression |
81
|
|
|
* |
82
|
25 |
|
* @param array $expressions |
83
|
|
|
* |
84
|
25 |
|
* @return array |
85
|
25 |
|
*/ |
86
|
5 |
|
public function evaluateExpressions(array $expressions): array |
87
|
|
|
{ |
88
|
|
|
$res = []; |
89
|
24 |
|
foreach ($expressions as $expression) { |
90
|
|
|
$res[] = $this->evaluateExpression($expression); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $res; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
50 |
|
* Configure that service by registering all services in an array |
98
|
|
|
*/ |
99
|
50 |
|
private function configure(): void |
100
|
50 |
|
{ |
101
|
50 |
|
$services = array_keys($this->container->findTaggedServiceIds('app.service')); |
|
|
|
|
102
|
50 |
|
foreach ($services as $service) { |
103
|
|
|
$service = $this->container->get($service); |
104
|
50 |
|
$this->services[$service->getName()] = $service; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|