<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * «abstract» InputStream
 *
 * @abstract
 * @access      public
 * @package     yana
 * @subpackage  io
 */
class InputStream extends SerializeableObject
{
    /**#@+
     * @ignore
     */

    /**
     * @access  protected
     * @var     string */  var $filename   = "";
    /**
     * @access  private
     * @var     array */   var $attributes = array();

    /**#@-*/

    /**
     * constructor
     *
     * @param string $filename
     */ 
    function InputStream($filename)
    {
        assert('is_string($filename);');
        settype($filename, "string");
        $this->filename = $filename;
        $this->resetStats();
    }

    /**
     * «abstract» read
     *
     * implement this method in sub class
     *
     * @access public
     * @abstract
     * @return boolean
     */
    function read()
    {
        /* intentionaly left blank */
        return true;
    }

    /**
     * get
     *
     * @access public
     * @return string
     */
    function get() {
        $text = "IO-Stream \"".$this->filename."\"\n";
        foreach ($this->attributes as $a => $b) {
            $text .= "\t- $a = ".gettype($b)."(".$b.")\n";
        }
        return $text;
    }

    /**
     * «abstract» toString
     *
     * implement this method in sub class
     *
     * @access public
     * @abstract
     * @return string
     */
    function toString()
    {
        if (!$this->exists()) {
            return "Path to '".$this->filename."' does not exist\n";
        } elseif ($this->isEmpty()) {
            return "Path to '".$this->filename."' exists, but it is wether empty or not loaded\n";
        } else {
            return $this->get();
        }
    }

    /**
     * getFilename
     *
     * @access public
     * @return string
     */
    function getFilename()
    {
        assert('is_string($this->filename);');
        return ( $this->filename );
    }

    /**
     * exists
     *
     * @access public
     * @return boolean
     */
    function exists()
    {
        return file_exists($this->getFilename());
    }

    /**
     * «abstract» isEmpty
     *
     * implement this method in sub class
     *
     * @access public
     * @abstract
     * @return boolean
     */
    function isEmpty()
    {
        return ( false );
    }

    /**
     * resetStats
     *
     * Reset file stats, e.g. after creating a file that did not exist.
     * While this method is never used in THIS class, it will propably
     * be essential for its sub classes.
     *
     * @access protected
     */
    function resetStats()
    {
        if (file_exists($this->filename) && is_readable($this->filename)) {
            $this->attributes['FILESIZE']    = filesize($this->filename);
            $this->attributes['MTIME']       = date("d.m.Y",filemtime($this->filename));
            $this->attributes['ATIME']       = date("d.m.Y H:i:s",fileatime($this->filename));
            $this->attributes['CTIME']       = date("d.m.Y",filectime($this->filename));
            $this->attributes['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
            clearstatcache();
        }
    }

}

?>