From 378881378aab5454c84cb1fecbbcc675f64dc27f Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 25 Sep 2020 21:18:18 +0200 Subject: Refactor textprocessors They are now managed by TextprocessorRepository and are instances of Textprocessor. This replaces the global $textprocessors variable. --- ratatoeskr/sys/textprocessors/HtmlProcessor.php | 20 ++++++ .../sys/textprocessors/LegacyTextprocessor.php | 32 +++++++++ .../sys/textprocessors/MarkdownProcessor.php | 22 +++++++ .../sys/textprocessors/PlainTextProcessor.php | 22 +++++++ ratatoeskr/sys/textprocessors/Textprocessor.php | 19 ++++++ .../sys/textprocessors/TextprocessorRepository.php | 76 ++++++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 ratatoeskr/sys/textprocessors/HtmlProcessor.php create mode 100644 ratatoeskr/sys/textprocessors/LegacyTextprocessor.php create mode 100644 ratatoeskr/sys/textprocessors/MarkdownProcessor.php create mode 100644 ratatoeskr/sys/textprocessors/PlainTextProcessor.php create mode 100644 ratatoeskr/sys/textprocessors/Textprocessor.php create mode 100644 ratatoeskr/sys/textprocessors/TextprocessorRepository.php (limited to 'ratatoeskr/sys/textprocessors') diff --git a/ratatoeskr/sys/textprocessors/HtmlProcessor.php b/ratatoeskr/sys/textprocessors/HtmlProcessor.php new file mode 100644 index 0000000..c3f691b --- /dev/null +++ b/ratatoeskr/sys/textprocessors/HtmlProcessor.php @@ -0,0 +1,20 @@ +fx = $fx; + $this->visible_in_backend = (bool)$visible_in_backend; + } + + public function apply(string $input): string + { + return (string)call_user_func($this->fx, $input); + } + + public function showInBackend(): bool + { + return $this->visible_in_backend; + } +} diff --git a/ratatoeskr/sys/textprocessors/MarkdownProcessor.php b/ratatoeskr/sys/textprocessors/MarkdownProcessor.php new file mode 100644 index 0000000..15f2f48 --- /dev/null +++ b/ratatoeskr/sys/textprocessors/MarkdownProcessor.php @@ -0,0 +1,22 @@ +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; + } +} -- cgit v1.2.3-54-g00ecf