vendor/exercise/htmlpurifier-bundle/src/HTMLPurifierConfigFactory.php line 49

Open in your IDE?
  1. <?php
  2. namespace Exercise\HTMLPurifierBundle;
  3. class HTMLPurifierConfigFactory
  4. {
  5.     /**
  6.      * @param string                    $profile       The configuration name, also used as cache id
  7.      * @param array                     $configArray   The config array to set up
  8.      * @param \HTMLPurifier_Config|null $defaultConfig The default config that every config must inherit or null if
  9.      *                                                 default
  10.      * @param array                     $parents       An array of config arrays to inherit by preloading or null
  11.      * @param array                     $attributes    A nullable array of rules as arrays by tag name holding two
  12.      *                                                 string elements, the first for the attribute name and the second
  13.      *                                                 for the rule (i.e: Text, ID, ...)
  14.      *                                                 [ ['img' => ['src' => 'URI', 'data-type' => Text']] ]
  15.      * @param array                     $elements      An array of arrays by element to add or override, arrays must
  16.      *                                                 hold a type ("Inline, "Block", ...), a content type ("Empty",
  17.      *                                                 "Optional: #PCDATA", ...), an attributes set ("Core", "Common",
  18.      *                                                 ...), a fourth optional may define attributes rules as array, and
  19.      *                                                 a fifth to list forbidden attributes
  20.      * @param array                     $blankElements An array of tag names that should not have any attributes
  21.      */
  22.     public static function create(
  23.         string $profile,
  24.         array $configArray,
  25.         \HTMLPurifier_Config $defaultConfig null,
  26.         array $parents = [],
  27.         array $attributes = [],
  28.         array $elements = [],
  29.         array $blankElements = []
  30.     ): \HTMLPurifier_Config {
  31.         if ($defaultConfig) {
  32.             $config = \HTMLPurifier_Config::inherit($defaultConfig);
  33.         } else {
  34.             $config = \HTMLPurifier_Config::createDefault();
  35.         }
  36.         foreach ($parents as $parent) {
  37.             $config->loadArray($parent);
  38.         }
  39.         $config->loadArray($configArray);
  40.         // Make the config unique
  41.         $config->set('HTML.DefinitionID'$profile);
  42.         $config->set('HTML.DefinitionRev'1);
  43.         $def $config->maybeGetRawHTMLDefinition();
  44.         // If the definition is not cached, build it
  45.         if ($def && ($attributes || $elements || $blankElements)) {
  46.             static::buildHTMLDefinition($def$attributes$elements$blankElements);
  47.         }
  48.         return $config;
  49.     }
  50.     /**
  51.      * Builds a config definition from the given parameters.
  52.      *
  53.      * This build should never happen on runtime, since purifiers cache should
  54.      * be generated during warm up.
  55.      */
  56.     public static function buildHTMLDefinition(\HTMLPurifier_Definition $def, array $attributes, array $elements, array $blankElements): void
  57.     {
  58.         foreach ($attributes as $elementName => $rule) {
  59.             foreach ($rule as $attributeName => $definition) {
  60.                 /* @see \HTMLPurifier_AttrTypes */
  61.                 $def->addAttribute($elementName$attributeName$definition);
  62.             }
  63.         }
  64.         foreach ($elements as $elementName => $config) {
  65.             /* @see \HTMLPurifier_HTMLModule::addElement() */
  66.             $el $def->addElement($elementName$config[0], $config[1], $config[2], isset($config[3]) ? $config[3] : []);
  67.             if (isset($config[4])) {
  68.                 $el->excludes array_fill_keys($config[4], true);
  69.             }
  70.         }
  71.         foreach ($blankElements as $blankElement) {
  72.             /* @see \HTMLPurifier_HTMLModule::addBlankElement() */
  73.             $def->addBlankElement($blankElement);
  74.         }
  75.     }
  76. }