[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/bantu/ini-get-wrapper/src/ -> IniGetWrapper.php (source)

   1  <?php
   2  
   3  /*
   4  * (c) Andreas Fischer <git@andreasfischer.net>
   5  *
   6  * For the full copyright and license information, please view the LICENSE
   7  * file that was distributed with this source code.
   8  */
   9  
  10  namespace bantu\IniGetWrapper;
  11  
  12  /**
  13  * Wrapper class around built-in ini_get() function.
  14  *
  15  * Provides easier handling of the different interpretations of ini values.
  16  */
  17  class IniGetWrapper
  18  {
  19      /**
  20      * Simple wrapper around ini_get()
  21      * See http://php.net/manual/en/function.ini-get.php
  22      *
  23      * @param string $varname  The configuration option name.
  24      * @return null|string     Null if configuration option does not exist.
  25      *                         The configuration option value (string) otherwise.
  26      */
  27      public function get($varname)
  28      {
  29          $value = $this->getPhp($varname);
  30          return $value === false ? null : $value;
  31      }
  32  
  33      /**
  34      * Gets the configuration option value as a trimmed string.
  35      *
  36      * @param string $varname  The configuration option name.
  37      * @return null|string     Null if configuration option does not exist.
  38      *                         The configuration option value (string) otherwise.
  39      */
  40      public function getString($varname)
  41      {
  42          $value = $this->get($varname);
  43          return $value === null ? null : trim($value);
  44      }
  45  
  46      /**
  47      * Gets configuration option value as a boolean.
  48      * Interprets the string value 'off' as false.
  49      *
  50      * @param string $varname  The configuration option name.
  51      * @return null|bool       Null if configuration option does not exist.
  52      *                         False if configuration option is disabled.
  53      *                         True otherwise.
  54      */
  55      public function getBool($varname)
  56      {
  57          $value = $this->getString($varname);
  58          return $value === null ? null : $value && strtolower($value) !== 'off';
  59      }
  60  
  61      /**
  62      * Gets configuration option value as an integer.
  63      *
  64      * @param string $varname  The configuration option name.
  65      * @return null|int|float  Null if configuration option does not exist or is not numeric.
  66      *                         The configuration option value (integer or float) otherwise.
  67      */
  68      public function getNumeric($varname)
  69      {
  70          $value = $this->getString($varname);
  71          return is_numeric($value) ? $value + 0 : null;
  72      }
  73  
  74      /**
  75      * Gets configuration option value in bytes.
  76      * Converts strings like '128M' to bytes (integer or float).
  77      *
  78      * @param string $varname  The configuration option name.
  79      * @return null|int|float  Null if configuration option does not exist or is not well-formed.
  80      *                         The configuration option value as bytes (integer or float) otherwise.
  81      */
  82      public function getBytes($varname)
  83      {
  84          $value = $this->getString($varname);
  85  
  86          if ($value === null) {
  87              return null;
  88          }
  89  
  90          if (is_numeric($value)) {
  91              // Already in bytes.
  92              return $value + 0;
  93          }
  94  
  95          if (strlen($value) < 2 || strlen($value) < 3 && $value[0] === '-') {
  96              // Either a single character
  97              // or two characters where the first one is a minus.
  98              return null;
  99          }
 100  
 101          // Split string into numeric value and unit.
 102          $value_numeric = substr($value, 0, -1);
 103          if (!is_numeric($value_numeric)) {
 104              return null;
 105          }
 106  
 107          switch (strtolower($value[strlen($value) - 1])) {
 108              case 'g':
 109                  $value_numeric *= 1024;
 110                  // no break
 111              case 'm':
 112                  $value_numeric *= 1024;
 113                  // no break
 114              case 'k':
 115                  $value_numeric *= 1024;
 116                  break;
 117  
 118              default:
 119                  // It's not already in bytes (and thus numeric)
 120                  // and does not carry a unit.
 121                  return null;
 122          }
 123  
 124          return $value_numeric;
 125      }
 126  
 127      /**
 128      * Gets configuration option value as a list (array).
 129      * Converts comma-separated string into list (array).
 130      *
 131      * @param string $varname  The configuration option name.
 132      * @return null|array      Null if configuration option does not exist.
 133      *                         The configuration option value as a list (array) otherwise.
 134      */
 135      public function getList($varname)
 136      {
 137          $value = $this->getString($varname);
 138          return $value === null ? null : explode(',', $value);
 139      }
 140  
 141      /**
 142      * Checks whether a list contains a given element (string).
 143      *
 144      * @param string $varname  The configuration option name.
 145      * @param string $needle   The element to check whether it is contained in the list.
 146      * @return null|bool       Null if configuration option does not exist.
 147      *                         Whether $needle is contained in the list otherwise.
 148      */
 149      public function listContains($varname, $needle)
 150      {
 151          $list = $this->getList($varname);
 152          return $list === null ? null : in_array($needle, $list, true);
 153      }
 154  
 155      /**
 156      * @param string $varname  The configuration option name.
 157      */
 158      protected function getPhp($varname)
 159      {
 160          return ini_get($varname);
 161      }
 162  }


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