99use Cake \Database \Expression \BetweenExpression ;
1010use Cake \Database \Expression \ComparisonExpression ;
1111use Cake \Database \Expression \IdentifierExpression ;
12- use Cake \Database \Expression \QueryExpression ;
13- use Cake \Database \Expression \UnaryExpression ;
1412use Cake \Database \Query \SelectQuery ;
1513use Cake \Datasource \EntityInterface ;
1614use Cake \Event \EventInterface ;
1715use Cake \I18n \DateTime ;
1816use Cake \ORM \Association ;
1917use Cake \ORM \Behavior ;
2018use Cake \ORM \Table ;
21- use Closure ;
2219use InvalidArgumentException ;
2320use function Cake \Core \pluginSplit ;
2421
@@ -56,7 +53,7 @@ class TrashBehavior extends Behavior
5653 */
5754 public function initialize (array $ config ): void
5855 {
59- if (! empty ($ config ['events ' ])) {
56+ if (isset ($ config ['events ' ]) && $ config [ ' events ' ] !== [] ) {
6057 $ this ->setConfig ('events ' , $ config ['events ' ], false );
6158 }
6259 }
@@ -70,10 +67,12 @@ public function initialize(array $config): void
7067 public function implementedEvents (): array
7168 {
7269 $ events = [];
73- if ($ this ->getConfig ('events ' ) === false ) {
70+ $ config = $ this ->getConfig ('events ' );
71+ if ($ config === false ) {
7472 return $ events ;
7573 }
76- foreach ((array )$ this ->getConfig ('events ' ) as $ eventKey => $ event ) {
74+
75+ foreach ((array )$ config as $ eventKey => $ event ) {
7776 if (is_numeric ($ eventKey )) {
7877 $ eventKey = $ event ;
7978 $ event = null ;
@@ -84,13 +83,16 @@ public function implementedEvents(): array
8483 if (!is_array ($ event )) {
8584 throw new InvalidArgumentException ('Event should be string or array ' );
8685 }
87- $ priority = $ this -> getConfig ( ' priority ' );
86+
8887 if (!array_key_exists ('callable ' , $ event ) || $ event ['callable ' ] === null ) {
8988 [, $ event ['callable ' ]] = pluginSplit ($ eventKey );
9089 }
90+
91+ $ priority = $ this ->getConfig ('priority ' );
9192 if ($ priority && !array_key_exists ('priority ' , $ event )) {
9293 $ event ['priority ' ] = $ priority ;
9394 }
95+
9496 $ events [$ eventKey ] = $ event ;
9597 }
9698
@@ -108,7 +110,7 @@ public function implementedEvents(): array
108110 */
109111 public function beforeDelete (EventInterface $ event , EntityInterface $ entity , ArrayObject $ options ): void
110112 {
111- if ($ options-> offsetExists ( 'purge ' ) && $ options ['purge ' ] === true ) {
113+ if (isset ( $ options[ 'purge ' ] ) && $ options ['purge ' ] === true ) {
112114 return ;
113115 }
114116
@@ -159,14 +161,9 @@ public function trash(EntityInterface $entity, array $options = []): bool
159161 }
160162 }
161163
162- $ data = [$ this ->getTrashField (false ) => new DateTime ()];
163- $ entity ->set ($ data , ['guard ' => false ]);
164-
165- if ($ this ->_table ->save ($ entity , $ options )) {
166- return true ;
167- }
164+ $ entity ->set ($ this ->getTrashField (false ), new DateTime ());
168165
169- return false ;
166+ return ( bool ) $ this -> _table -> save ( $ entity , $ options ) ;
170167 }
171168
172169 /**
@@ -238,7 +235,7 @@ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
238235 {
239236 return $ query
240237 ->applyOptions (['skipAddTrashCondition ' => true ])
241- ->andWhere ($ query -> newExpr ()-> isNotNull ( $ this ->getTrashField ()) );
238+ ->andWhere ([ $ this ->getTrashField () . ' IS NOT ' => null ] );
242239 }
243240
244241 /**
@@ -250,9 +247,7 @@ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
250247 */
251248 public function findWithTrashed (SelectQuery $ query , array $ options = []): SelectQuery
252249 {
253- return $ query ->applyOptions ([
254- 'skipAddTrashCondition ' => true ,
255- ]);
250+ return $ query ->applyOptions (['skipAddTrashCondition ' => true ]);
256251 }
257252
258253 /**
@@ -277,7 +272,7 @@ public function trashAll(mixed $conditions): int
277272 */
278273 public function emptyTrash (): int
279274 {
280- return $ this ->_table ->deleteAll ($ this ->_getUnaryExpression () );
275+ return $ this ->_table ->deleteAll ([ $ this ->getTrashField ( false ) . ' IS NOT ' => null ] );
281276 }
282277
283278 /**
@@ -300,7 +295,7 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
300295 return $ this ->_table ->save ($ entity , $ options );
301296 }
302297
303- return $ this ->_table ->updateAll ($ data , $ this ->_getUnaryExpression () );
298+ return $ this ->_table ->updateAll ($ data , [ $ this ->getTrashField ( false ) . ' IS NOT ' => null ] );
304299 }
305300
306301 /**
@@ -346,21 +341,6 @@ public function cascadingRestoreTrash(
346341 return $ result ;
347342 }
348343
349- /**
350- * Returns a unary expression for bulk record manipulation.
351- *
352- * @return \Closure
353- */
354- protected function _getUnaryExpression (): Closure
355- {
356- return fn (QueryExpression $ queryExpression ): QueryExpression => $ queryExpression
357- ->add (new UnaryExpression (
358- 'IS NOT NULL ' ,
359- $ this ->getTrashField (false ),
360- UnaryExpression::POSTFIX
361- ));
362- }
363-
364344 /**
365345 * Returns the table's field used to mark a `trashed` row.
366346 *
@@ -371,7 +351,7 @@ public function getTrashField(bool $aliased = true): string
371351 {
372352 $ field = $ this ->getConfig ('field ' );
373353
374- if (empty ( $ field) ) {
354+ if ($ field === null ) {
375355 $ columns = $ this ->_table ->getSchema ()->columns ();
376356 foreach (['deleted ' , 'trashed ' ] as $ name ) {
377357 if (in_array ($ name , $ columns , true )) {
@@ -380,12 +360,9 @@ public function getTrashField(bool $aliased = true): string
380360 }
381361 }
382362
383- /** @psalm-suppress RedundantCondition */
384- if (empty ($ field )) {
385- $ field = Configure::read ('Muffin/Trash.field ' );
386- }
363+ $ field ??= Configure::read ('Muffin/Trash.field ' );
387364
388- if (empty ( $ field) ) {
365+ if ($ field === null ) {
389366 throw new CakeException ('TrashBehavior: "field" config needs to be provided. ' );
390367 }
391368
0 commit comments