diff --git a/.gitignore b/.gitignore index 952755b..b7c918e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .idea /.phpunit.result.cache /tools +/nbproject/* diff --git a/README.md b/README.md index fe95049..be7bd1b 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,5 @@ # Trash -[![Build Status](https://img.shields.io/github/actions/workflow/status/UseMuffin/Trash/ci.yml?style=flat-square -)](https://github.com/UseMuffin/Trash/actions?query=workflow%3ACI+branch%3Amaster) -[![Coverage](https://img.shields.io/codecov/c/github/UseMuffin/Trash/master.svg?style=flat-square)](https://codecov.io/github/UseMuffin/Trash) -[![Total Downloads](https://img.shields.io/packagist/dt/muffin/trash.svg?style=flat-square)](https://packagist.org/packages/muffin/trash) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE) Adds "soft"-delete support to CakePHP tables. @@ -13,7 +9,7 @@ Adds "soft"-delete support to CakePHP tables. Using [Composer][composer]: ``` -composer require muffin/trash +composer require vandalorumrex/trash ``` You then need to load the plugin. You can use the shell command: @@ -115,13 +111,12 @@ To ensure your PRs are considered for upstream, you MUST follow the CakePHP codi ## Bugs & Feedback -http://github.com/usemuffin/trash/issues +http://github.com/vandalorumrex/trash/issues ## License -Copyright (c) 2015-present, [Use Muffin][muffin] and licensed under [The MIT License][mit]. +This library is license under the MIT License (MIT). Please see License File for more information. -[cakephp]:http://cakephp.org -[composer]:http://getcomposer.org -[mit]:http://www.opensource.org/licenses/mit-license.php -[muffin]:http://usemuffin.com +## Credits + +This library was forked from [UseMuffin Trash's](https://github.com/UseMuffin) on Github https://github.com/UseMuffin/Trash. diff --git a/composer.json b/composer.json index f728d07..447e16d 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,14 @@ { - "name": "muffin/trash", + "name": "vandalorumrex/trash", "description": "Adds soft delete support to CakePHP ORM tables.", "type": "cakephp-plugin", "keywords": [ "cakephp", - "muffin", + "vandalorumrex", "trash", "orm" ], - "homepage": "https://github.com/usemuffin/trash", + "homepage": "https://github.com/vandalorumrex/trash", "license": "MIT", "authors": [ { @@ -21,14 +21,19 @@ "homepage": "https://github.com/ADmad", "role": "Author" }, + { + "name": "VandalorumRex", + "homepage": "https://github.com/VandalorumRex", + "role": "Fixer" + }, { "name": "Others", "homepage": "https://github.com/usemuffin/trash/graphs/contributors" } ], "support": { - "issues": "https://github.com/usemuffin/trash/issues", - "source": "https://github.com/usemuffin/trash" + "issues": "https://github.com/vandalorumrex/trash/issues", + "source": "https://github.com/vandalorumrex/trash" }, "require": { "php": ">=8.1", diff --git a/src/Model/Behavior/TrashBehavior.php b/src/Model/Behavior/TrashBehavior.php index fae2b4f..cd4b1f4 100644 --- a/src/Model/Behavior/TrashBehavior.php +++ b/src/Model/Behavior/TrashBehavior.php @@ -8,6 +8,7 @@ use Cake\Core\Exception\CakeException; use Cake\Database\Expression\FieldInterface; use Cake\Database\Expression\IdentifierExpression; +use Cake\Database\Expression\QueryExpression; use Cake\Database\Query\SelectQuery; use Cake\Datasource\EntityInterface; use Cake\Event\EventInterface; @@ -15,6 +16,7 @@ use Cake\ORM\Association; use Cake\ORM\Behavior; use Cake\ORM\Table; +use Closure; use InvalidArgumentException; use function Cake\Core\pluginSplit; @@ -23,7 +25,7 @@ */ class TrashBehavior extends Behavior { - public const AFTER_DELETE_EVENT_OPTION = 'trash'; + public const string AFTER_DELETE_EVENT_OPTION = 'trash'; /** * Default configuration. @@ -137,7 +139,7 @@ public function beforeDelete(EventInterface $event, EntityInterface $entity, Arr * Trash given entity. * * @param \Cake\Datasource\EntityInterface $entity EntityInterface. - * @param array $options Trash operation options. + * @param array $options Trash operation options. * @return bool * @throws \Cake\Core\Exception\CakeException if no primary key is set on entity. */ @@ -160,7 +162,11 @@ public function trash(EntityInterface $entity, array $options = []): bool } } - $entity->set($this->getTrashField(false), new DateTime()); + if (method_exists($entity, 'patch')) { + $entity = $entity->patch([$this->getTrashField(false) => new DateTime()]); + } else { + $entity->set($this->getTrashField(false), new DateTime()); + } return (bool)$this->_table->save($entity, $options); } @@ -202,8 +208,7 @@ protected function shouldAddTrashCondition(SelectQuery $query): bool } if ( - $expression instanceof IdentifierExpression - && in_array($expression->getIdentifier(), $fieldIdentifiers, true) + $expression instanceof IdentifierExpression && in_array($expression->getIdentifier(), $fieldIdentifiers, true) ) { $addCondition = false; @@ -211,8 +216,7 @@ protected function shouldAddTrashCondition(SelectQuery $query): bool } if ( - $expression instanceof FieldInterface - && in_array($expression->getField(), $fieldIdentifiers, true) + $expression instanceof FieldInterface && in_array($expression->getField(), $fieldIdentifiers, true) ) { $addCondition = false; } @@ -231,8 +235,8 @@ protected function shouldAddTrashCondition(SelectQuery $query): bool public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery { return $query - ->applyOptions(['skipAddTrashCondition' => true]) - ->andWhere([$this->getTrashField() . ' IS NOT' => null]); + ->applyOptions(['skipAddTrashCondition' => true]) + ->andWhere([$this->getTrashField() . ' IS NOT' => null]); } /** @@ -250,15 +254,14 @@ public function findWithTrashed(SelectQuery $query, array $options = []): Select /** * Marks all rows matching `$conditions` as `trashed`. * - * @param mixed $conditions Conditions to be used, accepts anything Query::where() - * can take. + * @param \Cake\Database\Expression\QueryExpression|\Closure|array|string|null $conditions Conditions to be used, accepts anything Query::where() can take. * @return int Count Returns the affected rows. */ - public function trashAll(mixed $conditions): int + public function trashAll(array|QueryExpression|Closure|string|null $conditions): int { return $this->_table->updateAll( [$this->getTrashField(false) => new DateTime()], - $conditions + $conditions, ); } @@ -276,7 +279,7 @@ public function emptyTrash(): int * Restores all (or given) trashed row(s). * * @param \Cake\Datasource\EntityInterface|null $entity to restore. - * @param array $options Restore operation options (only applies when restoring a specific entity). + * @param array $options Restore operation options (only applies when restoring a specific entity). * @return \Cake\Datasource\EntityInterface|int|false */ public function restoreTrash(?EntityInterface $entity = null, array $options = []): false|int|EntityInterface @@ -287,7 +290,11 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [ if ($entity->isDirty()) { throw new CakeException('Can not restore from a dirty entity.'); } - $entity->set($data, ['guard' => false]); + if (method_exists($entity, 'patch')) { + $entity = $entity->patch($data, ['guard' => false]); + } else { + $entity->set($data, ['guard' => false]); + } return $this->_table->save($entity, $options); } @@ -299,12 +306,12 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [ * Restore an item from trashed status and all its related data * * @param \Cake\Datasource\EntityInterface|null $entity Entity instance - * @param array $options Restore operation options (only applies when restoring a specific entity). + * @param array $options Restore operation options (only applies when restoring a specific entity). * @return \Cake\Datasource\EntityInterface|int|bool */ public function cascadingRestoreTrash( ?EntityInterface $entity = null, - array $options = [] + array $options = [], ): bool|int|EntityInterface { $result = $this->restoreTrash($entity, $options); @@ -346,6 +353,7 @@ public function cascadingRestoreTrash( */ public function getTrashField(bool $aliased = true): string { + /** @var string|null $field */ $field = $this->getConfig('field'); if ($field === null) { @@ -356,7 +364,7 @@ public function getTrashField(bool $aliased = true): string break; } } - + /** @var string|null $field */ $field ??= Configure::read('Muffin/Trash.field'); if ($field === null) { @@ -383,11 +391,7 @@ public function getTrashField(bool $aliased = true): string protected function _isRecursable(Association $association, Table $table): bool { return ( - $association->getTarget()->hasBehavior('Trash') - || $association->getTarget()->hasBehavior(static::class) - ) - && $association->isOwningSide($table) - && $association->getDependent() - && $association->getCascadeCallbacks(); + $association->getTarget()->hasBehavior('Trash') || $association->getTarget()->hasBehavior(static::class) + ) && $association->isOwningSide($table) && $association->getDependent() && $association->getCascadeCallbacks(); } }