| 1 |
<?php |
| 2 |
# i think this class should go somewhere in a common PEAR-place, |
| 3 |
# because a lot of classes use options, at least PEAR::DB does |
| 4 |
# but since it is not very fancy to crowd the PEAR-namespace too much i dont know where to put it yet :-( |
| 5 |
|
| 6 |
// |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | PHP Version 4 | |
| 9 |
// +----------------------------------------------------------------------+ |
| 10 |
// | Copyright (c) 1997-2003 The PHP Group | |
| 11 |
// +----------------------------------------------------------------------+ |
| 12 |
// | This source file is subject to version 2.02 of the PHP license, | |
| 13 |
// | that is bundled with this package in the file LICENSE, and is | |
| 14 |
// | available at through the world-wide-web at | |
| 15 |
// | http://www.php.net/license/2_02.txt. | |
| 16 |
// | If you did not receive a copy of the PHP license and are unable to | |
| 17 |
// | obtain it through the world-wide-web, please send a note to | |
| 18 |
// | license@php.net so we can mail you a copy immediately. | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
// |
| 23 |
// Id: Options.php,v 1.4 2003/01/04 11:56:27 mj Exp |
| 24 |
// $Id: Options.php,v 1.4 2003/01/04 11:56:27 mj Exp $ |
| 25 |
|
| 26 |
require_once('PEAR.php'); |
| 27 |
|
| 28 |
/** |
| 29 |
* this class only defines commonly used methods, etc. |
| 30 |
* it is worthless without being extended |
| 31 |
* |
| 32 |
* @package Tree |
| 33 |
* @access public |
| 34 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 35 |
* |
| 36 |
*/ |
| 37 |
class Tree_Options extends PEAR |
| 38 |
{ |
| 39 |
/** |
| 40 |
* @var array $options you need to overwrite this array and give the keys, that are allowed |
| 41 |
*/ |
| 42 |
var $options = array(); |
| 43 |
|
| 44 |
var $_forceSetOption = false; |
| 45 |
|
| 46 |
/** |
| 47 |
* this constructor sets the options, since i normally need this and |
| 48 |
* in case the constructor doesnt need to do anymore i already have it done :-) |
| 49 |
* |
| 50 |
* @version 02/01/08 |
| 51 |
* @access public |
| 52 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 53 |
* @param array the key-value pairs of the options that shall be set |
| 54 |
* @param boolean if set to true options are also set |
| 55 |
* even if no key(s) was/were found in the options property |
| 56 |
*/ |
| 57 |
function Tree_Options( $options=array() , $force=false ) |
| 58 |
{ |
| 59 |
$this->_forceSetOption = $force; |
| 60 |
|
| 61 |
if( is_array($options) && sizeof($options) ) |
| 62 |
foreach( $options as $key=>$value ) |
| 63 |
$this->setOption( $key , $value ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* |
| 68 |
* @access public |
| 69 |
* @author Stig S. Baaken |
| 70 |
* @param |
| 71 |
* @param |
| 72 |
* @param boolean if set to true options are also set |
| 73 |
* even if no key(s) was/were found in the options property |
| 74 |
*/ |
| 75 |
function setOption( $option , $value , $force=false ) |
| 76 |
{ |
| 77 |
if( is_array($value) ) // if the value is an array extract the keys and apply only each value that is set |
| 78 |
{ // so we dont override existing options inside an array, if an option is an array |
| 79 |
foreach( $value as $key=>$aValue ) |
| 80 |
$this->setOption( array($option , $key) , $aValue ); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
if( is_array($option) ) |
| 85 |
{ |
| 86 |
$mainOption = $option[0]; |
| 87 |
$options = "['".implode("']['",$option)."']"; |
| 88 |
$evalCode = "\$this->options".$options." = \$value;"; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
$evalCode = "\$this->options[\$option] = \$value;"; |
| 93 |
$mainOption = $option; |
| 94 |
} |
| 95 |
|
| 96 |
if( $this->_forceSetOption==true || $force==true || isset($this->options[$mainOption]) ) |
| 97 |
{ |
| 98 |
eval($evalCode); |
| 99 |
return true; |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* set a number of options which are simply given in an array |
| 106 |
* |
| 107 |
* @access public |
| 108 |
* @author |
| 109 |
* @param |
| 110 |
* @param boolean if set to true options are also set |
| 111 |
* even if no key(s) was/were found in the options property |
| 112 |
*/ |
| 113 |
function setOptions( $options , $force=false ) |
| 114 |
{ |
| 115 |
if( is_array($options) && sizeof($options) ) |
| 116 |
{ |
| 117 |
foreach( $options as $key=>$value ) |
| 118 |
{ |
| 119 |
$this->setOption( $key , $value , $force ); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* |
| 126 |
* @access public |
| 127 |
* @author copied from PEAR: DB/commmon.php |
| 128 |
* @param boolean true on success |
| 129 |
*/ |
| 130 |
function getOption($option) |
| 131 |
{ |
| 132 |
if( func_num_args() > 1 && |
| 133 |
is_array($this->options[$option])) |
| 134 |
{ |
| 135 |
$args = func_get_args(); |
| 136 |
$evalCode = "\$ret = \$this->options['".implode( "']['" , $args )."'];"; |
| 137 |
eval( $evalCode ); |
| 138 |
return $ret; |
| 139 |
} |
| 140 |
|
| 141 |
if (isset($this->options[$option])) { |
| 142 |
return $this->options[$option]; |
| 143 |
} |
| 144 |
# return $this->raiseError("unknown option $option"); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* returns all the options |
| 150 |
* |
| 151 |
* @version 02/05/20 |
| 152 |
* @access public |
| 153 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 154 |
* @return string all options as an array |
| 155 |
*/ |
| 156 |
function getOptions() |
| 157 |
{ |
| 158 |
return $this->options; |
| 159 |
} |
| 160 |
} // end of class |
| 161 |
?> |