[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Keywords/ -> Configurator.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\Keywords;
   9  
  10  use s9e\TextFormatter\Configurator\Collections\NormalizedList;
  11  use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
  12  use s9e\TextFormatter\Configurator\Items\Regexp;
  13  use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
  14  use s9e\TextFormatter\Plugins\ConfiguratorBase;
  15  
  16  /**
  17  * @method mixed   add(string $key, mixed $value) Add an item to this collection
  18  * @method array   asConfig()
  19  * @method void    clear()                        Empty this collection
  20  * @method bool    contains(mixed $value)         Test whether a given value is present in this collection
  21  * @method integer count()
  22  * @method mixed   current()
  23  * @method void    delete(string $key)            Delete an item from this collection
  24  * @method bool    exists(string $key)            Test whether an item of given key exists
  25  * @method mixed   get(string $key)               Return a value from this collection
  26  * @method mixed   indexOf(mixed $value)          Find the index of a given value
  27  * @method integer|string key()
  28  * @method mixed   next()
  29  * @method string  normalizeKey(string $key)      Normalize an item's key
  30  * @method mixed   normalizeValue(mixed $value)   Normalize a value for storage
  31  * @method bool    offsetExists(string|integer $offset)
  32  * @method mixed   offsetGet(string|integer $offset)
  33  * @method void    offsetSet(string|integer $offset, mixed $value)
  34  * @method void    offsetUnset(string|integer $offset)
  35  * @method string  onDuplicate(string|null $action) Query and set the action to take when add() is called with a key that already exists
  36  * @method void    rewind()
  37  * @method mixed   set(string $key, mixed $value) Set and overwrite a value in this collection
  38  * @method bool    valid()
  39  */
  40  class Configurator extends ConfiguratorBase
  41  {
  42      use CollectionProxy;
  43  
  44      /**
  45      * @var string Name of the attribute used by this plugin
  46      */
  47      protected $attrName = 'value';
  48  
  49      /**
  50      * @var bool Whether keywords are case-sensitive
  51      */
  52      public $caseSensitive = true;
  53  
  54      /**
  55      * @var \s9e\TextFormatter\Configurator\Collections\NormalizedCollection List of [keyword => value]
  56      */
  57      protected $collection;
  58  
  59      /**
  60      * @var boolean Whether to capture only the first occurence of each keyword
  61      */
  62      public $onlyFirst = false;
  63  
  64      /**
  65      * @var string Name of the tag used by this plugin
  66      */
  67      protected $tagName = 'KEYWORD';
  68  
  69      /**
  70      * {@inheritdoc}
  71      */
  72  	protected function setUp()
  73      {
  74          $this->collection = new NormalizedList;
  75  
  76          $this->configurator->tags->add($this->tagName)->attributes->add($this->attrName);
  77      }
  78  
  79      /**
  80      * {@inheritdoc}
  81      */
  82  	public function asConfig()
  83      {
  84          if (!count($this->collection))
  85          {
  86              return;
  87          }
  88  
  89          $config = [
  90              'attrName' => $this->attrName,
  91              'tagName'  => $this->tagName
  92          ];
  93  
  94          if (!empty($this->onlyFirst))
  95          {
  96              $config['onlyFirst'] = $this->onlyFirst;
  97          }
  98  
  99          // Sort keywords in order to keep keywords that start with the same characters together. We
 100          // also remove duplicates that would otherwise skew the length computation done below
 101          $keywords = array_unique(iterator_to_array($this->collection));
 102          sort($keywords);
 103  
 104          // Group keywords by chunks of ~30KB to remain below PCRE's limit
 105          $groups   = [];
 106          $groupKey = 0;
 107          $groupLen = 0;
 108          foreach ($keywords as $keyword)
 109          {
 110              // NOTE: the value 4 is a guesstimate for the cost of each alternation
 111              $keywordLen  = 4 + strlen($keyword);
 112              $groupLen   += $keywordLen;
 113  
 114              if ($groupLen > 30000)
 115              {
 116                  $groupLen = $keywordLen;
 117                  ++$groupKey;
 118              }
 119  
 120              $groups[$groupKey][] = $keyword;
 121          }
 122  
 123          foreach ($groups as $keywords)
 124          {
 125              $regexp = RegexpBuilder::fromList(
 126                  $keywords,
 127                  ['caseInsensitive' => !$this->caseSensitive]
 128              );
 129  
 130              $regexp = '/\\b' . $regexp . '\\b/S';
 131  
 132              if (!$this->caseSensitive)
 133              {
 134                  $regexp .= 'i';
 135              }
 136  
 137              if (preg_match('/[^[:ascii:]]/', $regexp))
 138              {
 139                  $regexp .= 'u';
 140              }
 141  
 142              $config['regexps'][] = new Regexp($regexp, true);
 143          }
 144  
 145          return $config;
 146      }
 147  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1