Version: current

      1 <?php
      2 /*
      3  * Encapsulation of shipping package data
      4  *
      5  * @todo: unit conversion support
      6  * @todo: different shape support (eg cylinder) ...with ability to roll back to 'box' if need be
      7  */
      8 class ShippingPackage{
      9
     10     protected $Weight;
     11     protected $Height, $Width, $Depth;
     12     protected $Cost, $currency;
     13     protected $quantity;
     14     protected $Order;
     15
     16     protected static $default_dimensions = array(
     17         //'Height',
     18         //'Width',
     19         //'Depth',
     20     );
     21
     22     protected $defaultoptions = array(
     23         'Cost' => 0,
     24         'quantity' => 0,
     25         'weightunit' => 'kg',
     26         'Order' => 0,
     27     );
     28
     29     static function set_default_dimensions($dimensions) {
     30         self::$default_dimensions = $dimensions;
     31     }
     32
     33     static function get_default_dimensions() {
     34         return self::$default_dimensions;
     35     }
     36
     37     function __construct($weight = 0, $options = array(), $dimensions = array()){
     38         $this->Weight = $weight;
     39         foreach(self::get_default_dimensions() as $dimension){
     40             if(isset($dimensions[$dimension])) {
     41                 $this->$dimension = (float)$dimensions[$dimension]; //force float type for dimensions
     42             }
     43         }
     44         //set via aliases
     45         $o = array_merge($this->defaultoptions, $options);
     46         foreach($this->defaultoptions as $name => $option){
     47             if(isset($o[$name]))
     48                 $this->$name = $o[$name];
     49         }
     50     }
     51     function toArray(){
     52         $data = array(
     53             "Weight" => $this->Weight,
     54             "Cost" => $this->Cost,
     55             "currency" => $this->currency,
     56             "quantity" => $this->quantity
     57         );
     58         return array_filter($data);
     59     }
     60     function __toString(){
     61         $out = "";
     62         foreach($this->toArray() as $key => $value){
     63             $out .= strtoupper($key).$value;
     64         }
     65         return $out;
     66     }
     67     function Weight(){
     68         return $this->Weight;
     69     }
     70     function Cost(){
     71         return $this->Cost;
     72     }
     73
     74     function quantity(){
     75         return $this->quantity;
     76     }
     77
     78     /**
     79      * Calculate total volume, based on given dimensions
     80      */
     81     function Volume(){
     82         return $this->Height * $this->Width * $this->Depth;
     83     }
     84
     85     function Height(){
     86         return $this->Height;
     87     }
     88
     89     function Width(){
     90         return $this->Width;
     91     }
     92
     93     function Depth(){
     94         return $this->Depth;
     95     }
     96
     97     function Order(){
     98         return $this->Order;
     99     }
    100 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -