37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
<?php
|
|
|
|
/** Values derived from a submission (not entered by the visitor), exposed to templates. */
|
|
final class RsvFormCalculatedValues {
|
|
/** @return array<string, mixed> */
|
|
public function for(RsvFormDefinition $definition, RsvFormData $data): array {
|
|
$calculator = new RsvFormPriceCalculator();
|
|
global $rsv_form_price_registry;
|
|
|
|
$price_before_discount = 0.0;
|
|
foreach ($definition->getElements() as $element) {
|
|
$element_calculator = $rsv_form_price_registry->get($element->getType());
|
|
if ($element_calculator === null) {
|
|
continue;
|
|
}
|
|
$price_before_discount += (float) $element_calculator($element, $data->getValue($element->getName()));
|
|
}
|
|
|
|
$discount_pct = (new RsvMembershipService())->discount_for($definition, $data);
|
|
$final_price = $calculator->calculate($definition, $data);
|
|
|
|
return [
|
|
'price' => $final_price,
|
|
'price_before_discount' => $price_before_discount,
|
|
'discount_percent' => $discount_pct,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* The names these values expose, so template validation accepts {{ price }}.
|
|
* @return list<string>
|
|
*/
|
|
public static function names(): array {
|
|
return ['price', 'price_before_discount', 'discount_percent'];
|
|
}
|
|
}
|