public function ShippingRate::__construct in Commerce Shipping 8.2
Constructs a new ShippingRate object.
Parameters
array $definition: The definition.
File
- src/
ShippingRate.php, line 68
Class
- ShippingRate
- Represents a shipping rate.
Namespace
Drupal\commerce_shippingCode
public function __construct(array $definition) {
foreach ([
'shipping_method_id',
'service',
'amount',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new \InvalidArgumentException(sprintf('Missing required property %s.', $required_property));
}
}
if (!$definition['service'] instanceof ShippingService) {
throw new \InvalidArgumentException(sprintf('Property "service" should be an instance of %s.', ShippingService::class));
}
if (!$definition['amount'] instanceof Price) {
throw new \InvalidArgumentException(sprintf('Property "amount" should be an instance of %s.', Price::class));
}
// The ID is not required because most shipping methods generate one
// rate per service, and use the service ID when purchasing labels.
if (empty($definition['id'])) {
$shipping_method_id = $definition['shipping_method_id'];
$service_id = $definition['service']
->getId();
$definition['id'] = $shipping_method_id . '--' . $service_id;
}
$this->id = $definition['id'];
$this->shippingMethodId = $definition['shipping_method_id'];
$this->service = $definition['service'];
$this->originalAmount = $definition['original_amount'] ?? $definition['amount'];
$this->amount = $definition['amount'];
$this->description = $definition['description'] ?? '';
$this->deliveryDate = $definition['delivery_date'] ?? NULL;
}