[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/JavaScript/ -> FunctionProvider.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\Configurator\JavaScript;
   9  
  10  use InvalidArgumentException;
  11  
  12  class FunctionProvider
  13  {
  14      /**
  15      * @param array Function name as keys, JavaScript source as values
  16      */
  17      public static $cache = [
  18          'addslashes' => 'function(str)
  19  {
  20      return str.replace(/["\'\\\\]/g, \'\\\\$&\').replace(/\\u0000/g, \'\\\\0\');
  21  }',
  22          'dechex' => 'function(str)
  23  {
  24      return parseInt(str).toString(16);
  25  }',
  26          'intval' => 'function(str)
  27  {
  28      return parseInt(str) || 0;
  29  }',
  30          'ltrim' => 'function(str)
  31  {
  32      return str.replace(/^[ \\n\\r\\t\\0\\x0B]+/g, \'\');
  33  }',
  34          'mb_strtolower' => 'function(str)
  35  {
  36      return str.toLowerCase();
  37  }',
  38          'mb_strtoupper' => 'function(str)
  39  {
  40      return str.toUpperCase();
  41  }',
  42          'mt_rand' => 'function(min, max)
  43  {
  44      return (min + Math.floor(Math.random() * (max + 1 - min)));
  45  }',
  46          'rawurlencode' => 'function(str)
  47  {
  48      return encodeURIComponent(str).replace(
  49          /[!\'()*]/g,
  50          /**
  51          * @param {string} c
  52          */
  53          function(c)
  54          {
  55              return \'%\' + c.charCodeAt(0).toString(16).toUpperCase();
  56          }
  57      );
  58  }',
  59          'rtrim' => 'function(str)
  60  {
  61      return str.replace(/[ \\n\\r\\t\\0\\x0B]+$/g, \'\');
  62  }',
  63          'str_rot13' => 'function(str)
  64  {
  65      return str.replace(
  66          /[a-z]/gi,
  67          function(c)
  68          {
  69              return String.fromCharCode(c.charCodeAt(0) + ((c.toLowerCase() < \'n\') ? 13 : -13));
  70          }
  71      );
  72  }',
  73          'stripslashes' => 'function(str)
  74  {
  75      // NOTE: this will not correctly transform \\0 into a NULL byte. I consider this a feature
  76      //       rather than a bug. There\'s no reason to use NULL bytes in a text.
  77      return str.replace(/\\\\([\\s\\S]?)/g, \'\\\\1\');
  78  }',
  79          'strrev' => 'function(str)
  80  {
  81      return str.split(\'\').reverse().join(\'\');
  82  }',
  83          'strtolower' => 'function(str)
  84  {
  85      return str.toLowerCase();
  86  }',
  87          'strtotime' => 'function(str)
  88  {
  89      return Date.parse(str) / 1000;
  90  }',
  91          'strtoupper' => 'function(str)
  92  {
  93      return str.toUpperCase();
  94  }',
  95          'trim' => 'function(str)
  96  {
  97      return str.replace(/^[ \\n\\r\\t\\0\\x0B]+/g, \'\').replace(/[ \\n\\r\\t\\0\\x0B]+$/g, \'\');
  98  }',
  99          'ucfirst' => 'function(str)
 100  {
 101      return str[0].toUpperCase() + str.substring(1);
 102  }',
 103          'ucwords' => 'function(str)
 104  {
 105      return str.replace(
 106          /(?:^|\\s)[a-z]/g,
 107          function(m)
 108          {
 109              return m.toUpperCase()
 110          }
 111      );
 112  }',
 113          'urldecode' => 'function(str)
 114  {
 115      return decodeURIComponent("" + str);
 116  }',
 117          'urlencode' => 'function(str)
 118  {
 119      return encodeURIComponent(str);
 120  }'
 121      ];
 122  
 123      /**
 124      * Return a function's source from the cache or the filesystem
 125      *
 126      * @param  string $funcName Function's name
 127      * @return string           Function's source
 128      */
 129  	public static function get($funcName)
 130      {
 131          if (isset(self::$cache[$funcName]))
 132          {
 133              return self::$cache[$funcName];
 134          }
 135          if (preg_match('(^[a-z_0-9]+$)D', $funcName))
 136          {
 137              $filepath = __DIR__ . '/functions/' . $funcName . '.js';
 138              if (file_exists($filepath))
 139              {
 140                  return file_get_contents($filepath);
 141              }
 142          }
 143          throw new InvalidArgumentException("Unknown function '" . $funcName . "'");
 144      }
 145  }


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