|
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
|
|
|
public function withFormat(string $format, string $converter, string $mimeType = null, array $buckets = []): self |
|
15
|
|
|
{ |
|
16
|
|
|
$clone = clone $this; |
|
17
|
|
|
|
|
18
|
|
|
if (array_key_exists($format, $clone->formats)) { |
|
19
|
|
|
# remove format from buckets array |
|
20
|
|
|
array_walk($clone->buckets, static function (array &$formats) use ($format) { |
|
21
|
|
|
$formats = array_diff($formats, [$format]); |
|
22
|
|
|
}); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$clone->formats[$format] = [$converter, $mimeType, $buckets]; |
|
26
|
|
|
|
|
27
|
|
|
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, $clone->buckets)) { |
|
32
|
|
|
$clone->buckets[$bucket] = []; |
|
33
|
|
|
} |
|
34
|
|
|
$clone->buckets[$bucket][] = [$format]; |
|
35
|
|
|
} |
|
36
|
|
|
return $clone; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function hasFormat(string $format): bool |
|
40
|
|
|
{ |
|
41
|
|
|
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
|
|
|
|