blob: 5d918c062d102fb98b56d6cc47a766e8ad9bf3f7 (
plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?php
namespace r7r\cms\sys\textprocessors;
use Exception;
class TextprocessorRepository
{
/** @var Textprocessor[] */
private $textprocessors = [];
/**
* Builds a repository with the default textprocessors prepopulated.
* @return self
*/
public static function buildDefault(): self
{
$repo = new self();
$repo->register("Markdown", new MarkdownProcessor());
$repo->register("Plain Text", new PlainTextProcessor());
$repo->register("HTML", new HtmlProcessor());
return $repo;
}
public function register(string $name, Textprocessor $textprocessor): void
{
$this->textprocessors[$name] = $textprocessor;
}
public function getTextprocessor(string $name): ?Textprocessor
{
return $this->textprocessors[$name] ?? null;
}
/**
* @return Textprocessor[]
*/
public function all(): array
{
return $this->textprocessors;
}
/**
* Apply a textprocessor to the input text.
*
* @param string $input The input text
* @param string $name The name of the textprocessor
* @return string|null Will return null, if the textprocessor was not found
*/
public function apply(string $input, string $name): ?string
{
$textprocessor = $this->getTextprocessor($name);
return $textprocessor === null ? null : $textprocessor->apply($input);
}
/**
* Like {@see TextprocessorRepository::apply()}, but will throw an exception instead of returning null, if the textprocessor was not found.
*
* @param string $input The input text
* @param string $name The name of the textprocessor
* @return string
* @throws Exception
*/
public function mustApply(string $input, string $name): string
{
$out = $this->apply($input, $name);
if ($out === null) {
throw new Exception("Unknown Textprocessor: $name");
}
return $out;
}
}
|