PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / CarrierModalFormFactory.php

CarrierModalFormFactory.php in Packeta trunk, at src/Packetery/Module/Order/CarrierModalFormFactory.php

77 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class CarrierModalFormFactory
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Module\FormFactory;
13 use Packetery\Nette\Forms\Form;
14
15 /**
16 * Class CarrierModalFormFactory
17 *
18 * @package Packetery
19 */
20 class CarrierModalFormFactory {
21
22 public const FIELD_CARRIER_ID = 'carrierId';
23 private const FIELD_NONCE = 'packetery_carrier_metabox_nonce';
24
25 /**
26 * Class FormFactory
27 *
28 * @var FormFactory
29 */
30 private $formFactory;
31
32 /**
33 * FormFactory constructor
34 *
35 * @param FormFactory $formFactory Form factory.
36 */
37 public function __construct( FormFactory $formFactory ) {
38 $this->formFactory = $formFactory;
39 }
40
41 /**
42 * Creating a form
43 *
44 * @param string[] $carrierOptions An array of carrier names.
45 * @param string|null $currentCarrier Current carrier.
46 *
47 * @return Form
48 */
49 public function create( array $carrierOptions, ?string $currentCarrier ): Form {
50 $form = $this->formFactory->create();
51
52 $form->addSelect( self::FIELD_CARRIER_ID, __( 'Carrier:', 'packeta' ), $carrierOptions )
53 ->setRequired()
54 ->setPrompt( __( 'Pick a carrier', 'packeta' ) );
55
56 $form->addHidden( self::FIELD_NONCE );
57
58 foreach ( $carrierOptions as $carrierId => $carrierName ) {
59 if ( $carrierId === $currentCarrier ) {
60 $form->setDefaults(
61 [
62 self::FIELD_NONCE => wp_create_nonce(),
63 self::FIELD_CARRIER_ID => $currentCarrier,
64 ]
65 );
66
67 break;
68 }
69 }
70
71 $form->addSubmit( 'submit', __( 'Save', 'packeta' ) );
72 $form->addSubmit( 'cancel', __( 'Cancel', 'packeta' ) );
73
74 return $form;
75 }
76 }
77