• File: PaymentCollection.php
  • Full Path: /var/www/html/back/app/Collections/PaymentCollection.php
  • File size: 1.61 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

declare(strict_types=1);

namespace App\Collections;

use App\Models\Payment as PaymentEloquent;
use Illuminate\Database\Eloquent\Collection;

final class PaymentCollection extends Collection
{
    public function filter(?callable $callback = null): static
    {
        if (is_null(value: $callback)) {
            return parent::filter();
        }

        return parent::filter(
            callback: fn (PaymentEloquent $payment) => $callback($payment)
        );
    }

    public function matchesPayload(PaymentEloquent $payment, array $payload): bool
    {
        return match (true) {
            isset($payload['user_id']) && $payment->user_id == $payload['user_id'] => true,
            isset($payload['counterparty_id']) && $payment->counterparty_id == $payload['counterparty_id'] => true,
            isset($payload['invoice_id']) && $payment->invoices()->whereIn(
                column: 'id',
                values: (array) $payload['invoice_id']
            )->exists() => true,
            isset($payload['organisation_id']) && $payment->organisations()->whereIn(
                column: 'id',
                values: (array) $payload['organisation_id']
            )->exists() => true,
            isset($payload['project_id']) && $payment->projects()->whereIn(
                column: 'id',
                values: (array) $payload['project_id']
            )->exists() => true,
            isset($payload['article_id']) && $payment->articles()->whereIn(
                column: 'id',
                values: (array) $payload['article_id']
            )->exists() => true,
            default => false,
        };
    }
}