You are here

public static function BaseExtension::validateSettings in Markdown 8.2

Validates extension settings.

Parameters

array $settings: The extension settings to validate.

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

File

src/Plugin/Markdown/BaseExtension.php, line 38

Class

BaseExtension
Base class for markdown extensions.

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;
    $extensionId = $parent instanceof Mapping && ($id = $parent
      ->get('id')) ? $id
      ->getValue() : NULL;
    $extensionManager = ExtensionManager::create();
    if (!$extensionId || !$extensionManager
      ->hasDefinition($extensionId)) {
      throw new \RuntimeException(sprintf('Unknown markdown extension: "%s"', $extensionId));
    }
    $extension = $extensionManager
      ->createInstance($extensionId);

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