Initialer Laravel Commit für BetiX
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

This commit is contained in:
2026-04-04 18:01:50 +02:00
commit 0280278978
374 changed files with 65210 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Facades\Crypt;
use InvalidArgumentException;
/**
* Encrypted decimal stored as ciphertext (string) with fixed scale.
* Works with string or numeric inputs. Always returns string with fixed scale.
*/
class EncryptedDecimal implements CastsAttributes
{
private int $scale;
public function __construct(int $scale = 4)
{
if ($scale < 0 || $scale > 18) {
throw new InvalidArgumentException('Invalid scale for EncryptedDecimal.');
}
$this->scale = $scale;
}
public function get($model, string $key, $value, array $attributes)
{
if ($value === null || $value === '') {
// Treat null as zero but do not mutate DB implicitly
return number_format(0, $this->scale, '.', '');
}
try {
$plain = Crypt::decryptString((string) $value);
} catch (\Throwable $e) {
// If value is not decryptable (legacy/plain), try to normalize as plain string
$plain = (string) $value;
}
return $this->normalize($plain);
}
public function set($model, string $key, $value, array $attributes)
{
if ($value === null || $value === '') {
return [$key => null];
}
$normalized = $this->normalize($value);
return [$key => Crypt::encryptString($normalized)];
}
private function normalize($value): string
{
// Accept numeric, string with comma/dot; normalize to string with fixed scale
$s = is_string($value) ? trim($value) : (string) $value;
$s = str_replace([',', ' '], ['', ''], $s);
if (!preg_match('/^-?\d*(?:\.\d+)?$/', $s)) {
throw new InvalidArgumentException('Invalid decimal value.');
}
if ($s === '' || $s === '-') {
$s = '0';
}
// Use integer math on scaled value to avoid float precision
$scale = $this->scale;
// Split integer and fractional part
$neg = str_starts_with($s, '-') ? '-' : '';
if ($neg) $s = substr($s, 1);
[$int, $frac] = array_pad(explode('.', $s, 2), 2, '');
$frac = substr($frac . str_repeat('0', $scale), 0, $scale);
$intPart = ltrim($int === '' ? '0' : $int, '0');
if ($intPart === '') { $intPart = '0'; }
$out = ($neg ? '-' : '') . $intPart;
if ($scale > 0) {
$out .= '.' . $frac;
}
return $out;
}
}