<?php
/**
* @filename        ShippingResponse.php
* @description     This class is for sending response back to caller
* @version         1.0
* @package         MSHShippingLibrary.shippingClasses
* @author          Mohammad Sajjad Hossain - info@sajjadhossain.com
*
* @link            http://sajjadhossain.com
* @created on      December 07, 2010
* @Dependencies
* @license
***/
class ShippingResponse
{
    private $rates = array();
    private $errors = array();
    private $rawResponse = '';

    /**
    *@desc Adds rare to list
    *
    * @param string Name of the service
    * @param string Rate of the service
    */
    public function setRate($service, $rate)
    {
        if(!empty($service) && !empty($rate))
        {
            $this->rates[] = array('service' => $service, 'rate' => $rate);
        }
    }

    public function resetRates()
    {
        $this->rates = array();
    }

    public function getRates()
    {
        return $this->rates;
    }

    public function setRawResponse($response)
    {
        $this->rawResponse = $response;
    }

    public function getRawResponse()
    {
        return $this->rawResponse;
    }

    public function setError($message)
    {
        $this->errors[] = $message;
    }

    /**
    *@desc Returns the array of errors
    *
    * @return array
    */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
    *@desc Checks if there is any error
    *
    * @return bool
    */
    public function hasError()
    {
        return !empty($this->errors);
    }

    /**
    *@desc Prepares a custom string with errors
    *
    * @param string - Default valie ''. Possible values - 'ul', 'ol', 'div', 'p'
    *
    * @return string
    */
    function getErrorString($tag = '')
    {
        switch($tag)
        {
            case 'ol':
                $startTag = '<ol><li>';
                $endTag = '</li></ol>';
                $delimiter = '</li><li>';
                break;
            case 'div':
                $startTag = '<div>';
                $endTag = '</div>';
                $delimiter = '</div><div>';
                break;
            case 'p':
                $startTag = '<p>';
                $endTag = '</p>';
                $delimiter = '</p><p>';
                break;
            case 'ul':
                $startTag = '<ul><li>';
                $endTag = '</li></ul>';
                $delimiter = '</li><li>';
                break;
            default:
                $startTag = '';
                $endTag = '';
                $delimiter = '. ';
                break;
        }

        $errorString = trim(implode($delimiter, $this->errors));

        if($errorString)
        {
          return $startTag . $errorString . $endTag;
        }

        return '';
    }
}
//End of file ShippingResponse.php