-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileResultResourceExtractor.php
More file actions
61 lines (50 loc) · 1.62 KB
/
Copy pathCompileResultResourceExtractor.php
File metadata and controls
61 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Pdf;
use InvalidArgumentException;
use LibreSign\XObjectTemplate\Dto\CompileResult;
/** @internal */
class CompileResultResourceExtractor
{
/**
* @return array<string, array<string, mixed>>
*/
public function extract(CompileResult $result, string $resourceType, string $itemMessageTemplate): array
{
if (!array_key_exists($resourceType, $result->resources)) {
return [];
}
/** @var mixed $rawResources */
$rawResources = $result->resources[$resourceType];
$resources = $this->requireStringKeyedArray(
$rawResources,
sprintf('%s resources must be an array.', $resourceType),
);
$normalizedResources = [];
foreach (array_keys($resources) as $alias) {
$normalizedResources[$alias] = $this->requireStringKeyedArray(
$resources[$alias],
sprintf($itemMessageTemplate, $alias),
);
}
return $normalizedResources;
}
/**
* @return array<string, mixed>
*/
private function requireStringKeyedArray(mixed $value, string $message): array
{
if (!is_array($value)) {
throw new InvalidArgumentException($message);
}
foreach (array_keys($value) as $key) {
if (!is_string($key)) {
throw new InvalidArgumentException($message);
}
}
/** @var array<string, mixed> $value */
return $value;
}
}