generated from wayofdev/laravel-package-tpl
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #576 from wayofdev/feat/soft-deletes
- Loading branch information
Showing
21 changed files
with
918 additions
and
281 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
Large diffs are not rendered by default.
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
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WayOfDev\Cycle\Testing\Constraints; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use Cycle\Database\DatabaseProviderInterface; | ||
use Cycle\Database\Query\SelectQuery; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Throwable; | ||
|
||
use function json_encode; | ||
use function sprintf; | ||
|
||
class NotSoftDeletedInDatabase extends Constraint | ||
{ | ||
protected int $show = 3; | ||
|
||
protected DatabaseInterface $database; | ||
|
||
protected array $data; | ||
|
||
protected string $deletedAtColumn; | ||
|
||
public function __construct(DatabaseProviderInterface $database, array $data, string $deletedAtColumn) | ||
{ | ||
$this->data = $data; | ||
|
||
$this->database = $database->database(); | ||
|
||
$this->deletedAtColumn = $deletedAtColumn; | ||
} | ||
|
||
public function matches(mixed $other): bool | ||
{ | ||
/** @var SelectQuery $tableInterface */ | ||
$tableInterface = $this->database->table($other); | ||
|
||
try { | ||
$count = $tableInterface->where($this->data) | ||
->andWhere($this->deletedAtColumn, '=', null) | ||
->count(); | ||
|
||
return 0 < $count; | ||
} catch (Throwable $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function failureDescription($other): string | ||
{ | ||
return sprintf( | ||
'any existing row in the table [%s] matches the attributes %s.\n', | ||
$other, | ||
$this->toString() | ||
); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WayOfDev\Cycle\Testing\Constraints; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use Cycle\Database\DatabaseProviderInterface; | ||
use Cycle\Database\Query\SelectQuery; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Throwable; | ||
|
||
use function json_encode; | ||
use function sprintf; | ||
|
||
class SoftDeletedInDatabase extends Constraint | ||
{ | ||
protected int $show = 3; | ||
|
||
protected DatabaseInterface $database; | ||
|
||
protected array $data; | ||
|
||
protected string $deletedAtColumn; | ||
|
||
public function __construct(DatabaseProviderInterface $database, array $data, string $deletedAtColumn) | ||
{ | ||
$this->data = $data; | ||
|
||
$this->database = $database->database(); | ||
|
||
$this->deletedAtColumn = $deletedAtColumn; | ||
} | ||
|
||
public function matches(mixed $other): bool | ||
{ | ||
/** @var SelectQuery $tableInterface */ | ||
$tableInterface = $this->database->table($other); | ||
|
||
try { | ||
$count = $tableInterface->where($this->data) | ||
->andWhere($this->deletedAtColumn, '!=', null) | ||
->count(); | ||
|
||
return 0 < $count; | ||
} catch (Throwable $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function failureDescription($other): string | ||
{ | ||
return sprintf( | ||
'a soft deleted row in the table [%s] matches the attributes %s.', | ||
$other, | ||
$this->toString() | ||
); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WayOfDev\App\Entities; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use JsonException; | ||
use JsonSerializable; | ||
use Ramsey\Uuid\Uuid; | ||
use Stringable; | ||
|
||
use function json_decode; | ||
use function json_encode; | ||
|
||
final class Footprint implements JsonSerializable, Stringable | ||
{ | ||
private UserId $id; | ||
|
||
private string $party; | ||
|
||
private string $realm; | ||
|
||
public static function empty(string $authorizedParty = 'guest-party', string $realm = 'guest-realm'): self | ||
{ | ||
return new self(UserId::create(Uuid::NIL), $authorizedParty, $realm); | ||
} | ||
|
||
public static function random(string $authorizedParty = 'random-party', string $realm = 'random-realm'): self | ||
{ | ||
return new self(UserId::create(Uuid::uuid7()->toString()), $authorizedParty, $realm); | ||
} | ||
|
||
public static function fromArray(array $data): self | ||
{ | ||
$userId = UserId::fromString($data['id']); | ||
|
||
return new self($userId, $data['party'], $data['realm']); | ||
} | ||
|
||
/** | ||
* https://cycle-orm.dev/docs/advanced-column-wrappers/2.x/en. | ||
* | ||
* @throws JsonException | ||
*/ | ||
public static function castValue(string $value, DatabaseInterface $db): self | ||
{ | ||
return self::fromArray( | ||
json_decode($value, true, 512, JSON_THROW_ON_ERROR) | ||
); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'id' => $this->id->toString(), | ||
'party' => $this->party, | ||
'realm' => $this->realm, | ||
]; | ||
} | ||
|
||
public function id(): UserId | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function party(): string | ||
{ | ||
return $this->party; | ||
} | ||
|
||
public function realm(): string | ||
{ | ||
return $this->realm; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
/** | ||
* @throws JsonException | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return json_encode($this->toArray(), JSON_THROW_ON_ERROR); | ||
} | ||
|
||
private function __construct(UserId $id, string $party, string $realm) | ||
{ | ||
$this->id = $id; | ||
$this->party = $party; | ||
$this->realm = $realm; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WayOfDev\App\Entities; | ||
|
||
use Cycle\Annotated\Annotation\Relation\Embedded; | ||
|
||
trait HasSignatures | ||
{ | ||
#[Embedded(target: Signature::class, prefix: 'created_')] | ||
private Signature $created; | ||
|
||
#[Embedded(target: Signature::class, prefix: 'updated_')] | ||
private Signature $updated; | ||
|
||
#[Embedded(target: Signature::class, prefix: 'deleted_')] | ||
private ?Signature $deleted; | ||
|
||
public function created(): Signature | ||
{ | ||
return $this->created; | ||
} | ||
|
||
public function updated(): Signature | ||
{ | ||
return $this->updated; | ||
} | ||
|
||
public function deleted(): ?Signature | ||
{ | ||
if (! $this->deleted?->defined()) { | ||
return null; | ||
} | ||
|
||
return $this->deleted; | ||
} | ||
|
||
public function softDelete(Signature $deleted): void | ||
{ | ||
$this->deleted = $deleted; | ||
} | ||
} |
Oops, something went wrong.