generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\ValueObjects\Values; | ||
|
||
use EriBloo\CacheObjects\Contracts\CacheValueTransformer; | ||
use Closure; | ||
|
||
/** | ||
* @template TValue | ||
* | ||
* @implements CacheValueTransformer<TValue> | ||
*/ | ||
final readonly class GuardTransformer implements CacheValueTransformer | ||
{ | ||
/** | ||
* @param CacheValueTransformer<TValue> $transformer | ||
* @param (Closure(TValue): void)|null $onSaveGuard | ||
* @param (Closure(TValue): void)|null $onLoadGuard | ||
*/ | ||
public function __construct( | ||
private CacheValueTransformer $transformer, | ||
private Closure|null $onSaveGuard = null, | ||
private Closure|null $onLoadGuard = null, | ||
) {} | ||
|
||
public function onLoad(string $value): mixed | ||
{ | ||
$transformed = $this->transformer->onLoad($value); | ||
|
||
if ($this->onLoadGuard !== null) { | ||
call_user_func($this->onLoadGuard, $transformed); | ||
} | ||
|
||
return $transformed; | ||
} | ||
|
||
public function onSave(mixed $value): string | ||
{ | ||
if ($this->onSaveGuard !== null) { | ||
call_user_func($this->onSaveGuard, $value); | ||
} | ||
|
||
return $this->transformer->onSave($value); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Tests\Fixtures; | ||
|
||
use Carbon\CarbonInterval; | ||
use EriBloo\CacheObjects\Concerns\CacheObjectActions; | ||
use EriBloo\CacheObjects\Contracts\CacheObject; | ||
use EriBloo\CacheObjects\ValueObjects\Keys\StringKey; | ||
use EriBloo\CacheObjects\ValueObjects\Values\GuardTransformer; | ||
use EriBloo\CacheObjects\ValueObjects\Values\SerializeTransformer; | ||
use Closure; | ||
|
||
/** | ||
* @template ObjectWithTime | ||
* | ||
* @implements CacheObject<ObjectWithTime> | ||
*/ | ||
final class GuardCacheObject implements CacheObject | ||
{ | ||
use CacheObjectActions; | ||
|
||
public function __construct( | ||
private Closure|null $onSaveGuard, | ||
private Closure|null $onLoadGuard, | ||
) {} | ||
|
||
public function key(): StringKey | ||
{ | ||
return new StringKey('guard-cache-object'); | ||
} | ||
|
||
public function ttl(): CarbonInterval | ||
{ | ||
return CarbonInterval::seconds(0); | ||
} | ||
|
||
/** | ||
* @return GuardTransformer<ObjectWithTime> | ||
*/ | ||
public function transformer(): GuardTransformer | ||
{ | ||
return new GuardTransformer(new SerializeTransformer(), $this->onSaveGuard, $this->onLoadGuard); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Tests\Fixtures; | ||
|
||
use Carbon\CarbonInterface; | ||
|
||
final readonly class ObjectWithTime | ||
{ | ||
public function __construct( | ||
public CarbonInterface $time, | ||
) {} | ||
} |