library/log4php/helpers/LoggerOptionConverter.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     4  * contributor license agreements. See the NOTICE file distributed with
       
     5  * this work for additional information regarding copyright ownership.
       
     6  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     7  * (the "License"); you may not use this file except in compliance with
       
     8  * the License. You may obtain a copy of the License at
       
     9  *
       
    10  *       http://www.apache.org/licenses/LICENSE-2.0
       
    11  *
       
    12  * Unless required by applicable law or agreed to in writing, software
       
    13  * distributed under the License is distributed on an "AS IS" BASIS,
       
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    15  * See the License for the specific language governing permissions and
       
    16  * limitations under the License.
       
    17  *
       
    18  * @package log4php
       
    19  */
       
    20 
       
    21 /**
       
    22  * A convenience class to convert property values to specific types.
       
    23  *
       
    24  * @version $Revision: 1374617 $
       
    25  * @package log4php
       
    26  * @subpackage helpers
       
    27  * @since 0.5
       
    28  */
       
    29 class LoggerOptionConverter {
       
    30 
       
    31     /** String values which are converted to boolean TRUE. */
       
    32     private static $trueValues = array('1', 'true', 'yes', 'on');
       
    33 
       
    34     /**
       
    35      * String values which are converted to boolean FALSE.
       
    36      *
       
    37      * Note that an empty string must convert to false, because
       
    38      * parse_ini_file() which is used for parsing configuration
       
    39      * converts the value _false_ to an empty string.
       
    40      */
       
    41     private static $falseValues = array('0', 'false', 'no', 'off', '');
       
    42 
       
    43     /**
       
    44      * Read a predefined var.
       
    45      *
       
    46      * It returns a value referenced by <var>$key</var> using this search criteria:
       
    47      * - if <var>$key</var> is a constant then return it. Else
       
    48      * - if <var>$key</var> is set in <var>$_ENV</var> then return it. Else
       
    49      * - return <var>$def</var>.
       
    50      *
       
    51      * @param string $key The key to search for.
       
    52      * @param string $def The default value to return.
       
    53      * @return string    the string value of the system property, or the default
       
    54      *                    value if there is no property with that key.
       
    55      */
       
    56     public static function getSystemProperty($key, $def) {
       
    57         if (defined($key)) {
       
    58             return (string)constant($key);
       
    59         } else if (isset($_SERVER[$key])) {
       
    60             return (string)$_SERVER[$key];
       
    61         } else if (isset($_ENV[$key])) {
       
    62             return (string)$_ENV[$key];
       
    63         } else {
       
    64             return $def;
       
    65         }
       
    66     }
       
    67 
       
    68     /** Converts $value to boolean, or throws an exception if not possible. */
       
    69     public static function toBooleanEx($value) {
       
    70         if (isset($value)) {
       
    71             if (is_bool($value)) {
       
    72                 return $value;
       
    73             }
       
    74             $value = strtolower(trim($value));
       
    75             if (in_array($value, self::$trueValues)) {
       
    76                 return true;
       
    77             }
       
    78             if (in_array($value, self::$falseValues)) {
       
    79                 return false;
       
    80             }
       
    81         }
       
    82 
       
    83         throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to boolean.");
       
    84     }
       
    85 
       
    86     /**
       
    87      * Converts $value to integer, or throws an exception if not possible.
       
    88      * Floats cannot be converted to integer.
       
    89      */
       
    90     public static function toIntegerEx($value) {
       
    91         if (is_integer($value)) {
       
    92             return $value;
       
    93         }
       
    94         if (is_numeric($value) && ($value == (integer)$value)) {
       
    95             return (integer)$value;
       
    96         }
       
    97 
       
    98         throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to integer.");
       
    99     }
       
   100 
       
   101     /**
       
   102      * Converts $value to integer, or throws an exception if not possible.
       
   103      * Floats cannot be converted to integer.
       
   104      */
       
   105     public static function toPositiveIntegerEx($value) {
       
   106         if (is_integer($value) && $value > 0) {
       
   107             return $value;
       
   108         }
       
   109         if (is_numeric($value) && ($value == (integer)$value) && $value > 0) {
       
   110             return (integer)$value;
       
   111         }
       
   112 
       
   113         throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a positive integer.");
       
   114     }
       
   115 
       
   116     /** Converts the value to a level. Throws an exception if not possible. */
       
   117     public static function toLevelEx($value) {
       
   118         if ($value instanceof LoggerLevel) {
       
   119             return $value;
       
   120         }
       
   121         $level = LoggerLevel::toLevel($value);
       
   122         if ($level === null) {
       
   123             throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a logger level.");
       
   124         }
       
   125         return $level;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Converts a value to a valid file size (integer).
       
   130      *
       
   131      * Supports 'KB', 'MB' and 'GB' suffixes, where KB = 1024 B etc.
       
   132      *
       
   133      * The final value will be rounded to the nearest integer.
       
   134      *
       
   135      * Examples:
       
   136      * - '100' => 100
       
   137      * - '100.12' => 100
       
   138      * - '100KB' => 102400
       
   139      * - '1.5MB' => 1572864
       
   140      *
       
   141      * @param mixed $value File size (optionally with suffix).
       
   142      * @return integer Parsed file size.
       
   143      */
       
   144     public static function toFileSizeEx($value) {
       
   145 
       
   146         if (empty($value)) {
       
   147             throw new LoggerException("Empty value cannot be converted to a file size.");
       
   148         }
       
   149 
       
   150         if (is_numeric($value)) {
       
   151             return (integer)$value;
       
   152         }
       
   153 
       
   154         if (!is_string($value)) {
       
   155             throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a file size.");
       
   156         }
       
   157 
       
   158         $str = strtoupper(trim($value));
       
   159         $count = preg_match('/^([0-9.]+)(KB|MB|GB)?$/', $str, $matches);
       
   160 
       
   161         if ($count > 0) {
       
   162             $size = $matches[1];
       
   163             $unit = $matches[2];
       
   164 
       
   165             switch ($unit) {
       
   166                 case 'KB':
       
   167                     $size *= pow(1024, 1);
       
   168                     break;
       
   169                 case 'MB':
       
   170                     $size *= pow(1024, 2);
       
   171                     break;
       
   172                 case 'GB':
       
   173                     $size *= pow(1024, 3);
       
   174                     break;
       
   175             }
       
   176 
       
   177             return (integer)$size;
       
   178         }
       
   179 
       
   180         throw new LoggerException("Given value [$value] cannot be converted to a file size.");
       
   181     }
       
   182 
       
   183     /**
       
   184      * Converts a value to string, or throws an exception if not possible.
       
   185      *
       
   186      * Objects can be converted to string if they implement the magic
       
   187      * __toString() method.
       
   188      *
       
   189      */
       
   190     public static function toStringEx($value) {
       
   191         if (is_string($value)) {
       
   192             return $value;
       
   193         }
       
   194         if (is_numeric($value)) {
       
   195             return (string)$value;
       
   196         }
       
   197         if (is_object($value) && method_exists($value, '__toString')) {
       
   198             return (string)$value;
       
   199         }
       
   200 
       
   201         throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to string.");
       
   202     }
       
   203 
       
   204     /**
       
   205      * Performs value substitution for string options.
       
   206      *
       
   207      * An option can contain PHP constants delimited by '${' and '}'.
       
   208      *
       
   209      * E.g. for input string "some ${FOO} value", the method will attempt
       
   210      * to substitute ${FOO} with the value of constant FOO if it exists.
       
   211      *
       
   212      * Therefore, if FOO is a constant, and it has value "bar", the resulting
       
   213      * string will be "some bar value".
       
   214      *
       
   215      * If the constant is not defined, it will be replaced by an empty string,
       
   216      * and the resulting string will be "some  value".
       
   217      *
       
   218      * @param string $string String on which to perform substitution.
       
   219      * @return string
       
   220      */
       
   221     public static function substConstants($string) {
       
   222         preg_match_all('/\${([^}]+)}/', $string, $matches);
       
   223 
       
   224         foreach ($matches[1] as $key => $match) {
       
   225             $match = trim($match);
       
   226             $search = $matches[0][$key];
       
   227             $replacement = defined($match) ? constant($match) : '';
       
   228             $string = str_replace($search, $replacement, $string);
       
   229         }
       
   230         return $string;
       
   231     }
       
   232 }