You are here

public static function BaseParser::validateSettings in Markdown 8.2

Validates parser settings.

Parameters

array $settings: The parser settings to validate.

\Symfony\Component\Validator\Context\ExecutionContextInterface $context: The validation execution context.

File

src/Plugin/Markdown/BaseParser.php, line 51

Class

BaseParser
Base class form Markdown Parser instances.

Namespace

Drupal\markdown\Plugin\Markdown

Code

public static function validateSettings(array $settings, ExecutionContextInterface $context) {
  try {
    $object = $context
      ->getObject();
    $parent = $object instanceof Mapping ? $object
      ->getParent() : NULL;
    $parserId = $parent instanceof Mapping && ($id = $parent
      ->get('id')) ? $id
      ->getValue() : NULL;
    $parserManager = ParserManager::create();
    if (!$parserId || !$parserManager
      ->hasDefinition($parserId)) {
      throw new \RuntimeException(sprintf('Unknown markdown parser: "%s"', $parserId));
    }
    $parser = $parserManager
      ->createInstance($parserId);

    // Immediately return if parser doesn't have any settings.
    if (!$parser instanceof SettingsInterface) {
      return;
    }
    $defaultSettings = $parser::defaultSettings($parser
      ->getPluginDefinition());
    $unknownSettings = array_keys(array_diff_key($settings, $defaultSettings));
    if ($unknownSettings) {
      throw new \RuntimeException(sprintf('Unknown parser settings: %s', implode(', ', $unknownSettings)));
    }
  } catch (\RuntimeException $exception) {
    $context
      ->addViolation($exception
      ->getMessage());
  }
}