]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - config/Tools.php
More work for new ini config system. Tools has array type removed and first implimen...
[SourceForge/phpwiki.git] / config / Tools.php
1 <?php
2 rcs_id('$Id: Tools.php,v 1.6 2004-04-16 23:30:41 zorloc 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  * Class for Configuration properties
26  * 
27  * Class provides the functions 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         if (isset($params['validator'])) {
88             $params['validator'] = &Validator::getValidator($params['validator']);
89         }
90         return new ConfigValue($params);
91     }
92
93     /**
94     * Determines if the value is valid.
95     * 
96     * If the parameter is a valid value for this config value returns
97     * true, false else.
98     * @param mixed $value Value to be checked for validity.
99     * @return boolean True if valid, false else.
100     */
101     function valid($value){
102         if ($this->validator->validate($value)) {
103             return true;
104         }
105         trigger_error("Value for \'" . $this->name . "\' is invalid.",
106                       E_USER_WARNING);
107         return false;
108     }
109
110     /**
111     * Determines the value currently being used.
112     * 
113     * Just returns the default value.
114     * @return mixed The currently used value (the default).
115     */
116     function getStarting(){
117         return $this->defaultValue;
118     }
119     
120     /**
121     * Get the currently selected value.
122     * 
123     * @return mixed The currently selected value.
124     */
125     function getCurrent(){
126         return $this->currentValue;
127     }
128
129     /**
130     * Set the current value to this.
131     * 
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.
135     */    
136     function setCurrent($value){
137         if ($this->valid($value)) {
138             $this->currentValue = $value;
139         }
140     }
141     
142     /**
143     * Get the Name of the Value
144     * @return mixed Name of the value.
145     */
146     function getName(){
147         return $this->name;
148     }
149     
150     /**
151     * Get the default value of the Value
152     * @return mixed Default value of the value.
153     */
154     function getDefaultValue(){
155         return $this->defaultValue;
156     }
157     
158     /**
159     * Get the Short Description of the Value
160     * @return mixed Short Description of the value.
161     */
162     function getShortDescription(){
163         return $this->description['short'];
164     }
165
166     /**
167     * Get the Full Description of the Value
168     * @return mixed Full Description of the value.
169     */
170     function getFullDescription(){
171         return $this->description['full'];
172     }
173 }
174
175
176
177
178 /**
179 * Abstract base Validator Class
180 * @author Joby Walker<zorloc@imperium.org>
181 */
182 class Validator {
183
184     /**
185     * Constructor
186     * 
187     * Dummy constructor that does nothing.
188     */
189     function Validator(){
190         return;
191     }
192
193     /**
194     * Dummy valitate method -- always returns true.
195     * @param mixed $value Value to check.
196     * @return boolean Always returns true.
197     */
198     function validate($value){
199         return true;
200     }
201     
202     /**
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.
206     * @static
207     */
208     function getValidator($params){
209         extract($params, EXTR_OVERWRITE);
210         $class = 'Validator' . $type;
211         if (isset($list)){
212             $class .= 'List';
213             return new $class ($list);
214         } elseif (isset($range)) {
215             $class .= 'Range';
216             return new $class ($range);
217         } elseif (isset($pcre)){
218             $class .= 'Pcre';
219             return new $class ($pcre);
220         }
221         return new $class ();
222     
223     }
224
225 }
226
227 /**
228 * Validator subclass for use with boolean values
229 * @author Joby Walker<zorloc@imperium.org>
230 */
231 class ValidatorBoolean extends Validator {
232
233     /**
234     * Checks the parameter to see if it is a boolean, returns true if
235     * it is, else false.
236     * @param boolean $boolean Value to check to ensure it is a boolean.
237     * @return boolean True if parameter is boolean.
238     */
239     function validate ($boolean){
240         if (is_bool($boolean)) {
241             return true;
242         }
243         return false;
244     }
245 }
246
247 /**
248 * Validator subclass for use with integer values with no bounds.
249 * @author Joby Walker<zorloc@imperium.org>
250 */
251 class ValidatorInteger extends Validator {
252
253     /**
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.
257     */
258     function validate ($integer){
259         if (is_int($integer)) {
260             return true;
261         }
262         return false;
263     }
264 }
265
266 /**
267 * Validator subclass for use with integer values to be bound within a range.
268 * @author Joby Walker<zorloc@imperium.org>
269 */
270 class ValidatorIntegerRange extends ValidatorInteger {
271
272     /** 
273     * Minimum valid value
274     * @var integer
275     * @access protected
276     */
277     var $minimum;
278     
279     /** 
280     * Maximum valid value
281     * @var integer
282     * @access protected
283     */
284     var $maximum;
285
286     /**
287     * Constructor
288     * 
289     * Sets the minimum and maximum values from the parameter array.
290     * @param array $range Minimum and maximum valid values.
291     */
292     function ValidatorIntegerRange($range){
293         $this->minimum = $range['minimum'];
294         $this->maximum = $range['maximum'];
295         return;
296     }
297     
298     /**
299     * Checks to ensure that the parameter is an integer and within the desired 
300     * range.
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.
304     */
305     function validate ($integer){
306         if (is_int($integer)) {
307             if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
308                 return true;
309             }
310         }
311         return false;
312     }
313
314 }
315
316 /**
317 * Validator subclass for use with integer values to be selected from a list.
318 * @author Joby Walker<zorloc@imperium.org>
319 */
320 class ValidatorIntegerList extends ValidatorInteger {
321
322     /** 
323     * Array of potential valid values
324     * @var array
325     * @access protected
326     */
327     var $intList;
328     
329     /**
330     * Constructor
331     * 
332     * Saves parameter as the instance variable $intList.
333     * @param array List of valid values.
334     */
335     function ValidatorIntegerList($intList){
336         $this->intList = $intList;
337         return;
338     }
339
340     /**
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.
345     */
346     function validate ($integer){
347         if (is_int($integer)) {
348             if (in_array($integer, $this->intList, true)) {
349                 return true;
350             }
351         }
352         return false;
353     }
354
355 }
356
357 /**
358 * Validator subclass for string values with no bounds
359 * @author Joby Walker<zorloc@imperium.org>
360 */
361 class ValidatorString extends Validator {
362
363     /**
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.
367     */
368     function validate ($string){
369         if (is_string($string)) {
370             return true;
371         }
372         return false;
373     }
374
375 }
376
377 /**
378 * Validator subclass for string values to be selected from a list.
379 * @author Joby Walker<zorloc@imperium.org>
380 */
381 class ValidatorStringList extends ValidatorString {
382
383     /** 
384     * Array of potential valid values
385     * @var array
386     * @access protected
387     */
388     var $stringList;
389     
390     /**
391     * Constructor
392     * 
393     * Saves parameter as the instance variable $stringList.
394     * @param array List of valid values.
395     */
396     function ValidatorStringList($stringList){
397         $this->stringList = $stringList;
398         return;
399     }
400
401     /**
402     * Checks the parameter to ensure that is is a string, and within 
403     * the defined list.
404     * @param string $string Value to check.
405     * @return boolean True if parameter is a string and in the list, false else.
406     */
407     function validate($string){
408         if (is_string($string)) {
409             if (in_array($string, $this->stringList, true)) {
410                 return true;
411             }
412         }
413         return false;
414     }
415
416 }
417
418 /**
419 * Validator subclass for string values that must meet a PCRE.
420 * @author Joby Walker<zorloc@imperium.org>
421 */
422 class ValidatorStringPcre extends ValidatorString {
423
424     /** 
425     * PCRE to validate value
426     * @var array
427     * @access protected
428     */
429     var $pattern;
430
431     /**
432     * Constructor
433     * 
434     * Saves parameter as the instance variable $pattern.
435     * @param array PCRE pattern to determin validity.
436     */
437     function ValidatorStringPcre($pattern){
438         $this->pattern = $pattern;
439         return;
440     }
441
442     /**
443     * Checks the parameter to ensure that is is a string, and matches the 
444     * defined pattern.
445     * @param string $string Value to check.
446     * @return boolean True if parameter is a string and matches the pattern,
447     * false else.
448     */
449     function validate ($string){
450         if (is_string($string)) {
451             if (preg_match($this->pattern, $string)) {
452                 return true;
453             }
454         }
455         return false;
456     }
457 }
458
459 /**
460 * Validator subclass for constant values.
461 * @author Joby Walker<zorloc@imperium.org>
462 */
463 class ValidatorConstant extends Validator {
464
465     /**
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.
469     */
470     function validate ($constant){
471         if (defined($constant)) {
472             return true;
473         }
474         return false;
475     }
476 }
477
478 /**
479 * Validator subclass for constant values to be selected from a list.
480 * @author Joby Walker<zorloc@imperium.org>
481 */
482 class ValidatorConstantList extends Validator {
483
484     /** 
485     * Array of potential valid values
486     * @var array
487     * @access protected
488     */
489     var $constantList;
490
491     /**
492     * Constructor
493     * 
494     * Saves parameter as the instance variable $constantList.
495     * @param array List of valid values.
496     */
497     function ValidatorConstantList($constantList){
498         $this->constantList = $constantList;
499         return;
500     }
501
502     /**
503     * Checks the parameter to ensure that is is a constant, and within 
504     * the defined list.
505     * @param string $constant Value to check.
506     * @return boolean True if parameter is a constant and in the list, false else.
507     */
508     function validate ($constant){
509         if (defined($constant)) {
510             if (in_array($constant, $this->constantList, true)) {
511                 return true;
512             }
513         }
514         return false;
515     }
516 }
517
518 /**
519 * Validator subclass for an array.
520 * @author Joby Walker<zorloc@imperium.org>
521 */
522 class ValidatorArray extends Validator {
523
524     /*
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 
527     * array is valid.
528     * @param array $array Value to check.
529     * @return boolean True if the value is and array and members are valid, false else.
530     */
531     function validate($array){
532         if(is_array($array)){
533             return $this->validMembers($array);
534         }
535         return false;
536     }
537     
538     /**
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.
542     */
543     function validMembers($array){
544         return true;
545     }
546 }
547
548 /**
549 * Validator subclass for an array of strings.
550 * @author Joby Walker<zorloc@imperium.org>
551 */
552 class ValidatorArrayString extends Validator {
553
554     /**
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.
558     */
559     function validMembers($array){
560         foreach ($array as $member){
561             if (!is_string($member)) {
562                 return false;
563             }
564         }
565         return true;
566     }
567 }
568
569 /**
570 * Validator subclass for an array of strings that must be in a list of 
571 * defined values.
572 * @author Joby Walker<zorloc@imperium.org>
573 */
574 class ValidatorArrayStringList extends Validator {
575
576     /** 
577     * Array of potential valid values
578     * @var array
579     * @access protected
580     */
581     var $stringList;
582
583     /**
584     * Constructor
585     * 
586     * Saves parameter as the instance variable $stringList.
587     * @param array List of valid values.
588     */
589     function ValidatorArrayStringList($stringList){
590         $this->stringList = $stringList;
591         return;
592     }
593
594     /**
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, 
599     * false else.
600     */
601     function validMembers($array){
602         foreach ($array as $member){
603             if(!in_array($member, $stringList, true)){
604                 return false;
605             }
606         }
607         return true;
608     }
609
610 }
611
612
613
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)
617 //
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.
621 //
622 //Revision 1.2  2003/01/28 06:31:00  zorloc
623 //Mostly done but ConfigArray will probably need some more helper methods.
624 //
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.
628 //
629
630 // Local Variables:
631 // mode: php
632 // tab-width: 8
633 // c-basic-offset: 4
634 // c-hanging-comment-ender-p: nil
635 // indent-tabs-mode: nil
636 // End:
637 ?>