PHP Classes

File: _classes/Event.class.php

Recommend this page to a friend!
  Classes of Marius Zadara   Logger   _classes/Event.class.php   Download  
File: _classes/Event.class.php
Role: Class source
Content type: text/plain
Description: Event class definition
Class: Logger
Filter and export data of application events
Author: By
Last change:
Date: 16 years ago
Size: 5,164 bytes
 

Contents

Class file image Download
<?php

/**
 * Event class event definition.
 *
 * @author Marius Zadara <marius@zadara.org>
 * @category org.zadara.marius.logger.classes
 * @copyright (C) 2008 Marius Zadara <marius@zadara.org>
 * @license GNU GPL
 * @package org.zadara.marius.logger
 */

class Event implements IEvent
{
   
/**
     * Id of the event.
     *
     * @access protected
     * @see EventIDs
     */
   
protected $id;

   
/**
     * Source of the event.
     *
     * @access protected
     * @see Sources
     */
   
protected $source;
   
   
/**
     * Category of the event.
     *
     * @access protected
     * @see Categories
     */
   
protected $category;
   
   
/**
     * Message of the event.
     *
     * @access protected
     */
   
protected $message;

   
   
/**
     * Level of the event.
     *
     * @access protected
     * @see Levels
     */
   
protected $level;
       
   
/**
     * Timestamp of the event.
     *
     * @access protected
     */
   
protected $timestamp;

   
   
/**
     * Extra-fields for the event.
     * These are not standard fields definned in Fields class.
     *
     * @access protected
     */
   
protected $extraFields;
   

   
/**
     * Event constructor.
     *
     * @param String $id Id of the event. Allows null.
     * @param String $source Source of the event. Allows null.
     * @param String $category Category of the event. Allows null.
     * @param String $level Level of the event. Allows null.
     * @param $String message Message of the event. Allows null.
     * @param Array $extraFields Extrapfields of the event. Allows null. Use pairs as: key[0] = value[0], key[1] = value[1], etc.
     *
     * @return Event
     *
     * @see EventIDs
     * @see Sources
     * @see Categories
     * @see Levels
     *
     */
   
public function Event($id = null, $source = null, $category = null, $level = null, $message = null, $extraFields = null)
    {
       
// init the fields
       
$this->id = is_null($id) ? null : $id;
       
$this->source = is_null($source) ? null : $source;
       
$this->category = is_null($category) ? null : $category;
       
$this->level = is_null($level) ? null : $level;
       
$this->message = is_null($message) ? null : $message;
       
       
// set the current timestamp
       
$this->timestamp = time();
       
       
$this->extraFields = null;
       
       
// validat the extra fields
       
if (is_array($extraFields) && (sizeof($extraFields) > 0))
        {
           
$this->extraFields = array();
           
$this->extraFields = $extraFields;
        }
    }
   
   
/**
     * Event id getter
     * @access public
     */
   
public function getId()
    {
        return
$this->id;
    }
   
   
/**
     * Event source getter
     * @access public
     */
   
public function getSource()
    {
        return
$this->source;
    }
   
   
   
/**
     * Event category getter
     * @access public
     */
   
public function getCategory()
    {
        return
$this->category;
    }
   
   
/**
     * Event message getter
     * @access public
     */
   
public function getMessage()
    {
        return
$this->message;
    }
   
   
/**
     * Event timestamp getter
     * @access public
     */
   
public function getTimestamp()
    {
        return
$this->timestamp;
    }
   
   
/**
     * Event level getter
     * @access public
     */
   
public function getLevel()
    {
        return
$this->level;
    }
   
   
   
/**
     * Function to get the translation of a variable
     *
     * @param $var The variable
     * @see Translations
     */
   
private function getTranslation($var)
    {
        return
sprintf("<b>%s</b>", Translations::${$var});
    }
   

   
/**
     * Event string representation.
     *
     * @see Translations
     * @see getTranslation()
     *
     * @return string
     */
   
public function __toString()
    {
       
// return default string representation
        // in case of empty fields
       
if
        (
            (
$this->id == null) && ($this->source == null) && ($this->category == null) &&
            (
$this->message == null) && ($this->level == null) && ($this->extraFields == null)
        )
        return
sprintf("%s: %s=%s", $this->getTranslation("EVENT"), $this->getTranslation("TIMESTAMP"), $this->timestamp);

       
// init the string representation
       
$asString = sprintf("%s: ", $this->getTranslation("EVENT"));

       
// add fields only if they have been set
       
if ($this->id != null) $asString .= sprintf("%s=%s;", $this->getTranslation("ID"), $this->id);
        if (
$this->source != null) $asString .= sprintf("%s=%s;", $this->getTranslation("SOURCE"), $this->source);
        if (
$this->category != null) $asString .= sprintf("%s=%s;", $this->getTranslation("CATEGORY"), $this->category);
        if (
$this->level != null) $asString .= sprintf("%s=%s;", $this->getTranslation("LEVEL"), $this->level);
       
       
$asString .= sprintf("%s=%s;", $this->getTranslation("TIMESTAMP"), $this->timestamp);
       
        if (
$this->message != null) $asString .= sprintf("%s=%s;", $this->getTranslation("MESSAGE"), $this->message);
       
       
// add also the extra fields
       
if ($this->extraFields != null)
        {
           
$asString .= sprintf("%s:", $this->getTranslation("EXTRA_FIELDS"));

           
// add each extra field with its value
           
for ($i=0; $i < sizeof($this->extraFields); $i++)
               
$asString .= (($i % 2) == 0) ? sprintf("%s -> ", $this->extraFields[$i]) : sprintf("%s, ", $this->extraFields[$i]);
        }

       
// return the string representation
       
return $asString;
    }
}


?>