]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - config/Tools.php
Code Housecleaning: adjust line-endings to unix style.
[SourceForge/phpwiki.git] / config / Tools.php
1 <?php
2 rcs_id('$Id: Tools.php,v 1.4 2003-12-07 19:23:48 carstenklapp Exp $')
3 /*
4  Copyright 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23
24 /**
25  * Base class for Configuration properties
26  * 
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>
30  */
31 class ConfigValue {
32     
33     /** 
34     * Name of the Value.
35     * @var string
36     * @access protected
37     */
38     var $name;
39     /** 
40     * The current value.
41     * @var mixed
42     * @access protected
43     */
44     var $currentValue;
45     /** 
46     * The default value.
47     * @var mixed
48     * @access protected
49     */
50     var $defaultValue;
51     /** 
52     * Array with a short and full description.
53     * @var array
54     * @access protected
55     */
56     var $description;
57     /** 
58     * Validator object to validate a new value.
59     * @var object
60     * @access protected
61     */
62     var $validator;
63     
64     /**
65     * Constructor
66     * 
67     * Initializes instance variables from parameter array.
68     * @param array $params Array with properties of the config value.
69     */
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();
77     }
78     
79     /**
80     * Static method to get the proper subclass.
81     * 
82     * @param array $params Config Values properties.
83     * @return object A subclass of ConfigValue.
84     * @static
85     */
86     function getConfig($params){
87         $class = 'Config' . $params['type'];
88         if (isset($params['validator'])) {
89             $params['validator'] = &Validator::getValidator($params['validator']);
90         }
91         return &new $class ($params);
92     }
93
94     /**
95     * Determines if the value is valid.
96     * 
97     * If the parameter is a valid value for this config value returns
98     * true, false else.
99     * @param mixed $value Value to be checked for validity.
100     * @return boolean True if valid, false else.
101     */
102     function valid($value){
103         if ($this->validator->validate($value)) {
104             return true;
105         }
106         trigger_error("Value for \'" . $this->name . "\' is invalid.",
107                       E_USER_WARNING);
108         return false;
109     }
110
111     /**
112     * Determines the value currently being used.
113     * 
114     * Just returns the default value.
115     * @return mixed The currently used value (the default).
116     */
117     function getStarting(){
118         return $this->defaultValue;
119     }
120     
121     /**
122     * Get the currently selected value.
123     * 
124     * @return mixed The currently selected value.
125     */
126     function getCurrent(){
127         return $this->currentValue;
128     }
129
130     /**
131     * Set the current value to this.
132     * 
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.
136     */    
137     function setCurrent($value){
138         if ($this->valid($value)) {
139             $this->currentValue = $value;
140         }
141     }
142     
143     /**
144     * Get the Name of the Value
145     * @return mixed Name of the value.
146     */
147     function getName(){
148         return $this->name;
149     }
150     
151     /**
152     * Get the default value of the Value
153     * @return mixed Default value of the value.
154     */
155     function getDefaultValue(){
156         return $this->defaultValue;
157     }
158     
159     /**
160     * Get the Short Description of the Value
161     * @return mixed Short Description of the value.
162     */
163     function getShortDescription(){
164         return $this->description['short'];
165     }
166
167     /**
168     * Get the Full Description of the Value
169     * @return mixed Full Description of the value.
170     */
171     function getFullDescription(){
172         return $this->description['full'];
173     }
174 }
175
176 /**
177 * Configuration class for Constants
178
179 * Subclass of ConfigValue which overrides the getStarting method 
180 * to provide the true value currently used.
181 * @author Joby Walker<zorloc@imperium.org>
182 */
183 class ConfigConstant extends ConfigValue {
184
185     /**
186     * Determines the currently used value of this constant
187     * @return mixed The value currently used.
188     */
189     function getStarting(){
190         if (defined($this->name)) {
191             $starting = constant($this->name);
192             if ($this->valid($starting)) {
193                 return $starting;
194             }
195         }
196         return $this->defaultValue;
197     }
198 }
199
200 /**
201 * Configuration class for Variables
202
203 * Subclass of ConfigValue which overrides the getStarting method 
204 * to provide the true value currently used
205 * @author Joby Walker<zorloc@imperium.org>
206 */
207 class ConfigVariable extends ConfigValue {
208
209     /**
210     * Determines the currently used value of this variable
211     * @return mixed The value currently used.
212     */
213     function getStarting(){
214         if (isset(${$this->name})) {
215             $starting = ${$this->name};
216             if ($this->valid($starting)) {
217                 return $starting;
218             }
219         }
220         return $this->defaultValue;
221     }
222 }
223
224
225 class ConfigArray extends ConfigVariable {
226
227
228     function ConfigArray($params){
229         $this->name = $params['name'];
230         $this->description = $params['description'];
231         $number = 0;
232         foreach ($params['defaultValue'] as $config){
233             $this->value = &ConfigValue::getConfig($config);
234             $number++;
235         }
236         return;
237     }
238 }
239
240
241
242 /**
243 * Abstract base Validator Class
244 * @author Joby Walker<zorloc@imperium.org>
245 */
246 class Validator {
247
248     /**
249     * Constructor
250     * 
251     * Dummy constructor that does nothing.
252     */
253     function Validator(){
254         return;
255     }
256
257     /**
258     * Dummy valitate method -- always returns true.
259     * @param mixed $value Value to check.
260     * @return boolean Always returns true.
261     */
262     function validate($value){
263         return true;
264     }
265     
266     /**
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.
270     * @static
271     */
272     function getValidator($params){
273         extract($params, EXTR_OVERWRITE);
274         $class = 'Validator' . $type;
275         if (isset($list)){
276             $class .= 'List';
277             return &new $class ($list);
278         } elseif (isset($range)) {
279             $class .= 'Range';
280             return &new $class ($range);
281         } elseif (isset($pcre)){
282             $class .= 'Pcre';
283             return &new $class ($pcre);
284         }
285         return &new $class ();
286     
287     }
288
289 }
290
291 /**
292 * Validator subclass for use with boolean values
293 * @author Joby Walker<zorloc@imperium.org>
294 */
295 class ValidatorBoolean extends Validator {
296
297     /**
298     * Checks the parameter to see if it is a boolean, returns true if
299     * it is, else false.
300     * @param boolean $boolean Value to check to ensure it is a boolean.
301     * @return boolean True if parameter is boolean.
302     */
303     function validate ($boolean){
304         if (is_bool($boolean)) {
305             return true;
306         }
307         return false;
308     }
309 }
310
311 /**
312 * Validator subclass for use with integer values with no bounds.
313 * @author Joby Walker<zorloc@imperium.org>
314 */
315 class ValidatorInteger extends Validator {
316
317     /**
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.
321     */
322     function validate ($integer){
323         if (is_int($integer)) {
324             return true;
325         }
326         return false;
327     }
328 }
329
330 /**
331 * Validator subclass for use with integer values to be bound within a range.
332 * @author Joby Walker<zorloc@imperium.org>
333 */
334 class ValidatorIntegerRange extends ValidatorInteger {
335
336     /** 
337     * Minimum valid value
338     * @var integer
339     * @access protected
340     */
341     var $minimum;
342     
343     /** 
344     * Maximum valid value
345     * @var integer
346     * @access protected
347     */
348     var $maximum;
349
350     /**
351     * Constructor
352     * 
353     * Sets the minimum and maximum values from the parameter array.
354     * @param array $range Minimum and maximum valid values.
355     */
356     function ValidatorIntegerRange($range){
357         $this->minimum = $range['minimum'];
358         $this->maximum = $range['maximum'];
359         return;
360     }
361     
362     /**
363     * Checks to ensure that the parameter is an integer and within the desired 
364     * range.
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.
368     */
369     function validate ($integer){
370         if (is_int($integer)) {
371             if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
372                 return true;
373             }
374         }
375         return false;
376     }
377
378 }
379
380 /**
381 * Validator subclass for use with integer values to be selected from a list.
382 * @author Joby Walker<zorloc@imperium.org>
383 */
384 class ValidatorIntegerList extends ValidatorInteger {
385
386     /** 
387     * Array of potential valid values
388     * @var array
389     * @access protected
390     */
391     var $intList;
392     
393     /**
394     * Constructor
395     * 
396     * Saves parameter as the instance variable $intList.
397     * @param array List of valid values.
398     */
399     function ValidatorIntegerList($intList){
400         $this->intList = $intList;
401         return;
402     }
403
404     /**
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.
409     */
410     function validate ($integer){
411         if (is_int($integer)) {
412             if (in_array($integer, $this->intList, true)) {
413                 return true;
414             }
415         }
416         return false;
417     }
418
419 }
420
421 /**
422 * Validator subclass for string values with no bounds
423 * @author Joby Walker<zorloc@imperium.org>
424 */
425 class ValidatorString extends Validator {
426
427     /**
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.
431     */
432     function validate ($string){
433         if (is_string($string)) {
434             return true;
435         }
436         return false;
437     }
438
439 }
440
441 /**
442 * Validator subclass for string values to be selected from a list.
443 * @author Joby Walker<zorloc@imperium.org>
444 */
445 class ValidatorStringList extends ValidatorString {
446
447     /** 
448     * Array of potential valid values
449     * @var array
450     * @access protected
451     */
452     var stringList;
453     
454     /**
455     * Constructor
456     * 
457     * Saves parameter as the instance variable $stringList.
458     * @param array List of valid values.
459     */
460     function ValidatorStringList($stringList){
461         $this->stringList = $stringList;
462         return;
463     }
464
465     /**
466     * Checks the parameter to ensure that is is a string, and within 
467     * the defined list.
468     * @param string $string Value to check.
469     * @return boolean True if parameter is a string and in the list, false else.
470     */
471     function validate($string){
472         if (is_string($string)) {
473             if (in_array($string, $this->stringList, true)) {
474                 return true;
475             }
476         }
477         return false;
478     }
479
480 }
481
482 /**
483 * Validator subclass for string values that must meet a PCRE.
484 * @author Joby Walker<zorloc@imperium.org>
485 */
486 class ValidatorStringPcre extends ValidatorString {
487
488     /** 
489     * PCRE to validate value
490     * @var array
491     * @access protected
492     */
493     var pattern;
494
495     /**
496     * Constructor
497     * 
498     * Saves parameter as the instance variable $pattern.
499     * @param array PCRE pattern to determin validity.
500     */
501     function ValidatorStringPcre($pattern){
502         $this->pattern = $pattern;
503         return;
504     }
505
506     /**
507     * Checks the parameter to ensure that is is a string, and matches the 
508     * defined pattern.
509     * @param string $string Value to check.
510     * @return boolean True if parameter is a string and matches the pattern,
511     * false else.
512     */
513     function validate ($string){
514         if (is_string($string)) {
515             if (preg_match($this->pattern, $string)) {
516                 return true;
517             }
518         }
519         return false;
520     }
521 }
522
523 /**
524 * Validator subclass for constant values.
525 * @author Joby Walker<zorloc@imperium.org>
526 */
527 class ValidatorConstant extends Validator {
528
529     /**
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.
533     */
534     function validate ($constant){
535         if (defined($constant)) {
536             return true;
537         }
538         return false;
539     }
540 }
541
542 /**
543 * Validator subclass for constant values to be selected from a list.
544 * @author Joby Walker<zorloc@imperium.org>
545 */
546 class ValidatorConstantList extends Validator {
547
548     /** 
549     * Array of potential valid values
550     * @var array
551     * @access protected
552     */
553     var constantList;
554
555     /**
556     * Constructor
557     * 
558     * Saves parameter as the instance variable $constantList.
559     * @param array List of valid values.
560     */
561     function ValidatorConstantList($constantList){
562         $this->constantList = $constantList;
563         return;
564     }
565
566     /**
567     * Checks the parameter to ensure that is is a constant, and within 
568     * the defined list.
569     * @param string $constant Value to check.
570     * @return boolean True if parameter is a constant and in the list, false else.
571     */
572     function validate ($constant){
573         if (defined($constant)) {
574             if (in_array($constant, $this->constantList, true)) {
575                 return true;
576             }
577         }
578         return false;
579     }
580 }
581
582 /**
583 * Validator subclass for an array.
584 * @author Joby Walker<zorloc@imperium.org>
585 */
586 class ValidatorArray extends Validator {
587
588     /*
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 
591     * array is valid.
592     * @param array $array Value to check.
593     * @return boolean True if the value is and array and members are valid, false else.
594     */
595     function validate($array){
596         if(is_array($array)){
597             return $this->validMembers($array);
598         }
599         return false
600     }
601     
602     /**
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.
606     */
607     function validMembers($array){
608         return true;
609     }
610 }
611
612 /**
613 * Validator subclass for an array of strings.
614 * @author Joby Walker<zorloc@imperium.org>
615 */
616 class ValidatorArrayString extends Validator {
617
618     /**
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.
622     */
623     function validMembers($array){
624         foreach ($array as $member){
625             if (!is_string($member)) {
626                 return false;
627             }
628         }
629         return true;
630     }
631 }
632
633 /**
634 * Validator subclass for an array of strings that must be in a list of 
635 * defined values.
636 * @author Joby Walker<zorloc@imperium.org>
637 */
638 class ValidatorArrayStringList extends Validator {
639
640     /** 
641     * Array of potential valid values
642     * @var array
643     * @access protected
644     */
645     var stringList;
646
647     /**
648     * Constructor
649     * 
650     * Saves parameter as the instance variable $stringList.
651     * @param array List of valid values.
652     */
653     function ValidatorArrayStringList($stringList){
654         $this->stringList = $stringList;
655         return;
656     }
657
658     /**
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, 
663     * false else.
664     */
665     function validMembers($array){
666         foreach ($array as $member){
667             if(!in_array($member, $stringList, true)){
668                 return false;
669             }
670         }
671         return true;
672     }
673
674 }
675
676
677
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.
682 //
683 //Revision 1.2  2003/01/28 06:31:00  zorloc
684 //Mostly done but ConfigArray will probably need some more helper methods.
685 //
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.
689 //
690
691 // Local Variables:
692 // mode: php
693 // tab-width: 8
694 // c-basic-offset: 4
695 // c-hanging-comment-ender-p: nil
696 // indent-tabs-mode: nil
697 // End:
698 ?>