2 rcs_id('$Id: Tools.php,v 1.5 2003-12-07 19:25:41 carstenklapp Exp $');
4 Copyright 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Base class for Configuration properties
27 * Class provides the base functions for subclasses to get and set
28 * valid values for configuration properties.
29 * @author Joby Walker<zorloc@imperium.org>
52 * Array with a short and full description.
58 * Validator object to validate a new value.
67 * Initializes instance variables from parameter array.
68 * @param array $params Array with properties of the config value.
70 function ConfigValue($params){
71 $this->name = $params['name'];
72 $this->section = $params['section'];
73 $this->defaultValue = $params['defaultValue'];
74 $this->description = $params['description'];
75 $this->validator = &$params['validator'];
76 $this->currentValue = $this->getStarting();
80 * Static method to get the proper subclass.
82 * @param array $params Config Values properties.
83 * @return object A subclass of ConfigValue.
86 function getConfig($params){
87 $class = 'Config' . $params['type'];
88 if (isset($params['validator'])) {
89 $params['validator'] = &Validator::getValidator($params['validator']);
91 return new $class ($params);
95 * Determines if the value is valid.
97 * If the parameter is a valid value for this config value returns
99 * @param mixed $value Value to be checked for validity.
100 * @return boolean True if valid, false else.
102 function valid($value){
103 if ($this->validator->validate($value)) {
106 trigger_error("Value for \'" . $this->name . "\' is invalid.",
112 * Determines the value currently being used.
114 * Just returns the default value.
115 * @return mixed The currently used value (the default).
117 function getStarting(){
118 return $this->defaultValue;
122 * Get the currently selected value.
124 * @return mixed The currently selected value.
126 function getCurrent(){
127 return $this->currentValue;
131 * Set the current value to this.
133 * Checks to see if the parameter is a valid value, if so it
134 * sets the parameter to currentValue.
135 * @param mixed $value The value to set.
137 function setCurrent($value){
138 if ($this->valid($value)) {
139 $this->currentValue = $value;
144 * Get the Name of the Value
145 * @return mixed Name of the value.
152 * Get the default value of the Value
153 * @return mixed Default value of the value.
155 function getDefaultValue(){
156 return $this->defaultValue;
160 * Get the Short Description of the Value
161 * @return mixed Short Description of the value.
163 function getShortDescription(){
164 return $this->description['short'];
168 * Get the Full Description of the Value
169 * @return mixed Full Description of the value.
171 function getFullDescription(){
172 return $this->description['full'];
177 * Configuration class for Constants
179 * Subclass of ConfigValue which overrides the getStarting method
180 * to provide the true value currently used.
181 * @author Joby Walker<zorloc@imperium.org>
183 class ConfigConstant extends ConfigValue {
186 * Determines the currently used value of this constant
187 * @return mixed The value currently used.
189 function getStarting(){
190 if (defined($this->name)) {
191 $starting = constant($this->name);
192 if ($this->valid($starting)) {
196 return $this->defaultValue;
201 * Configuration class for Variables
203 * Subclass of ConfigValue which overrides the getStarting method
204 * to provide the true value currently used
205 * @author Joby Walker<zorloc@imperium.org>
207 class ConfigVariable extends ConfigValue {
210 * Determines the currently used value of this variable
211 * @return mixed The value currently used.
213 function getStarting(){
214 if (isset(${$this->name})) {
215 $starting = ${$this->name};
216 if ($this->valid($starting)) {
220 return $this->defaultValue;
225 class ConfigArray extends ConfigVariable {
228 function ConfigArray($params){
229 $this->name = $params['name'];
230 $this->description = $params['description'];
232 foreach ($params['defaultValue'] as $config){
233 $this->value = &ConfigValue::getConfig($config);
243 * Abstract base Validator Class
244 * @author Joby Walker<zorloc@imperium.org>
251 * Dummy constructor that does nothing.
253 function Validator(){
258 * Dummy valitate method -- always returns true.
259 * @param mixed $value Value to check.
260 * @return boolean Always returns true.
262 function validate($value){
267 * Get the proper Valitator subclass for the provided parameters
268 * @param array $params Initialization values for Validator.
269 * @return object Validator subclass for use with the parameters.
272 function getValidator($params){
273 extract($params, EXTR_OVERWRITE);
274 $class = 'Validator' . $type;
277 return new $class ($list);
278 } elseif (isset($range)) {
280 return new $class ($range);
281 } elseif (isset($pcre)){
283 return new $class ($pcre);
285 return new $class ();
292 * Validator subclass for use with boolean values
293 * @author Joby Walker<zorloc@imperium.org>
295 class ValidatorBoolean extends Validator {
298 * Checks the parameter to see if it is a boolean, returns true if
300 * @param boolean $boolean Value to check to ensure it is a boolean.
301 * @return boolean True if parameter is boolean.
303 function validate ($boolean){
304 if (is_bool($boolean)) {
312 * Validator subclass for use with integer values with no bounds.
313 * @author Joby Walker<zorloc@imperium.org>
315 class ValidatorInteger extends Validator {
318 * Checks the parameter to ensure that it is an integer.
319 * @param integer $integer Value to check.
320 * @return boolean True if parameter is an integer, false else.
322 function validate ($integer){
323 if (is_int($integer)) {
331 * Validator subclass for use with integer values to be bound within a range.
332 * @author Joby Walker<zorloc@imperium.org>
334 class ValidatorIntegerRange extends ValidatorInteger {
337 * Minimum valid value
344 * Maximum valid value
353 * Sets the minimum and maximum values from the parameter array.
354 * @param array $range Minimum and maximum valid values.
356 function ValidatorIntegerRange($range){
357 $this->minimum = $range['minimum'];
358 $this->maximum = $range['maximum'];
363 * Checks to ensure that the parameter is an integer and within the desired
365 * @param integer $integer Value to check.
366 * @return boolean True if the parameter is an integer and within the
367 * desired range, false else.
369 function validate ($integer){
370 if (is_int($integer)) {
371 if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
381 * Validator subclass for use with integer values to be selected from a list.
382 * @author Joby Walker<zorloc@imperium.org>
384 class ValidatorIntegerList extends ValidatorInteger {
387 * Array of potential valid values
396 * Saves parameter as the instance variable $intList.
397 * @param array List of valid values.
399 function ValidatorIntegerList($intList){
400 $this->intList = $intList;
405 * Checks the parameter to ensure that it is an integer, and
406 * within the defined list.
407 * @param integer $integer Value to check.
408 * @return boolean True if parameter is an integer and in list, false else.
410 function validate ($integer){
411 if (is_int($integer)) {
412 if (in_array($integer, $this->intList, true)) {
422 * Validator subclass for string values with no bounds
423 * @author Joby Walker<zorloc@imperium.org>
425 class ValidatorString extends Validator {
428 * Checks the parameter to ensure that is is a string.
429 * @param string $string Value to check.
430 * @return boolean True if parameter is a string, false else.
432 function validate ($string){
433 if (is_string($string)) {
442 * Validator subclass for string values to be selected from a list.
443 * @author Joby Walker<zorloc@imperium.org>
445 class ValidatorStringList extends ValidatorString {
448 * Array of potential valid values
457 * Saves parameter as the instance variable $stringList.
458 * @param array List of valid values.
460 function ValidatorStringList($stringList){
461 $this->stringList = $stringList;
466 * Checks the parameter to ensure that is is a string, and within
468 * @param string $string Value to check.
469 * @return boolean True if parameter is a string and in the list, false else.
471 function validate($string){
472 if (is_string($string)) {
473 if (in_array($string, $this->stringList, true)) {
483 * Validator subclass for string values that must meet a PCRE.
484 * @author Joby Walker<zorloc@imperium.org>
486 class ValidatorStringPcre extends ValidatorString {
489 * PCRE to validate value
498 * Saves parameter as the instance variable $pattern.
499 * @param array PCRE pattern to determin validity.
501 function ValidatorStringPcre($pattern){
502 $this->pattern = $pattern;
507 * Checks the parameter to ensure that is is a string, and matches the
509 * @param string $string Value to check.
510 * @return boolean True if parameter is a string and matches the pattern,
513 function validate ($string){
514 if (is_string($string)) {
515 if (preg_match($this->pattern, $string)) {
524 * Validator subclass for constant values.
525 * @author Joby Walker<zorloc@imperium.org>
527 class ValidatorConstant extends Validator {
530 * Checks the parameter to ensure that is is a constant.
531 * @param string $constant Value to check.
532 * @return boolean True if parameter is a constant, false else.
534 function validate ($constant){
535 if (defined($constant)) {
543 * Validator subclass for constant values to be selected from a list.
544 * @author Joby Walker<zorloc@imperium.org>
546 class ValidatorConstantList extends Validator {
549 * Array of potential valid values
558 * Saves parameter as the instance variable $constantList.
559 * @param array List of valid values.
561 function ValidatorConstantList($constantList){
562 $this->constantList = $constantList;
567 * Checks the parameter to ensure that is is a constant, and within
569 * @param string $constant Value to check.
570 * @return boolean True if parameter is a constant and in the list, false else.
572 function validate ($constant){
573 if (defined($constant)) {
574 if (in_array($constant, $this->constantList, true)) {
583 * Validator subclass for an array.
584 * @author Joby Walker<zorloc@imperium.org>
586 class ValidatorArray extends Validator {
589 * Checks to ensure that the parameter is an array then passes the
590 * array on to validMembers() to ensure that each member of the
592 * @param array $array Value to check.
593 * @return boolean True if the value is and array and members are valid, false else.
595 function validate($array){
596 if(is_array($array)){
597 return $this->validMembers($array);
603 * Checks to ensure that the members of the array are valid. Always true here.
604 * @param array $array Array of members to check
605 * @return boolean Always true since there are no restrictions on the members.
607 function validMembers($array){
613 * Validator subclass for an array of strings.
614 * @author Joby Walker<zorloc@imperium.org>
616 class ValidatorArrayString extends Validator {
619 * Checks to ensure that the members of the array are valid strings.
620 * @param array $array Array of members to check
621 * @return boolean True if the members are valid strings, false else.
623 function validMembers($array){
624 foreach ($array as $member){
625 if (!is_string($member)) {
634 * Validator subclass for an array of strings that must be in a list of
636 * @author Joby Walker<zorloc@imperium.org>
638 class ValidatorArrayStringList extends Validator {
641 * Array of potential valid values
650 * Saves parameter as the instance variable $stringList.
651 * @param array List of valid values.
653 function ValidatorArrayStringList($stringList){
654 $this->stringList = $stringList;
659 * Checks to ensure that the members of the array are valid strings and
660 * within the defined list.
661 * @param array $array Array of members to check
662 * @return boolean True if the members are valid strings are in the defined list,
665 function validMembers($array){
666 foreach ($array as $member){
667 if(!in_array($member, $stringList, true)){
678 //$Log: not supported by cvs2svn $
679 //Revision 1.3 2003/01/28 18:53:25 zorloc
680 //Added some more Validator subclasses to handle arrays of for which the
681 //validation criteria should be the same for all members.
683 //Revision 1.2 2003/01/28 06:31:00 zorloc
684 //Mostly done but ConfigArray will probably need some more helper methods.
686 //Revision 1.1 2003/01/23 00:32:04 zorloc
687 //Initial work for classes to hold configuration constants/variables. Base
688 //ConfigValue class and subclasses for constants and variables.
695 // c-hanging-comment-ender-p: nil
696 // indent-tabs-mode: nil