Advertisement
Guest User

Untitled

a guest
Nov 7th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * A base class from which all classes which have configurable properties are
  5.  * extended. Provides a generic setter with integrated validation.  
  6.  */
  7. abstract class LoggerBase {
  8.  
  9.     // Various property types
  10.     const PROP_TYPE_BOOLEAN = 'boolean';
  11.     const PROP_TYPE_INTEGER = 'integer';
  12.     const PROP_TYPE_NUMERIC = 'numeric';
  13.     const PROP_TYPE_STRING = 'string';
  14.     const PROP_TYPE_FILESIZE = 'filesize';
  15.     const PROP_TYPE_LEVEL = 'level';
  16.    
  17.     /**
  18.      * The main setter function, used for setting all properties.
  19.      *
  20.      * @return TRUE if the property was successfully set, FALSE if an error occured.
  21.      */
  22.     protected function set($property, $value, $type, $check = null)
  23.     {
  24.         if (!property_exists($this, $property)) {
  25.             $this->warn("Property '$property' does not exist.");
  26.             return false;
  27.         }
  28.        
  29.         switch($type) {
  30.             case self::PROP_TYPE_BOOLEAN:
  31.                 return $this->setBoolean($property, $value);
  32.                
  33.             case self::PROP_TYPE_INTEGER:
  34.                 return $this->setInteger($property, $value);
  35.            
  36.             case self::PROP_TYPE_NUMERIC:
  37.                 return $this->setNumeric($property, $value);
  38.                
  39.             case self::PROP_TYPE_STRING:
  40.                 return $this->setString($property, $value);
  41.                
  42.             default:
  43.                 $this->warn("No setter defined for property type [$type].");
  44.                 return false;
  45.         }
  46.     }
  47.    
  48.     /** Setter function for boolean type. */
  49.     private function setBoolean($property, $value)
  50.     {
  51.         try {
  52.             $this->$property = LoggerOptionConverter::toBooleanEx($value);
  53.         } catch (Exception $ex) {
  54.             $value = var_export($value, true);
  55.             $propValue = var_export($this->$property, true);
  56.             $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed, current value: [$propValue].");
  57.         }
  58.     }
  59.    
  60.     /** Setter function for integer type. */
  61.     private function setInteger($property, $value)
  62.     {
  63.         try {
  64.             $this->$property = LoggerOptionConverter::toIntegerEx($value);
  65.         } catch (Exception $ex) {
  66.             $value = var_export($value, true);
  67.             $propValue = var_export($this->$property, true);
  68.             $this->warn("Invalid value given for '$property' property: [$value]. Expected an integer. Property not changed, current value: [$propValue].");
  69.         }
  70.     }
  71.    
  72.     /** Setter function for numeric type. */
  73.     private function setNumeric($property, $value)
  74.     {
  75.         try {
  76.             $this->$property = LoggerOptionConverter::toNumericEx($value);
  77.         } catch (Exception $ex) {
  78.             $value = var_export($value, true);
  79.             $propValue = var_export($this->$property, true);
  80.             $this->warn("Invalid value given for '$property' property: [$value]. Expected a number. Property not changed, current value: [$propValue].");
  81.         }
  82.     }
  83.    
  84.     /** Setter function for string type. */
  85.     private function setString($property, $value)
  86.     {
  87.         try {
  88.             $this->$property = LoggerOptionConverter::toStringEx($value);
  89.         } catch (Exception $ex) {
  90.             $value = var_export($value, true);
  91.             $propValue = var_export($this->$property, true);
  92.             $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed, current value: [$propValue].");
  93.         }
  94.     }
  95.    
  96.     /** Triggers a warning. */
  97.     protected function warn($message)
  98.     {
  99.         $class = get_class($this);
  100.         trigger_error("log4php: $class: $message", E_USER_WARNING);
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. ?>
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement