<?php
/**
 * Wombat Mail Mime Part Attachment
 *
 * $Id$
 *
 * @author gERD Schaufelberger <gerd@exit0.net>
 * @license PHP License
 * @package Wombat
 * @subpackage MailMime
 */

WBClass::load('WBMail_Mime_Part');

/**
 * Wombat Mail Mime Part Attachment
 *
 * Mime Mails part attachment
 *
 * @version 0.1.0
 * @package Wombat
 * @subpackage MailMime
 */
class WBMail_Mime_Part_Attachment extends WBMail_Mime_Part
{
    /**
     * attachment id - default is "__new"
     * @var string
     */
    protected $id = '__new';

    /**
     * filename
     * @var string
     */
    protected $name = '';

    /**
     * data mime-type
     * @var string
     */
    protected $mime = 'application/octet-stream';

    /**
     * attachment binary data
     * @var binary string
     */
    protected $data = null;

    /**
     * attachment size in bytes
     * @var int
     */
    protected $size = 0;

    /**
     * inline file content id
     * @var string
     */
    protected $cid = '';

    /**
     * support for lazy loading
     * @var WBMail_Mime_Storage
     */
    protected $dataLoader   =   null;

    /**
     * Set optional data loader
     *
     * Data loader allows lazy loading of binary data. Lazy loading
     * is only available for few storage modules. In case this feature
     * is available, data will be loaded on getData()
     *
     * @see getData()
     * @param WBMail_Mime_Storage $loader
     */
    public function SetDataLoader(WBMail_Mime_Storage $loader)
    {
        $this->dataLoader   =   $loader;
    }

    /**
     * Fetch attachment id
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set attachment id
     *
     * @param string $id
     */
    public function setId( $id = '__new' )
    {
        $this->id   =   $id;
    }

    /**
     * Get file name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set attachment's file name
     *
     * @param string $name
     */
    public function setName( $name )
    {
        $this->name =   $name;
    }

    /**
     * Get binary data's mime type
     *
     * @return string
     */
    public function getMime()
    {
        return $this->mime;
    }

    /**
     * Set binary data's mime type
     *
     * @param string $mime
     */
    public function setMime( $mime )
    {
        $this->mime =   $mime;
    }

    /**
     * Get size of binary data in bytes
     *
     * @return int
     */
    public function getSize()
    {
        return $this->size;
    }

    /**
     * Set size of binary data in bytes
     *
     * @param int $size
     */
    public function setSize($size)
    {
        $this->size =   $size;
    }

    /**
     * Get content id of data
     *
     * @return string
     */
    public function getCid()
    {
        return $this->cid;
    }

    /**
     * Set content id
     *
     * @param string $cid
     */
    public function setCid($cid)
    {
        if (empty($cid)) {
            $cid    =   '';
        }
        $this->cid  =   $cid;
    }

    /**
     * Get binary data
     *
     * If data-loader is set, data get loaded on demand
     *
     * @return string
     */
    public function getData()
    {
        if ($this->dataLoader && is_null($this->data)) {
            $this->setData($this->dataLoader->getAttachmentData($this->id));
        }
        return $this->data;
    }

    /**
     * Set binary data
     *
     * Set data and remove data loader object
     *
     * @param string $data
     */
    public function setData($data)
    {
        $this->data         =   $data;
        $this->size         =   strlen( $data );
        $this->dataLoader   =   null;
    }
}