2 rcs_id('$Id: Tools.php,v 1.6 2004-04-16 23:30:41 zorloc 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 * Class for Configuration properties
27 * Class provides the functions 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 if (isset($params['validator'])) {
88 $params['validator'] = &Validator::getValidator($params['validator']);
90 return new ConfigValue($params);
94 * Determines if the value is valid.
96 * If the parameter is a valid value for this config value returns
98 * @param mixed $value Value to be checked for validity.
99 * @return boolean True if valid, false else.
101 function valid($value){
102 if ($this->validator->validate($value)) {
105 trigger_error("Value for \'" . $this->name . "\' is invalid.",
111 * Determines the value currently being used.
113 * Just returns the default value.
114 * @return mixed The currently used value (the default).
116 function getStarting(){
117 return $this->defaultValue;
121 * Get the currently selected value.
123 * @return mixed The currently selected value.
125 function getCurrent(){
126 return $this->currentValue;
130 * Set the current value to this.
132 * Checks to see if the parameter is a valid value, if so it
133 * sets the parameter to currentValue.
134 * @param mixed $value The value to set.
136 function setCurrent($value){
137 if ($this->valid($value)) {
138 $this->currentValue = $value;
143 * Get the Name of the Value
144 * @return mixed Name of the value.
151 * Get the default value of the Value
152 * @return mixed Default value of the value.
154 function getDefaultValue(){
155 return $this->defaultValue;
159 * Get the Short Description of the Value
160 * @return mixed Short Description of the value.
162 function getShortDescription(){
163 return $this->description['short'];
167 * Get the Full Description of the Value
168 * @return mixed Full Description of the value.
170 function getFullDescription(){
171 return $this->description['full'];
179 * Abstract base Validator Class
180 * @author Joby Walker<zorloc@imperium.org>
187 * Dummy constructor that does nothing.
189 function Validator(){
194 * Dummy valitate method -- always returns true.
195 * @param mixed $value Value to check.
196 * @return boolean Always returns true.
198 function validate($value){
203 * Get the proper Valitator subclass for the provided parameters
204 * @param array $params Initialization values for Validator.
205 * @return object Validator subclass for use with the parameters.
208 function getValidator($params){
209 extract($params, EXTR_OVERWRITE);
210 $class = 'Validator' . $type;
213 return new $class ($list);
214 } elseif (isset($range)) {
216 return new $class ($range);
217 } elseif (isset($pcre)){
219 return new $class ($pcre);
221 return new $class ();
228 * Validator subclass for use with boolean values
229 * @author Joby Walker<zorloc@imperium.org>
231 class ValidatorBoolean extends Validator {
234 * Checks the parameter to see if it is a boolean, returns true if
236 * @param boolean $boolean Value to check to ensure it is a boolean.
237 * @return boolean True if parameter is boolean.
239 function validate ($boolean){
240 if (is_bool($boolean)) {
248 * Validator subclass for use with integer values with no bounds.
249 * @author Joby Walker<zorloc@imperium.org>
251 class ValidatorInteger extends Validator {
254 * Checks the parameter to ensure that it is an integer.
255 * @param integer $integer Value to check.
256 * @return boolean True if parameter is an integer, false else.
258 function validate ($integer){
259 if (is_int($integer)) {
267 * Validator subclass for use with integer values to be bound within a range.
268 * @author Joby Walker<zorloc@imperium.org>
270 class ValidatorIntegerRange extends ValidatorInteger {
273 * Minimum valid value
280 * Maximum valid value
289 * Sets the minimum and maximum values from the parameter array.
290 * @param array $range Minimum and maximum valid values.
292 function ValidatorIntegerRange($range){
293 $this->minimum = $range['minimum'];
294 $this->maximum = $range['maximum'];
299 * Checks to ensure that the parameter is an integer and within the desired
301 * @param integer $integer Value to check.
302 * @return boolean True if the parameter is an integer and within the
303 * desired range, false else.
305 function validate ($integer){
306 if (is_int($integer)) {
307 if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
317 * Validator subclass for use with integer values to be selected from a list.
318 * @author Joby Walker<zorloc@imperium.org>
320 class ValidatorIntegerList extends ValidatorInteger {
323 * Array of potential valid values
332 * Saves parameter as the instance variable $intList.
333 * @param array List of valid values.
335 function ValidatorIntegerList($intList){
336 $this->intList = $intList;
341 * Checks the parameter to ensure that it is an integer, and
342 * within the defined list.
343 * @param integer $integer Value to check.
344 * @return boolean True if parameter is an integer and in list, false else.
346 function validate ($integer){
347 if (is_int($integer)) {
348 if (in_array($integer, $this->intList, true)) {
358 * Validator subclass for string values with no bounds
359 * @author Joby Walker<zorloc@imperium.org>
361 class ValidatorString extends Validator {
364 * Checks the parameter to ensure that is is a string.
365 * @param string $string Value to check.
366 * @return boolean True if parameter is a string, false else.
368 function validate ($string){
369 if (is_string($string)) {
378 * Validator subclass for string values to be selected from a list.
379 * @author Joby Walker<zorloc@imperium.org>
381 class ValidatorStringList extends ValidatorString {
384 * Array of potential valid values
393 * Saves parameter as the instance variable $stringList.
394 * @param array List of valid values.
396 function ValidatorStringList($stringList){
397 $this->stringList = $stringList;
402 * Checks the parameter to ensure that is is a string, and within
404 * @param string $string Value to check.
405 * @return boolean True if parameter is a string and in the list, false else.
407 function validate($string){
408 if (is_string($string)) {
409 if (in_array($string, $this->stringList, true)) {
419 * Validator subclass for string values that must meet a PCRE.
420 * @author Joby Walker<zorloc@imperium.org>
422 class ValidatorStringPcre extends ValidatorString {
425 * PCRE to validate value
434 * Saves parameter as the instance variable $pattern.
435 * @param array PCRE pattern to determin validity.
437 function ValidatorStringPcre($pattern){
438 $this->pattern = $pattern;
443 * Checks the parameter to ensure that is is a string, and matches the
445 * @param string $string Value to check.
446 * @return boolean True if parameter is a string and matches the pattern,
449 function validate ($string){
450 if (is_string($string)) {
451 if (preg_match($this->pattern, $string)) {
460 * Validator subclass for constant values.
461 * @author Joby Walker<zorloc@imperium.org>
463 class ValidatorConstant extends Validator {
466 * Checks the parameter to ensure that is is a constant.
467 * @param string $constant Value to check.
468 * @return boolean True if parameter is a constant, false else.
470 function validate ($constant){
471 if (defined($constant)) {
479 * Validator subclass for constant values to be selected from a list.
480 * @author Joby Walker<zorloc@imperium.org>
482 class ValidatorConstantList extends Validator {
485 * Array of potential valid values
494 * Saves parameter as the instance variable $constantList.
495 * @param array List of valid values.
497 function ValidatorConstantList($constantList){
498 $this->constantList = $constantList;
503 * Checks the parameter to ensure that is is a constant, and within
505 * @param string $constant Value to check.
506 * @return boolean True if parameter is a constant and in the list, false else.
508 function validate ($constant){
509 if (defined($constant)) {
510 if (in_array($constant, $this->constantList, true)) {
519 * Validator subclass for an array.
520 * @author Joby Walker<zorloc@imperium.org>
522 class ValidatorArray extends Validator {
525 * Checks to ensure that the parameter is an array then passes the
526 * array on to validMembers() to ensure that each member of the
528 * @param array $array Value to check.
529 * @return boolean True if the value is and array and members are valid, false else.
531 function validate($array){
532 if(is_array($array)){
533 return $this->validMembers($array);
539 * Checks to ensure that the members of the array are valid. Always true here.
540 * @param array $array Array of members to check
541 * @return boolean Always true since there are no restrictions on the members.
543 function validMembers($array){
549 * Validator subclass for an array of strings.
550 * @author Joby Walker<zorloc@imperium.org>
552 class ValidatorArrayString extends Validator {
555 * Checks to ensure that the members of the array are valid strings.
556 * @param array $array Array of members to check
557 * @return boolean True if the members are valid strings, false else.
559 function validMembers($array){
560 foreach ($array as $member){
561 if (!is_string($member)) {
570 * Validator subclass for an array of strings that must be in a list of
572 * @author Joby Walker<zorloc@imperium.org>
574 class ValidatorArrayStringList extends Validator {
577 * Array of potential valid values
586 * Saves parameter as the instance variable $stringList.
587 * @param array List of valid values.
589 function ValidatorArrayStringList($stringList){
590 $this->stringList = $stringList;
595 * Checks to ensure that the members of the array are valid strings and
596 * within the defined list.
597 * @param array $array Array of members to check
598 * @return boolean True if the members are valid strings are in the defined list,
601 function validMembers($array){
602 foreach ($array as $member){
603 if(!in_array($member, $stringList, true)){
614 //$Log: not supported by cvs2svn $
615 //Revision 1.5 2003/12/07 19:25:41 carstenklapp
616 //Code Housecleaning: fixed syntax errors. (php -l *.php)
618 //Revision 1.3 2003/01/28 18:53:25 zorloc
619 //Added some more Validator subclasses to handle arrays of for which the
620 //validation criteria should be the same for all members.
622 //Revision 1.2 2003/01/28 06:31:00 zorloc
623 //Mostly done but ConfigArray will probably need some more helper methods.
625 //Revision 1.1 2003/01/23 00:32:04 zorloc
626 //Initial work for classes to hold configuration constants/variables. Base
627 //ConfigValue class and subclasses for constants and variables.
634 // c-hanging-comment-ender-p: nil
635 // indent-tabs-mode: nil