<?php
/**
* @filename        ShippingItem.php
* @description
* @version         1.0
* @package         MSHShippingLibrary.shippingClasses
* @author          Mohammad Sajjad Hossain - info@sajjadhossain.com
*
* @link            http://sajjadhossain.com
* @created on      December 27, 2010
* @Dependencies
* @license
***/

class ShippingItem
{
    private $quantity = 1;
    private $weight = 0;
    private $length = 0;
    private $width = 0;
    private $height = 0;
    private $description = '';
    private $errors = array();

    /**
    *@desc Allowed values 'lb', 'kg', 'oz'
    */
    private $weightUnit = 'lb';

    /**
    *@desc Allowed values 'in', 'cm'
    */
    private $lengthUnit = 'in';

    public function __constuct(){}

    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;
    }

    public function setWeight($weight)
    {
        $this->weight = $weight;
    }

    public function setHeight($height)
    {
        $this->height = $height;
    }

    public function setLength($length)
    {
        $this->length = $length;
    }

    public function setWidth($width)
    {
        $this->width = $width;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    public function setWeightUnit($unit)
    {
        $this->weightUnit = $unit;
    }

    public function setLengthUnit($unit)
    {
        $this->lengthUnit = $unit;
    }

    public function getQuantity()
    {
        return $this->quantity;
    }

    public function getWeightInPound()
    {
        if($this->weightUnit == 'lb')
        {
            return $this->weight;
        }
        else if($this->weightUnit == 'kg')
        {
            return round(($this->weight * 2.2046), 2);
        }
        else if($this->weightUnit == 'oz')
        {
            return round(($this->weight * 0.0625), 2);
        }        
    }
    public function getWeightInOunce()
    {
        if($this->weightUnit == 'lb')
        {
            return round(($this->weight * 16), 2);
        }
        else if($this->weightUnit == 'kg')
        {
            return round(($this->weight * 35.27396195), 2);
        }
        else if($this->weightUnit == 'oz')
        {
            return $this->weight;
        }else {
        	return $this->weight;
        }        
    }
    public function getWeightInKilogram()
    {
        if($this->weightUnit == 'kg')
        {
            return $this->weight;
        }
        else if($this->weightUnit == 'lb')
        {
            return round(($this->weight / 2.2046), 2);
        }
        else if($this->weightUnit == 'oz')
        {
            return round(($this->weight / 0.028349523), 2);
        }        
    }

    /**
    *@desc Convers length
    *
    * @param float
    * @param character Allowed 'in', 'cm'
    * @param character Allowed 'in', 'cm'
    */
    private function convertLength($value, $from, $to)
    {
        if($from == 'cm' && $to == 'in')
        {
            return round(($value * 0.3937), 2); // 1 cm == 0.3937 inch
        }
        else if($from == 'in' && $to == 'cm')
        {
            return round(($value * 2.54), 2);   // 1 inch = 2.54 cm
        }
    }

    public function getHeightInInch()
    {
        if($this->lengthUnit == 'in')
        {
            return $this->height;
        }
        else if($this->lengthUnit == 'cm')
        {
            return $this->convertLength($this->height, 'cm', 'in');
        }
    }

    public function getHeightInCentimeter()
    {
        if($this->lengthUnit == 'cm')
        {
            return $this->height;
        }
        else if($this->lengthUnit == 'in')
        {
            return $this->convertLength($this->height, 'in', 'cm');
        }
    }

    public function getLengthInInch()
    {
        if($this->lengthUnit == 'in')
        {
            return $this->length;
        }
        else if($this->lengthUnit == 'cm')
        {
            return $this->convertLength($this->length, 'cm', 'in');
        }
    }

    public function getLengthInCentimeter()
    {
        if($this->lengthUnit == 'cm')
        {
            return $this->length;
        }
        else if($this->lengthUnit == 'in')
        {
            return $this->convertLength($this->length, 'in', 'cm');
        }
    }

    public function getWidthInInch()
    {
        if($this->lengthUnit == 'in')
        {
            return $this->width;
        }
        else if($this->lengthUnit == 'cm')
        {
            return $this->convertLength($this->width, 'cm', 'in');
        }
    }

    public function getWidthInCentimeter()
    {
        if($this->lengthUnit == 'cm')
        {
            return $this->width;
        }
        else if($this->lengthUnit == 'in')
        {
            return $this->convertLength($this->width, 'in', 'cm');
        }
    }

    public function getDescription()
    {
        return $this->description;
    }

    /**
    *@desc Validate package information
    *
    * @param array $attributes
    */
    function validate($attributes)
    {
        if(in_array('quantity', $attributes))
        {
            if(empty($this->quantity))
            {
                $this->errors[] = 'Quantity is missing';
                return false;
            }
        }
        else if(in_array('weight', $attributes))
        {
            if(empty($this->weight))
            {
                $this->errors[] = 'Weight is missing';
                return false;
            }
        }
        else if(in_array('length', $attributes))
        {
            if(empty($this->length))
            {
                $this->errors[] = 'Length is missing';
                return false;
            }
        }
        else if(in_array('height', $attributes))
        {
            if(empty($this->height))
            {
                $this->errors[] = 'Height is missing';
                return false;
            }
        }
        else if(in_array('width', $attributes))
        {
            if(empty($this->width))
            {
                $this->errors[] = 'Width is missing';
                return false;
            }
        }
    }
}
//End of file ShippingItem.php