|
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
|
|
|
* @package silvershop-shipping |
|
9
|
|
|
*/ |
|
10
|
|
|
class ShippingPackage |
|
11
|
|
|
{ |
|
12
|
|
|
protected $weight; |
|
13
|
|
|
protected $height; |
|
14
|
|
|
protected $width; |
|
15
|
|
|
protected $depth; |
|
16
|
|
|
protected $diameter; |
|
17
|
|
|
protected $value; |
|
18
|
|
|
protected $currency; |
|
19
|
|
|
protected $quantity; |
|
20
|
|
|
|
|
21
|
|
|
protected $defaultdimensions = array( |
|
22
|
|
|
'height' => 0, |
|
23
|
|
|
'width' => 0, |
|
24
|
|
|
'depth' => 0, |
|
25
|
|
|
'diameter' => 0 |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
protected $defaultoptions = array( |
|
29
|
|
|
'value' => 0, |
|
30
|
|
|
'quantity' => 0, |
|
31
|
|
|
'shape' => 'box', |
|
32
|
|
|
'weightunit' => 'kg', |
|
33
|
|
|
'widthunit' => 'cm', |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
protected $dimensionaliases = array( |
|
37
|
|
|
0 => 'height', |
|
38
|
|
|
1 => 'width', |
|
39
|
|
|
2 => 'depth', |
|
40
|
|
|
'h' => 'height', |
|
41
|
|
|
'w' => 'width', |
|
42
|
|
|
'd' => 'depth' |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
public function __construct($weight = 0, $dimensions = array(), $options = array()) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->weight = $weight; |
|
48
|
|
|
//set via aliases |
|
49
|
|
|
foreach ($dimensions as $key => $dimension) { |
|
50
|
|
|
if (isset($this->dimensionaliases[$key])) { |
|
51
|
|
|
$dimensions[$this->dimensionaliases[$key]] = $dimension; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
$d = array_merge($this->defaultdimensions, $dimensions); |
|
55
|
|
|
foreach ($this->defaultdimensions as $name => $dimension) { |
|
56
|
|
|
if (isset($d[$name])) { |
|
57
|
|
|
$this->$name = (float)$d[$name]; //force float type for dimensions |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
$o = array_merge($this->defaultoptions, $options); |
|
61
|
|
|
foreach ($this->defaultoptions as $name => $option) { |
|
62
|
|
|
if (isset($o[$name])) { |
|
63
|
|
|
$this->$name = $o[$name]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
//force 0 values for anything below 0 |
|
67
|
|
|
$zerochecks = array_merge( |
|
68
|
|
|
$this->defaultdimensions, |
|
69
|
|
|
array('value' => null, 'quantity' => null) |
|
70
|
|
|
); |
|
71
|
|
|
foreach ($zerochecks as $dimension => $value) { |
|
72
|
|
|
if ($this->$dimension < 0) { |
|
73
|
|
|
$this->$dimension = 0; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function toArray() |
|
79
|
|
|
{ |
|
80
|
|
|
$data = array( |
|
81
|
|
|
"weight" => $this->weight, |
|
82
|
|
|
"height" => $this->height, |
|
83
|
|
|
"width" => $this->width, |
|
84
|
|
|
"depth" => $this->depth, |
|
85
|
|
|
"volume" => $this->volume(), |
|
86
|
|
|
"diameter" => $this->diameter, |
|
87
|
|
|
"value" => $this->value, |
|
88
|
|
|
"currency" => $this->currency, |
|
89
|
|
|
"quantity" => $this->quantity |
|
90
|
|
|
); |
|
91
|
|
|
return array_filter($data); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function __toString() |
|
95
|
|
|
{ |
|
96
|
|
|
$out = ""; |
|
97
|
|
|
foreach ($this->toArray() as $key => $value) { |
|
98
|
|
|
$out .= strtoupper($key).$value; |
|
99
|
|
|
} |
|
100
|
|
|
return $out; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Calculate total volume, based on given dimensions |
|
105
|
|
|
*/ |
|
106
|
|
|
public function volume() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->height * $this->width * $this->depth; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function weight() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->weight; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function height() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->height; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function width() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->width; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function depth() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->depth; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function value() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->value; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function quantity() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->quantity; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|