-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7727096
commit 4bde2ad
Showing
15 changed files
with
219 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Cursor\CursorInterface; | ||
use Mongolid\Util\QueryBuilder; | ||
|
||
trait SoftDeleteTrait | ||
{ | ||
public bool $isSoftDeleteEnabled = true; | ||
|
||
public function isTrashed(): bool | ||
{ | ||
return !is_null($this->{QueryBuilder::getDeletedAtColumn($this)}); | ||
} | ||
|
||
public function restore(): bool | ||
{ | ||
$collumn = QueryBuilder::getDeletedAtColumn($this); | ||
|
||
if (!$this->isTrashed()) { | ||
return false; | ||
} | ||
|
||
unset($this->{$collumn}); | ||
|
||
return $this->update(); | ||
} | ||
|
||
public function forceDelete(): bool | ||
{ | ||
return $this->execute('delete'); | ||
} | ||
|
||
public function executeSoftDelete(): bool | ||
{ | ||
$deletedAtColumn = QueryBuilder::getDeletedAtColumn($this); | ||
$this->$deletedAtColumn = new UTCDateTime(new DateTime('now')); | ||
|
||
return $this->update(); | ||
} | ||
|
||
public static function withTrashed( | ||
mixed $query = [], | ||
array $projection = [], | ||
bool $useCache = false | ||
): CursorInterface { | ||
return self::performSearch($query, $projection, $useCache); | ||
} | ||
|
||
private static function whereWithMapper(mixed $query, array $projection, bool $useCache) | ||
{ | ||
$mapper = self::getDataMapperInstance(); | ||
|
||
$mapper->withTrashed = true; | ||
|
||
return $mapper->where($query, $projection, $useCache); | ||
} | ||
|
||
private static function whereWithBuilder(mixed $query, array $projection, bool $useCache) | ||
{ | ||
$mapper = self::getBuilderInstance(); | ||
|
||
$mapper->withTrashed = true; | ||
|
||
return $mapper->where( | ||
new static(), | ||
$query, | ||
$projection, | ||
$useCache | ||
); | ||
} | ||
|
||
private static function performSearch(mixed $query, array $projection, bool $useCache) | ||
{ | ||
$isLegacy = method_exists(self::class, 'getDataMapperInstance'); | ||
|
||
return $isLegacy ? | ||
self::whereWithMapper($query, $projection, $useCache) : | ||
self::whereWithBuilder($query, $projection, $useCache); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.