44
55namespace MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \Core ;
66
7- use MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \AbstractAnonymizer ;
7+ use MakinaCorpus \DbToolsBundle \Anonymization \Anonymizer \AbstractSingleColumnAnonymizer ;
88use MakinaCorpus \DbToolsBundle \Attribute \AsAnonymizer ;
9+ use MakinaCorpus \QueryBuilder \Expression ;
910use MakinaCorpus \QueryBuilder \Vendor ;
1011use MakinaCorpus \QueryBuilder \Query \Update ;
1112
2122 can work with 'datetime' or 'date' formats.
2223 TXT
2324)]
24- class DateAnonymizer extends AbstractAnonymizer
25+ class DateAnonymizer extends AbstractSingleColumnAnonymizer
2526{
2627 #[\Override]
2728 protected function validateOptions (): void
@@ -51,29 +52,25 @@ protected function validateOptions(): void
5152 }
5253
5354 #[\Override]
54- public function anonymize (Update $ update ): void
55+ public function createAnonymizeExpression (Update $ update ): Expression
5556 {
5657 $ format = $ this ->options ->get ('format ' , 'datetime ' );
5758
5859 $ min = $ this ->options ->getDate ('min ' );
5960 $ max = $ this ->options ->getDate ('max ' );
6061
6162 if ($ min && $ max ) {
62- $ this ->anonymizeWithDateRange ($ update , $ format , $ min , $ max );
63-
64- return ;
63+ return $ this ->anonymizeWithDateRange ($ update , $ format , $ min , $ max );
6564 }
6665
6766 if ($ delta = $ this ->options ->getInterval ('delta ' )) {
68- $ this ->anonmizeWithDelta ($ update , $ format , $ delta );
69-
70- return ;
67+ return $ this ->anonmizeWithDelta ($ update , $ format , $ delta );
7168 }
7269
7370 throw new \InvalidArgumentException ("Providing either the 'delta' option, or both 'min' and 'max' options is required. " );
7471 }
7572
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
7774 {
7875 $ diff = $ max ->diff ($ min , true );
7976
@@ -96,10 +93,10 @@ private function anonymizeWithDateRange(Update $update, string $format, \DateTim
9693 $ delta /= 2 ;
9794 $ middleDate = $ min ->add (\DateInterval::createFromDateString (\sprintf ("%d %s " , $ delta , $ unit )));
9895
99- $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ middleDate , $ delta , $ unit );
96+ return $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ middleDate , $ delta , $ unit );
10097 }
10198
102- private function anonmizeWithDelta (Update $ update , string $ format , \DateInterval $ delta ): void
99+ private function anonmizeWithDelta (Update $ update , string $ format , \DateInterval $ delta ): Expression
103100 {
104101 // @todo I wish for a better alternative...
105102 // query-builder can deal with \DateInterval by- itself, but we are
@@ -137,43 +134,37 @@ private function anonmizeWithDelta(Update $update, string $format, \DateInterval
137134 $ expr = $ update ->expression ();
138135 $ columnExpr = $ expr ->column ($ this ->columnName , $ this ->tableName );
139136
140- $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ columnExpr , $ delta , $ unit );
137+ return $ this ->anonymizeWithDeltaAndReferenceDate ($ update , $ format , $ columnExpr , $ delta , $ unit );
141138 }
142139
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
144141 {
145142 $ expr = $ update ->expression ();
146143
147144 $ randomDeltaExpr = $ this ->getRandomIntExpression ($ delta , 0 - $ delta );
148145
149146 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+ )
164158 )
165159 );
166160 } 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 ' ,
177168 )
178169 );
179170 }
0 commit comments