Files
mareike/app/Models/PaymentMethod.php
T

108 lines
3.4 KiB
PHP

<?php
namespace App\Models;
use App\Scopes\CommonModel;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
/**
* @property string $id
* @property string $slug
*/
class PaymentMethod extends CommonModel
{
use HasUuids;
public $incrementing = false;
protected $keyType = 'string';
public const string PAYMENT_ACCOUNT_TRANSACTION = 'PAYMENT_ACCOUNT_TRANSACTION';
public const string PAYMENT_NOT_DEFINED = 'PAYMENT_NOT_DEFINED';
public const array DEFAULTS = [
self::PAYMENT_ACCOUNT_TRANSACTION => ['name' => 'Überweisung auf Veranstaltungskonto', 'description' => null],
self::PAYMENT_NOT_DEFINED => ['name' => 'Sonstiges (Barzahlung, Zahlung vor Ort)', 'description' => null],
];
/**
* Options-Schema je Zahlungsmodul. Definiert im Code (nicht in der DB), damit es
* nicht mit dem Modul-Code auseinanderdriftet. In der DB stehen nur die Werte
* (configuration-JSON auf available_payment_methods bzw. event_payment_methods).
*
* Jede Option: ['name' => string, 'label' => string, 'type' => string, 'required' => bool]
*
* @var array<string, array<int, array{name: string, label: string, type: string, required: bool}>>
*/
public const array OPTIONS = [
self::PAYMENT_ACCOUNT_TRANSACTION => [
['name' => 'account_owner', 'label' => 'Kontoinhaber', 'type' => 'string', 'required' => true],
['name' => 'iban', 'label' => 'IBAN', 'type' => 'string', 'required' => true],
['name' => 'bic', 'label' => 'BIC', 'type' => 'string', 'required' => false],
],
self::PAYMENT_NOT_DEFINED => [],
];
protected $fillable = [
'slug',
];
/**
* Options-Schema für einen Slug.
*
* @return array<int, array{name: string, label: string, type: string, required: bool}>
*/
public static function optionsFor(string $slug): array
{
return self::OPTIONS[$slug] ?? [];
}
/**
* Namen aller Pflicht-Optionen eines Slugs.
*
* @return array<int, string>
*/
public static function requiredOptionKeys(string $slug): array
{
return array_values(array_map(
static fn (array $option) => $option['name'],
array_filter(self::optionsFor($slug), static fn (array $option) => $option['required'] ?? false)
));
}
/**
* Reduziert eine Konfiguration auf die im Code definierten Options-Keys des Slugs.
* Unbekannte Keys werden verworfen -- das Schema (self::OPTIONS) ist die Autorität.
*
* @param array<string, mixed> $config
* @return array<string, mixed>
*/
public static function sanitizeConfiguration(string $slug, array $config): array
{
$result = [];
foreach (self::optionsFor($slug) as $option) {
if (array_key_exists($option['name'], $config)) {
$result[$option['name']] = $config[$option['name']];
}
}
return $result;
}
/**
* Prüft, ob alle Pflicht-Optionen eines Slugs in der Konfiguration befüllt (non-empty) sind.
*
* @param array<string, mixed> $config
*/
public static function isConfigurationComplete(string $slug, array $config): bool
{
foreach (self::requiredOptionKeys($slug) as $key) {
$value = $config[$key] ?? null;
if ($value === null || $value === '' || $value === []) {
return false;
}
}
return true;
}
}