1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace roxblnfk\SmartStream\Matching; |
6
|
|
|
|
7
|
|
|
final class SimpleMatcherConfig |
8
|
|
|
{ |
9
|
|
|
/** @var array of array< 0: converter class; 1: mime type; 2: array of bucket class > */ |
10
|
|
|
private array $formats = []; |
11
|
|
|
/** @var string[][] */ |
12
|
|
|
private array $buckets = []; |
13
|
|
|
|
14
|
1 |
|
public function withFormat(string $format, string $converter, string $mimeType = null, array $buckets = []): self |
15
|
|
|
{ |
16
|
1 |
|
$new = clone $this; |
17
|
|
|
|
18
|
1 |
|
if (array_key_exists($format, $new->formats)) { |
19
|
|
|
# remove format from buckets array |
20
|
|
|
array_walk($new->buckets, static function (array &$formats) use ($format) { |
21
|
|
|
$formats = array_diff($formats, [$format]); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
$new->formats[$format] = [$converter, $mimeType, $buckets]; |
26
|
|
|
|
27
|
1 |
|
foreach ($buckets as $bucket) { |
28
|
|
|
if (!is_string($bucket)) { |
29
|
|
|
throw new \InvalidArgumentException('Bucket should be string value.'); |
30
|
|
|
} |
31
|
|
|
if (!array_key_exists($bucket, $new->buckets)) { |
32
|
|
|
$new->buckets[$bucket] = []; |
33
|
|
|
} |
34
|
|
|
$new->buckets[$bucket][] = $format; |
35
|
|
|
} |
36
|
1 |
|
return $new; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function hasFormat(string $format): bool |
40
|
|
|
{ |
41
|
1 |
|
return array_key_exists($format, $this->formats); |
42
|
|
|
} |
43
|
|
|
public function getConverter(string $format): string |
44
|
|
|
{ |
45
|
|
|
return $this->formats[$format][0]; |
46
|
|
|
} |
47
|
|
|
public function getMimeType(string $format): ?string |
48
|
|
|
{ |
49
|
|
|
return $this->formats[$format][1]; |
50
|
|
|
} |
51
|
|
|
public function hasBucketFormat(string $bucket): bool |
52
|
|
|
{ |
53
|
|
|
return array_key_exists($bucket, $this->buckets); |
54
|
|
|
} |
55
|
|
|
public function getBucketFormats(string $bucket): array |
56
|
|
|
{ |
57
|
|
|
return $this->buckets[$bucket]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|