|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Swis\Laravel\Encrypted\Casts; |
| 6 | + |
| 7 | +use Carbon\CarbonImmutable; |
| 8 | +use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Support\Carbon; |
| 11 | +use Illuminate\Support\Facades\Crypt; |
| 12 | +use Illuminate\Support\Facades\Date; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + * |
| 17 | + * @implements \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Carbon|\Carbon\CarbonImmutable, string|\Illuminate\Support\Carbon|\Carbon\CarbonImmutable> |
| 18 | + */ |
| 19 | +class EncryptedDateTime implements CastsAttributes |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param array<int, string> $arguments |
| 23 | + * @param \Closure(Carbon|CarbonImmutable|null): (Carbon|CarbonImmutable|null)|null $modifier |
| 24 | + */ |
| 25 | + public function __construct(protected array $arguments, protected ?\Closure $modifier = null) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param array<string, mixed> $attributes |
| 31 | + */ |
| 32 | + public function get(Model $model, string $key, mixed $value, array $attributes): Carbon|CarbonImmutable|null |
| 33 | + { |
| 34 | + if ($value === null) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $value = ($model::$encrypter ?? Crypt::getFacadeRoot())->decrypt($value, false); |
| 39 | + |
| 40 | + return with($this->parse($model, $key, $value, $attributes), $this->modifier); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param \Illuminate\Support\Carbon|\Carbon\CarbonImmutable|string|null $value |
| 45 | + * @param array<string, mixed> $attributes |
| 46 | + */ |
| 47 | + public function set(Model $model, string $key, #[\SensitiveParameter] mixed $value, array $attributes): ?string |
| 48 | + { |
| 49 | + if ($value === null) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + $value = is_string($value) ? $value : $value->format($model->getDateFormat()); |
| 54 | + |
| 55 | + return ($model::$encrypter ?? Crypt::getFacadeRoot())->encrypt($value, false); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param string|null $value |
| 60 | + * @param array<string, mixed> $attributes |
| 61 | + */ |
| 62 | + public function serialize(Model $model, string $key, #[\SensitiveParameter] mixed $value, array $attributes): ?string |
| 63 | + { |
| 64 | + if ($value === null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + return !empty($this->arguments[0]) ? Date::parse($value)->format($this->arguments[0]) : $value; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param string|null $original |
| 73 | + * @param string|null $value |
| 74 | + */ |
| 75 | + public function compare(Model $model, string $key, mixed $original, mixed $value): bool |
| 76 | + { |
| 77 | + if (!empty(($model::$encrypter ?? Crypt::getFacadeRoot())->getPreviousKeys())) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + $original = $this->get($model, $key, $original, []); |
| 82 | + $value = $this->get($model, $key, $value, []); |
| 83 | + |
| 84 | + if ($original === $value) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + if ($original === null || $value === null) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return $original->equalTo($value); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param string|int $value |
| 97 | + * @param array<string, mixed> $attributes |
| 98 | + */ |
| 99 | + protected function parse(Model $model, string $key, #[\SensitiveParameter] mixed $value, array $attributes): Carbon|CarbonImmutable|null |
| 100 | + { |
| 101 | + if (is_numeric($value)) { |
| 102 | + return Date::createFromTimestamp($value, date_default_timezone_get()); |
| 103 | + } |
| 104 | + |
| 105 | + if ($this->isStandardDateFormat($value)) { |
| 106 | + return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->startOfDay()); |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + return Date::createFromFormat($model->getDateFormat(), $value); |
| 111 | + } catch (\InvalidArgumentException) { |
| 112 | + return Date::parse($value); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + protected function isStandardDateFormat(#[\SensitiveParameter] string $value): bool |
| 117 | + { |
| 118 | + return (bool) preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value); |
| 119 | + } |
| 120 | +} |
0 commit comments