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 |
|
25 |
require_once('PEAR.php'); |
26 |
|
27 |
/** |
28 |
* this class only defines commonly used methods, etc. |
29 |
* it is worthless without being extended |
30 |
* |
31 |
* @package Tree |
32 |
* @access public |
33 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
34 |
* |
35 |
*/ |
36 |
class Tree_Options extends PEAR |
37 |
{ |
38 |
/** |
39 |
* @var array $options you need to overwrite this array and give the keys, that are allowed |
40 |
*/ |
41 |
var $options = array(); |
42 |
|
43 |
var $_forceSetOption = false; |
44 |
|
45 |
/** |
46 |
* this constructor sets the options, since i normally need this and |
47 |
* in case the constructor doesnt need to do anymore i already have it done :-) |
48 |
* |
49 |
* @version 02/01/08 |
50 |
* @access public |
51 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
52 |
* @param array the key-value pairs of the options that shall be set |
53 |
* @param boolean if set to true options are also set |
54 |
* even if no key(s) was/were found in the options property |
55 |
*/ |
56 |
function Tree_Options( $options=array() , $force=false ) |
57 |
{ |
58 |
$this->_forceSetOption = $force; |
59 |
|
60 |
if( is_array($options) && sizeof($options) ) |
61 |
foreach( $options as $key=>$value ) |
62 |
$this->setOption( $key , $value ); |
63 |
} |
64 |
|
65 |
/** |
66 |
* |
67 |
* @access public |
68 |
* @author Stig S. Baaken |
69 |
* @param |
70 |
* @param |
71 |
* @param boolean if set to true options are also set |
72 |
* even if no key(s) was/were found in the options property |
73 |
*/ |
74 |
function setOption( $option , $value , $force=false ) |
75 |
{ |
76 |
if( is_array($value) ) // if the value is an array extract the keys and apply only each value that is set |
77 |
{ // so we dont override existing options inside an array, if an option is an array |
78 |
foreach( $value as $key=>$aValue ) |
79 |
$this->setOption( array($option , $key) , $aValue ); |
80 |
return true; |
81 |
} |
82 |
|
83 |
if( is_array($option) ) |
84 |
{ |
85 |
$mainOption = $option[0]; |
86 |
$options = "['".implode("']['",$option)."']"; |
87 |
$evalCode = "\$this->options".$options." = \$value;"; |
88 |
} |
89 |
else |
90 |
{ |
91 |
$evalCode = "\$this->options[\$option] = \$value;"; |
92 |
$mainOption = $option; |
93 |
} |
94 |
|
95 |
if( $this->_forceSetOption==true || $force==true || isset($this->options[$mainOption]) ) |
96 |
{ |
97 |
eval($evalCode); |
98 |
return true; |
99 |
} |
100 |
return false; |
101 |
} |
102 |
|
103 |
/** |
104 |
* set a number of options which are simply given in an array |
105 |
* |
106 |
* @access public |
107 |
* @author |
108 |
* @param |
109 |
* @param boolean if set to true options are also set |
110 |
* even if no key(s) was/were found in the options property |
111 |
*/ |
112 |
function setOptions( $options , $force=false ) |
113 |
{ |
114 |
if( is_array($options) && sizeof($options) ) |
115 |
{ |
116 |
foreach( $options as $key=>$value ) |
117 |
{ |
118 |
$this->setOption( $key , $value , $force ); |
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
/** |
124 |
* |
125 |
* @access public |
126 |
* @author copied from PEAR: DB/commmon.php |
127 |
* @param boolean true on success |
128 |
*/ |
129 |
function getOption($option) |
130 |
{ |
131 |
if( func_num_args() > 1 && |
132 |
is_array($this->options[$option])) |
133 |
{ |
134 |
$args = func_get_args(); |
135 |
$evalCode = "\$ret = \$this->options['".implode( "']['" , $args )."'];"; |
136 |
eval( $evalCode ); |
137 |
return $ret; |
138 |
} |
139 |
|
140 |
if (isset($this->options[$option])) { |
141 |
return $this->options[$option]; |
142 |
} |
143 |
# return $this->raiseError("unknown option $option"); |
144 |
return false; |
145 |
} |
146 |
|
147 |
/** |
148 |
* returns all the options |
149 |
* |
150 |
* @version 02/05/20 |
151 |
* @access public |
152 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
153 |
* @return string all options as an array |
154 |
*/ |
155 |
function getOptions() |
156 |
{ |
157 |
return $this->options; |
158 |
} |
159 |
} // end of class |
160 |
?> |