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..536f049 100644 --- a/src/Model/Behavior/TrashBehavior.php +++ b/src/Model/Behavior/TrashBehavior.php @@ -1,4 +1,5 @@ $event) { + foreach ((array) $config as $eventKey => $event) { if (is_numeric($eventKey)) { $eventKey = $event; $event = null; @@ -143,7 +145,7 @@ public function beforeDelete(EventInterface $event, EntityInterface $entity, Arr */ public function trash(EntityInterface $entity, array $options = []): bool { - $primaryKey = (array)$this->_table->getPrimaryKey(); + $primaryKey = (array) $this->_table->getPrimaryKey(); foreach ($primaryKey as $field) { if (!$entity->has($field)) { @@ -160,9 +162,13 @@ 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); + 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]); } /** @@ -287,7 +291,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); } @@ -317,9 +325,9 @@ public function cascadingRestoreTrash( } } else { /** @var list $foreignKey */ - $foreignKey = (array)$association->getForeignKey(); + $foreignKey = (array) $association->getForeignKey(); /** @var list $bindingKey */ - $bindingKey = (array)$association->getBindingKey(); + $bindingKey = (array) $association->getBindingKey(); $conditions = array_combine($foreignKey, $entity->extract($bindingKey)); foreach ($association->find('withTrashed')->where($conditions) as $related) { @@ -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(); } }