Пример #1
0
 /**
  * Evaluate options
  *
  * An Example:
  * <code>
  *  $conf_options = array(
  *         // option  => array(Type, Default, Conf)
  *        'hierarchy' => array('bool', true),
  *        'num'       => array('interval', null),
  *        'filter'    => array('string', null),
  *        'sort'      => array('enum', 'name', array('name', 'reading', 'date')),
  *  );
  *  $options = array('filter'=>'AAA');
  *  list($options, $unknowns) = PluginSonotsOption::evaluate_options($options, $conf_options);
  *  var_export($options); // array('hierarchy'=>true,'num'=>null,'filter'=>'AAA','sort'=>'name')
  * </code>
  *
  * Another Example with parse_option_line:
  * <code>
  *  $conf_options = array(
  *         // option  => array(Type, Default, Conf)
  *        'hierarchy' => array('bool', true),
  *        'num'       => array('interval', null),
  *        'filter'    => array('string', null, 'default'),
  *        'sort'      => array('enum', 'name', array('name', 'reading', 'date')),
  *  );
  *  $optline = 'Hoge/,filter,sort=reading';
  *  $options = PluginSonotsOption::parse_option_line($optline);
  *  var_export($options); // array('Hoge/'=>true,'filter'=>true,'sort'=>'reading')
  *  list($options, $unknowns) = PluginSonotsOption::evaluate_options($options, $conf_options);
  *  var_export($options); // array('hierarchy'=>true,'num'=>null,'filter'=>'default','sort'=>'reading')
  *  var_export($unknowns); // array('Hoge/'=>true)
  * </code>
  *
  * How to Write $conf_options:
  * <pre>
  *   $conf_options is an array of array(Type, Default, Conf)
  *
  *   Role of Type)
  *     Specify one of the following types as a string:
  *     - bool      : boolean true or false
  *     - string    : string
  *     - array     : array
  *     - enum      : take only one element of possible values
  *     - enumarray : take only elements in possible values
  *     - number    : number
  *     - interval  : interval string. @see parse_interval for details
  *     - options   : options (will be recursively evaluated inside)
  *
  *   Role of Default)
  *     It is the default value when the option was not specified by users. 
  *
  *   Role of Conf)
  *     Basically, the conf also means a default value, but it is for
  *     when the option was given but the option argument was not given, i.e., 
  *     the case no "=value" in the option line. Let me call it as default2. 
  *     Concurrently, the conf has a different meaning for following types 
  *     and this was the original main role of conf. 
  *     - enum      : list possible values as an array. As default2, 
  *                   the first element of array is used. 
  *     - enumarray : list possible values as an array. As default2, 
  *                   the entire array is used. 
  *     - options   : $conf_options recursively. As default2, 'options' 
  *                   are recursively configured by $conf_options for this
  *                   options, thus, defaults in $conf_options are used. 
  * </pre>
  *
  * @access public
  * @static
  * @param array $options 
  *    $options[$name] = $value
  * @param array $conf_options
  *    $conf_options[$name] = array(type, default, conf)
  * @return array array($options, $unknowns)
  * - $options[$name] = $evaluated_value
  * - $unknowns[$unknown_name] = $value
  * @uses evaluate_option
  * @uses parse_interval
  * @version $Id: v 1.6 2008-06-12 11:14:46 sonots $
  * @since   v 1.0
  */
 function evaluate_options($options, $conf_options)
 {
     $default = array();
     foreach ($conf_options as $key => $tmp) {
         $default[$key] = isset($conf_options[$key][1]) ? $conf_options[$key][1] : null;
     }
     $options = array_merge($default, $options);
     $unknowns = array();
     foreach ($options as $key => $val) {
         if (isset($conf_options[$key])) {
             $type = isset($conf_options[$key][0]) ? $conf_options[$key][0] : null;
             $conf = isset($conf_options[$key][2]) ? $conf_options[$key][2] : null;
             list($options[$key], $unknowns[$key]) = PluginSonotsOption::evaluate_option($val, $type, $conf);
             if (is_null($unknowns[$key])) {
                 unset($unknowns[$key]);
             }
         } else {
             unset($options[$key]);
             $unknowns[$key] = $val;
         }
     }
     return array($options, $unknowns);
 }