Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * A base class from which all classes which have configurable properties are
- * extended. Provides a generic setter with integrated validation.
- */
- abstract class LoggerBase {
- // Various property types
- const PROP_TYPE_BOOLEAN = 'boolean';
- const PROP_TYPE_INTEGER = 'integer';
- const PROP_TYPE_NUMERIC = 'numeric';
- const PROP_TYPE_STRING = 'string';
- const PROP_TYPE_FILESIZE = 'filesize';
- const PROP_TYPE_LEVEL = 'level';
- /**
- * The main setter function, used for setting all properties.
- *
- * @return TRUE if the property was successfully set, FALSE if an error occured.
- */
- protected function set($property, $value, $type, $check = null)
- {
- if (!property_exists($this, $property)) {
- $this->warn("Property '$property' does not exist.");
- return false;
- }
- switch($type) {
- case self::PROP_TYPE_BOOLEAN:
- return $this->setBoolean($property, $value);
- case self::PROP_TYPE_INTEGER:
- return $this->setInteger($property, $value);
- case self::PROP_TYPE_NUMERIC:
- return $this->setNumeric($property, $value);
- case self::PROP_TYPE_STRING:
- return $this->setString($property, $value);
- default:
- $this->warn("No setter defined for property type [$type].");
- return false;
- }
- }
- /** Setter function for boolean type. */
- private function setBoolean($property, $value)
- {
- try {
- $this->$property = LoggerOptionConverter::toBooleanEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed, current value: [$propValue].");
- }
- }
- /** Setter function for integer type. */
- private function setInteger($property, $value)
- {
- try {
- $this->$property = LoggerOptionConverter::toIntegerEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected an integer. Property not changed, current value: [$propValue].");
- }
- }
- /** Setter function for numeric type. */
- private function setNumeric($property, $value)
- {
- try {
- $this->$property = LoggerOptionConverter::toNumericEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a number. Property not changed, current value: [$propValue].");
- }
- }
- /** Setter function for string type. */
- private function setString($property, $value)
- {
- try {
- $this->$property = LoggerOptionConverter::toStringEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed, current value: [$propValue].");
- }
- }
- /** Triggers a warning. */
- protected function warn($message)
- {
- $class = get_class($this);
- trigger_error("log4php: $class: $message", E_USER_WARNING);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement