Options.php 5.6 KB

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