fix: harden report class loading and query parameters
This commit is contained in:
@@ -1,31 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace App\Domain\Reporting {
|
|
||||||
|
|
||||||
use \ReportFilters;
|
namespace App\Domain\Reporting {
|
||||||
use \ReportDataMapper;
|
|
||||||
use \ReportQuery;
|
|
||||||
use \ReportAudience;
|
|
||||||
|
|
||||||
require_once __DIR__ . '/ReportFilters.php';
|
require_once __DIR__ . '/ReportFilters.php';
|
||||||
require_once __DIR__ . '/ReportDataMapper.php';
|
require_once __DIR__ . '/ReportDataMapper.php';
|
||||||
require_once __DIR__ . '/ReportQuery.php';
|
require_once __DIR__ . '/ReportQuery.php';
|
||||||
require_once __DIR__ . '/ReportAudience.php';
|
require_once __DIR__ . '/ReportAudience.php';
|
||||||
|
|
||||||
|
use \ReportFilters;
|
||||||
|
use \ReportDataMapper;
|
||||||
|
use \ReportQuery;
|
||||||
|
use \ReportAudience;
|
||||||
|
|
||||||
final class ClientHistoryReport implements ReportQuery
|
final class ClientHistoryReport implements ReportQuery
|
||||||
{
|
{
|
||||||
public function __construct(private readonly ?ReportFilters $filters = null, private readonly ?ReportDataMapper $mapper = null) {}
|
public function __construct(private readonly ?ReportFilters $filters = null, private readonly ?ReportDataMapper $mapper = null) {}
|
||||||
|
|
||||||
/** @param list<array<string,mixed>> $rows @return list<array<string,mixed>> */
|
/** @param list<array<string,mixed>> $rows @return list<array<string,mixed>> */
|
||||||
public function build(array $rows, string $audience = 'client'): array
|
public function build(array $rows, string $audience = 'client'): array
|
||||||
{
|
{
|
||||||
ReportAudience::validate($audience);
|
ReportAudience::validate($audience);
|
||||||
$filters = $this->filters ?? new ReportFilters(); $mapper = $this->mapper ?? new ReportDataMapper(); $selected = [];
|
$filters = $this->filters ?? new ReportFilters();
|
||||||
|
$mapper = $this->mapper ?? new ReportDataMapper();
|
||||||
|
$selected = [];
|
||||||
foreach ($rows as $row) if ($filters->matches($row)) $selected[] = $row;
|
foreach ($rows as $row) if ($filters->matches($row)) $selected[] = $row;
|
||||||
usort($selected, static fn(array $a, array $b): int => strcmp((string)($a['changed_at'] ?? ''), (string)($b['changed_at'] ?? '')) ?: ((int)($a['id'] ?? $a['jobcard_id'] ?? 0) <=> (int)($b['id'] ?? $b['jobcard_id'] ?? 0)) ?: strcmp((string)($a['reference_no'] ?? ''), (string)($b['reference_no'] ?? '')));
|
usort($selected, static fn(array $a, array $b): int => strcmp((string)($a['changed_at'] ?? ''), (string)($b['changed_at'] ?? '')) ?: ((int)($a['id'] ?? $a['jobcard_id'] ?? 0) <=> (int)($b['id'] ?? $b['jobcard_id'] ?? 0)) ?: strcmp((string)($a['reference_no'] ?? ''), (string)($b['reference_no'] ?? '')));
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($selected as $row) $result[] = $audience === ReportAudience::INTERNAL ? $mapper->internalHistory($row) : $mapper->clientHistory($row);
|
foreach ($selected as $row) $result[] = $audience === ReportAudience::INTERNAL ? $mapper->internalHistory($row) : $mapper->clientHistory($row);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query(array $rows, string $audience = 'client'): array { return $this->build($rows, $audience); }
|
public function query(array $rows, string $audience = 'client'): array { return $this->build($rows, $audience); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user