build_descriptor( $session, [], [ [ 'key' => 'service_id', 'type' => 'integer', 'required' => true, ], [ 'key' => 'duration_id', 'type' => 'integer', 'required' => false, ], [ 'key' => 'attendees', 'type' => 'integer', 'required' => false, ], [ 'key' => 'price_minor', 'type' => 'integer', 'required' => false, ], ] ); } /** * Validate submitted input. * * @param BookingSession $session Session. * @param array $input Payload. */ public function validate( BookingSession $session, array $input ): ValidationResult { unset( $session ); $errors = []; if ( null === $this->positive_int( $input, 'service_id' ) ) { $errors['service_id'] = 'Please choose a service.'; } if ( isset( $input['attendees'] ) && (int) $input['attendees'] < 1 ) { $errors['attendees'] = 'Attendees must be at least 1.'; } return [] === $errors ? ValidationResult::ok() : ValidationResult::invalid( $errors ); } /** * Apply validated input. * * @param BookingSession $session Session. * @param array $input Payload. */ public function apply( BookingSession $session, array $input ): BookingSession { $service_id = (int) $input['service_id']; $duration_id = $this->positive_int( $input, 'duration_id' ); $attendees = isset( $input['attendees'] ) ? max( 1, (int) $input['attendees'] ) : 1; $price = isset( $input['price_minor'] ) ? max( 0, (int) $input['price_minor'] ) : $session->draft()->price_minor(); $currency = $this->string_value( $input, 'currency' ) ?? $session->draft()->currency(); $draft = $session->draft()->with_service( $service_id, $duration_id, $attendees, $price, $currency ); $totals = $session->totals(); $totals['subtotal_minor'] = $price; $totals['total_minor'] = $price; $totals['due_now_minor'] = $price; $totals['currency'] = $currency; return $session->with_draft( $draft )->with_totals( $totals ); } }