4
4
5
5
namespace MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \Core ;
6
6
7
- use MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \AbstractAnonymizer ;
7
+ use MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \AbstractSingleColumnAnonymizer ;
8
8
use MakinaCorpus \DbToolsBundle \Attribute \AsAnonymizer ;
9
+ use MakinaCorpus \QueryBuilder \Expression ;
9
10
use MakinaCorpus \QueryBuilder \Vendor ;
10
11
use MakinaCorpus \QueryBuilder \Query \Update ;
11
12
21
22
can work with 'datetime' or 'date' formats.
22
23
TXT
23
24
)]
24
- class DateAnonymizer extends AbstractAnonymizer
25
+ class DateAnonymizer extends AbstractSingleColumnAnonymizer
25
26
{
26
27
#[\Override]
27
28
protected function validateOptions (): void
@@ -51,29 +52,25 @@ protected function validateOptions(): void
51
52
}
52
53
53
54
#[\Override]
54
- public function anonymize (Update $ update ): void
55
+ public function createAnonymizeExpression (Update $ update ): Expression
55
56
{
56
57
$ format = $ this ->options ->get ('format ' , 'datetime ' );
57
58
58
59
$ min = $ this ->options ->getDate ('min ' );
59
60
$ max = $ this ->options ->getDate ('max ' );
60
61
61
62
if ($ min && $ max ) {
62
- $ this ->anonymizeWithDateRange ($ update , $ format , $ min , $ max );
63
-
64
- return ;
63
+ return $ this ->anonymizeWithDateRange ($ update , $ format , $ min , $ max );
65
64
}
66
65
67
66
if ($ delta = $ this ->options ->getInterval ('delta ' )) {
68
- $ this ->anonmizeWithDelta ($ update , $ format , $ delta );
69
-
70
- return ;
67
+ return $ this ->anonmizeWithDelta ($ update , $ format , $ delta );
71
68
}
72
69
73
70
throw new \InvalidArgumentException ("Providing either the 'delta' option, or both 'min' and 'max' options is required. " );
74
71
}
75
72
76
- private function anonymizeWithDateRange (Update $ update , string $ format , \DateTimeImmutable $ min , \DateTimeImmutable $ max ): void
73
+ private function anonymizeWithDateRange (Update $ update , string $ format , \DateTimeImmutable $ min , \DateTimeImmutable $ max ): Expression
77
74
{
78
75
$ diff = $ max ->diff ($ min , true );
79
76
@@ -96,10 +93,10 @@ private function anonymizeWithDateRange(Update $update, string $format, \DateTim
96
93
$ delta /= 2 ;
97
94
$ middleDate = $ min ->add (\DateInterval::createFromDateString (\sprintf ("%d %s " , $ delta , $ unit )));
98
95
99
- $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ middleDate , $ delta , $ unit );
96
+ return $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ middleDate , $ delta , $ unit );
100
97
}
101
98
102
- private function anonmizeWithDelta (Update $ update , string $ format , \DateInterval $ delta ): void
99
+ private function anonmizeWithDelta (Update $ update , string $ format , \DateInterval $ delta ): Expression
103
100
{
104
101
// @todo I wish for a better alternative...
105
102
// query-builder can deal with \DateInterval by- itself, but we are
@@ -137,43 +134,37 @@ private function anonmizeWithDelta(Update $update, string $format, \DateInterval
137
134
$ expr = $ update ->expression ();
138
135
$ columnExpr = $ expr ->column ($ this ->columnName , $ this ->tableName );
139
136
140
- $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ columnExpr , $ delta , $ unit );
137
+ return $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ columnExpr , $ delta , $ unit );
141
138
}
142
139
143
- private function anonymizeWithDeltaAndReferenceDate (Update $ update , string $ format , mixed $ referenceDate , int $ delta , string $ unit ): void
140
+ private function anonymizeWithDeltaAndReferenceDate (Update $ update , string $ format , mixed $ referenceDate , int $ delta , string $ unit ): Expression
144
141
{
145
142
$ expr = $ update ->expression ();
146
143
147
144
$ randomDeltaExpr = $ this ->getRandomIntExpression ($ delta , 0 - $ delta );
148
145
149
146
if ($ this ->databaseSession ->vendorIs (Vendor::SQLITE )) {
150
- $ update ->set (
151
- $ this ->columnName ,
152
- $ this ->getSetIfNotNullExpression (
153
- $ expr ->dateAdd (
154
- $ referenceDate ,
155
- $ expr ->intervalUnit (
156
- // This additional cast is necessary for SQLite only because it
157
- // will mix up int addition and string concatenation, causing
158
- // the interval string to be malformed. For all other vendors,
159
- // it's a no-op.
160
- $ expr ->cast ($ randomDeltaExpr , 'varchar ' ),
161
- $ unit
162
- ),
163
- ),
147
+ return $ this ->getSetIfNotNullExpression (
148
+ $ expr ->dateAdd (
149
+ $ referenceDate ,
150
+ $ expr ->intervalUnit (
151
+ // This additional cast is necessary for SQLite only because it
152
+ // will mix up int addition and string concatenation, causing
153
+ // the interval string to be malformed. For all other vendors,
154
+ // it's a no-op.
155
+ $ expr ->cast ($ randomDeltaExpr , 'varchar ' ),
156
+ $ unit
157
+ )
164
158
)
165
159
);
166
160
} else {
167
- $ update ->set (
168
- $ this ->columnName ,
169
- $ this ->getSetIfNotNullExpression (
170
- $ expr ->cast (
171
- $ expr ->dateAdd (
172
- $ referenceDate ,
173
- $ expr ->intervalUnit ($ randomDeltaExpr , $ unit ),
174
- ),
175
- 'date ' === $ format ? 'date ' : 'timestamp ' ,
176
- )
161
+ return $ this ->getSetIfNotNullExpression (
162
+ $ expr ->cast (
163
+ $ expr ->dateAdd (
164
+ $ referenceDate ,
165
+ $ expr ->intervalUnit ($ randomDeltaExpr , $ unit ),
166
+ ),
167
+ 'date ' === $ format ? 'date ' : 'timestamp ' ,
177
168
)
178
169
);
179
170
}
0 commit comments