From edf14dcb0380a92a268f4869cb1acbb5e683abb3 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Mon, 13 Dec 2021 17:02:35 -0600 Subject: [PATCH] feat: improve tag support for parity with formatjs --- src/Config.php | 18 +++ src/ConfigInterface.php | 21 +++ src/FormatPHP.php | 5 + src/Icu/MessageFormat/Parser/Error.php | 7 +- .../UnableToParseMessageException.php | 62 +++++++ src/Intl/MessageFormat.php | 152 +++++++++++++++++- src/Intl/MessageFormatInterface.php | 30 +++- tests/ConfigTest.php | 8 +- tests/FormatPHPTest.php | 27 ++++ tests/Intl/MessageFormatTest.php | 127 +++++++++++++++ 10 files changed, 451 insertions(+), 6 deletions(-) create mode 100644 src/Icu/MessageFormat/Parser/Exception/UnableToParseMessageException.php diff --git a/src/Config.php b/src/Config.php index 4a988b3..812aed4 100644 --- a/src/Config.php +++ b/src/Config.php @@ -34,14 +34,24 @@ class Config implements ConfigInterface private ?LocaleInterface $defaultLocale; private string $idInterpolatorPattern; + /** + * @var array + */ + private array $defaultRichTextElements; + + /** + * @param array $defaultRichTextElements + */ public function __construct( LocaleInterface $locale, ?LocaleInterface $defaultLocale = null, + array $defaultRichTextElements = [], string $idInterpolatorPattern = IdInterpolator::DEFAULT_ID_INTERPOLATION_PATTERN ) { $this->locale = $locale; $this->defaultLocale = $defaultLocale; $this->idInterpolatorPattern = $idInterpolatorPattern; + $this->defaultRichTextElements = $defaultRichTextElements; } public function getDefaultLocale(): ?LocaleInterface @@ -58,4 +68,12 @@ public function getLocale(): LocaleInterface { return $this->locale; } + + /** + * @return array + */ + public function getDefaultRichTextElements(): array + { + return $this->defaultRichTextElements; + } } diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index aa9d6d2..17a2aa3 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -35,6 +35,27 @@ interface ConfigInterface */ public function getDefaultLocale(): ?LocaleInterface; + /** + * Returns a map of tag names to rich text formatting functions + * + * This is meant to provide a centralized way to format common tags such as + * ``, `

`, or enforcing a certain design system in the codebase + * (e.g., standardized ``, `