Class: PluginSonotsOption
Source Location: /sonots/option.class.php
Class PluginSonotsOption
Class Overview
|
Advanced Option Parser for PukiWiki Plugin
Example1) function plugin_hoge_convert()
{
$conf_options = array(
'num' => array('number', 100),
'prefix' => array('string', 'Hoge/'),
);
$line = csv_implode(',', $args);
list($options, $unknowns)
}
Example2) function plugin_hoge_inline()
{
$line = csv_implode(',', $args);
// no $conf_options is also useful
}
Example3) function plugin_hoge_action()
{
global $vars;
$conf_options = array(
'num' => array('number', 100),
'prefix' => array('string', 'Hoge/'),
);
$options = $vars;
list($options, $unknowns)
}
Located in /sonots/option.class.php [line 56]
Author(s):
Information Tags:
| Version: | $Id: option.class.php,v 1.10 2008-07-30 11:14:46 sonots $ |
| License: | GPL v2 |
|
Methods
|
Method Summary
| static
array
|
conv_interval() |
Convert ($offset, $length) interval form to ($start, $end) interval form. |
Methods
static array boolean_to_numeric(
array
$options
)
|
|
Reverse numeric_to_boolean
Parameters:
API Tags:
Information Tags:
| Version: | $Id: v 1.0 2008-06-07 11:14:46 sonots $ |
| Since: | v 1.4 |
static array conv_interval(
array
$interval, [array
$entire = array(1, PHP_INT_MAX)]
)
|
|
Convert ($offset, $length) interval form to ($start, $end) interval form.
Example)
array(0, 5) to array(1, 5)
array(1, null) to array(2, 10)
array(3, 1) to array(4, 4)
array(-5, null) to array(6, 10)
array(0, -4) to array(1, 6)
Parameters:
|
array |
$interval: |
array(int $offset, int $length) |
|
array |
$entire: |
array(int $min, int $max) |
API Tags:
Information Tags:
| Version: | $Id: v 1.1 2008-07-17 11:14:46 sonots $ |
| Since: | v 1.0 |
static array evaluate_option(
mixed
$val, string
$type, [mixed
$conf = null]
)
|
|
Evaluate an option
Lists of Supported Types) - 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
Parameters:
|
mixed |
$val: |
option value. |
|
string |
$type: |
option type |
|
mixed |
$conf: |
config. See evaluate_options. |
API Tags:
Information Tags:
| Version: | $Id: v 1.7 2008-07-30 11:14:46 sonots $ |
| Since: | v 1.0 |
static array evaluate_options(
array
$options, array
$conf_options
)
|
|
Evaluate options
An Example: $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');
var_export($options); // array('hierarchy'=>true,'num'=>null,'filter'=>'AAA','sort'=>'name')
Another Example with parse_option_line: $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';
var_export($options); // array('Hoge/'=>true,'filter'=>true,'sort'=>'reading')
var_export($options); // array('hierarchy'=>true,'num'=>null,'filter'=>'default','sort'=>'reading')
How to Write $conf_options: $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.
Parameters:
|
array |
$options: |
$options[$name] = $value |
|
array |
$conf_options: |
$conf_options[$name] = array(type, default, conf) |
API Tags:
Information Tags:
| Version: | $Id: v 1.6 2008-06-12 11:14:46 sonots $ |
| Since: | v 1.0 |
static string glue_option_line(
array
$options, [boolean
$encode = true]
)
|
|
Recover option line.
FYI) Encoding is required especially when key/val values include delimiter characters, '=', ',', '(', and ')'. Usually use true.
Parameters:
|
array |
$options: |
|
|
boolean |
$encode: |
perform encode key/val |
API Tags:
Information Tags:
| Version: | $Id: v 1.0 2008-06-07 11:14:46 sonots $ |
| Since: | v 1.4 |
static string glue_uri_option_line(
array
$options
)
|
|
Recover option line, but into GET argument style such as
opt1=val1&opt2=val2&opt3=(a&b) Note that this is not inverse of parse_uri_option_line exactly because parse_uri_option_line assumes input variables are already rawurldecoded, but this performs rawurlencode.
Parameters:
API Tags:
Information Tags:
| Version: | $Id: v 1.0 2008-07-16 11:14:46 sonots $ |
| Since: | v 1.8 |
static array numeric_to_boolean(
array
$array
)
|
|
Convert numeric key element to boolean value element.
By string_to_array, $string = 'foo,bar' => array(0=>'foo',1=>'bar').
Want options as, $string = 'foo,bar' => array('foo'=>true,'bar'=>true).
Perform this conversion.
Parameters:
API Tags:
Information Tags:
| Version: | $Id: v 1.0 2008-06-07 11:14:46 sonots $ |
| Since: | v 1.4 |
static mixed parse_interval(
string
$interval, [int
$start = 1]
)
|
|
Parse an interval num string
Example) 1:5 means 1st to 5th returns array(0, 5)
2:3 means 2nd to 3rd returns array(1, 2)
2: means 2nd to end returns array(1, null)
:3 means 1st to 2rd returns array(0, 3)
4 means 4th returns array(3, 1)
- 5: means last 5th to end returns array(- 5, null)
:-5 means 1st to last 5th returns array(0, -4)
1+2 means 1st to 3rd returns array(0, 3)
Parameters:
|
string |
$interval: |
|
|
int |
$start: |
0|1. Tell where is the offset 0. Default is 1 as example. |
API Tags:
Information Tags:
| Version: | $Id: v 1.1 2008-07-17 11:14:46 sonots $ |
| Since: | v 1.0 |
static array parse_option_line(
string
$line, [boolean
$trim = false], [boolean
$decode = true]
)
|
|
Parse option line as followings:
Rule) , is used to separate options.
= is used to separate option key (name) and option value.
() is used if element is an array.
Example) $line = 'prefix=Hoge/,num=1:5,contents=(num=1,depth=1),hoge';
// array('prefix'=>'Hoge/','num'=>'1:5',
// 'contents'=>array('num'=>'1','depth'=>'1'),'hoge'=>true);
// () becomes an array()
// Option which does not have "=[value]" is set to TRUE anyway.
parse_option_line is upper version of the simple sonots::parse_options($args). parse_options does not support array arguments, but parse_option_line does. Except array arguments, both should be able to generate the same results. FYI) Decoding is required especially when key/val values include delimiter characters, '=', ',', '(', and ')'. Usually use true.
Parameters:
|
string |
$line: |
|
|
boolean |
$trim: |
trim option key/val |
|
boolean |
$decode: |
perform decode key/val |
API Tags:
Information Tags:
| Version: | $Id: v 1.5 2008-06-07 11:14:46 sonots $ |
| Since: | v 1.0 |
static array parse_uri_option_line(
array
$vars
)
|
|
Parse option arguments given by GET.
1) Assume that the GET argument line was as opt1=val1&opt2=val2&opt3=(a&b) Thus, now you should have in $vars array('opt1'=>'val1','opt2'=>'val2','opt3'=>'(a','b)'=>'') or 2) Assume that the GET argument line was as opt1=val1&opt2=val2&opt3=(a,b) Thus, now you should have in $vars array('opt1'=>'val1','opt2'=>'val2','opt3'=>'(a,b)')
Parameters:
API Tags:
Information Tags:
| Version: | $Id: v 1.0 2008-07-16 11:14:46 sonots $ |
| Since: | v 1.8 |
|
|