| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Class Form. | |
| 4 | + * | |
| 5 | + * @package Packetery | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | declare( strict_types=1 ); |
| 4 | 9 | |
| 5 | 10 | namespace Packetery\Module\Order; |
| @@ -4,16 +9,19 @@ | ||
| 4 | 9 | |
| 5 | 10 | namespace Packetery\Module\Order; |
| 6 | 11 | |
| 7 | 12 | use Packetery\Core\CoreHelper; |
| 8 | -use Packetery\Core\Entity\Order; | |
| 9 | 13 | use Packetery\Core\Validator; |
| 10 | 14 | use Packetery\Module\FormFactory; |
| 11 | 15 | use Packetery\Module\FormValidators; |
| 12 | -use Packetery\Module\Framework\WpAdapter; | |
| 13 | 16 | use Packetery\Module\Options\OptionsProvider; |
| 14 | 17 | use Packetery\Nette\Forms; |
| 15 | 18 | |
| 19 | +/** | |
| 20 | + * Class Form. | |
| 21 | + * | |
| 22 | + * @package Packetery | |
| 23 | + */ | |
| 16 | 24 | class Form { |
| 17 | 25 | |
| 18 | 26 | public const FIELD_WEIGHT = 'packeteryWeight'; |
| 19 | 27 | public const FIELD_ORIGINAL_WEIGHT = 'packeteryOriginalWeight'; |
| @@ -27,32 +35,30 @@ | ||
| 27 | 35 | public const FIELD_CALCULATED_COD = 'packeteryCalculatedCod'; |
| 28 | 36 | public const FIELD_DELIVER_ON = 'packeteryDeliverOn'; |
| 29 | 37 | |
| 30 | 38 | /** |
| 39 | + * Class FormFactory | |
| 40 | + * | |
| 31 | 41 | * @var FormFactory |
| 32 | 42 | */ |
| 33 | 43 | private $formFactory; |
| 34 | 44 | |
| 35 | 45 | /** |
| 46 | + * Class Provider | |
| 47 | + * | |
| 36 | 48 | * @var OptionsProvider |
| 37 | 49 | */ |
| 38 | 50 | private $options; |
| 39 | 51 | |
| 40 | 52 | /** |
| 41 | - * @var WpAdapter | |
| 42 | - */ | |
| 43 | - private $wpAdapter; | |
| 44 | - | |
| 45 | - /** | |
| 46 | 53 | * FormFactory constructor |
| 47 | 54 | * |
| 48 | 55 | * @param FormFactory $formFactory Form factory. |
| 49 | 56 | * @param OptionsProvider $options Options provider. |
| 50 | 57 | */ |
| 51 | - public function __construct( FormFactory $formFactory, OptionsProvider $options, WpAdapter $wpAdapter ) { | |
| 58 | + public function __construct( FormFactory $formFactory, OptionsProvider $options ) { | |
| 52 | 59 | $this->formFactory = $formFactory; |
| 53 | 60 | $this->options = $options; |
| 54 | - $this->wpAdapter = $wpAdapter; | |
| 55 | 61 | } |
| 56 | 62 | |
| 57 | 63 | public function create(): Forms\Form { |
| 58 | 64 | $form = $this->formFactory->create(); |
| @@ -57,39 +63,38 @@ | ||
| 57 | 63 | public function create(): Forms\Form { |
| 58 | 64 | $form = $this->formFactory->create(); |
| 59 | 65 | $unit = $this->options->getDimensionsUnit(); |
| 60 | 66 | |
| 61 | - $form->addText( self::FIELD_WEIGHT, $this->wpAdapter->__( 'Weight (kg)', 'packeta' ) ) | |
| 67 | + $form->addText( self::FIELD_WEIGHT, __( 'Weight (kg)', 'packeta' ) ) | |
| 62 | 68 | ->setRequired( false ) |
| 63 | 69 | ->setNullable() |
| 64 | - ->addRule( Forms\Form::FLOAT, $this->wpAdapter->__( 'The weight must be a number.', 'packeta' ) ) | |
| 65 | - ->addRule( Forms\Form::MIN, $this->wpAdapter->__( 'The weight must be a positive number.', 'packeta' ), 0 ); | |
| 70 | + ->addRule( Forms\Form::FLOAT ); | |
| 66 | 71 | $form->addHidden( self::FIELD_ORIGINAL_WEIGHT ); |
| 67 | - $this->formFactory->addDimension( $form, self::FIELD_LENGTH, (string) $this->wpAdapter->__( 'Length', 'packeta' ), $unit ) | |
| 72 | + $this->formFactory->addDimension( $form, self::FIELD_LENGTH, __( 'Length', 'packeta' ), $unit ) | |
| 68 | 73 | ->setNullable(); |
| 69 | - $this->formFactory->addDimension( $form, self::FIELD_WIDTH, (string) $this->wpAdapter->__( 'Width', 'packeta' ), $unit ) | |
| 74 | + $this->formFactory->addDimension( $form, self::FIELD_WIDTH, __( 'Width', 'packeta' ), $unit ) | |
| 70 | 75 | ->setNullable(); |
| 71 | - $this->formFactory->addDimension( $form, self::FIELD_HEIGHT, (string) $this->wpAdapter->__( 'Height', 'packeta' ), $unit ) | |
| 76 | + $this->formFactory->addDimension( $form, self::FIELD_HEIGHT, __( 'Height', 'packeta' ), $unit ) | |
| 72 | 77 | ->setNullable(); |
| 73 | - $form->addCheckbox( self::FIELD_ADULT_CONTENT, $this->wpAdapter->__( 'Adult content', 'packeta' ) ) | |
| 78 | + $form->addCheckbox( self::FIELD_ADULT_CONTENT, __( 'Adult content', 'packeta' ) ) | |
| 74 | 79 | ->setRequired( false ); |
| 75 | - $form->addText( self::FIELD_COD, $this->wpAdapter->__( 'Cash on delivery', 'packeta' ) ) | |
| 80 | + $form->addText( self::FIELD_COD, __( 'Cash on delivery', 'packeta' ) ) | |
| 76 | 81 | ->setRequired( false ) |
| 77 | 82 | ->setNullable() |
| 78 | 83 | ->addRule( Forms\Form::FLOAT ); |
| 79 | 84 | $form->addHidden( self::FIELD_CALCULATED_COD ); |
| 80 | - $form->addText( self::FIELD_VALUE, $this->wpAdapter->__( 'Order value', 'packeta' ) ) | |
| 85 | + $form->addText( self::FIELD_VALUE, __( 'Order value', 'packeta' ) ) | |
| 81 | 86 | ->setRequired( false ) |
| 82 | 87 | ->setNullable() |
| 83 | 88 | ->addRule( Forms\Form::FLOAT ); |
| 84 | 89 | $form->addHidden( self::FIELD_CALCULATED_VALUE ); |
| 85 | - $form->addText( self::FIELD_DELIVER_ON, $this->wpAdapter->__( 'Planned dispatch', 'packeta' ) ) | |
| 90 | + $form->addText( self::FIELD_DELIVER_ON, __( 'Planned dispatch', 'packeta' ) ) | |
| 86 | 91 | ->setHtmlAttribute( 'class', 'date-picker' ) |
| 87 | 92 | ->setHtmlAttribute( 'autocomplete', 'off' ) |
| 88 | 93 | ->setRequired( false ) |
| 89 | 94 | ->setNullable() |
| 90 | 95 | // translators: %s: Represents minimal date for delayed delivery. |
| 91 | - ->addRule( [ FormValidators::class, 'dateIsLater' ], $this->wpAdapter->__( 'Date must be later than %s', 'packeta' ), wp_date( CoreHelper::DATEPICKER_FORMAT ) ); | |
| 96 | + ->addRule( [ FormValidators::class, 'dateIsLater' ], __( 'Date must be later than %s', 'packeta' ), wp_date( CoreHelper::DATEPICKER_FORMAT ) ); | |
| 92 | 97 | |
| 93 | 98 | return $form; |
| 94 | 99 | } |
| 95 | 100 | |
| @@ -106,8 +111,9 @@ | ||
| 106 | 111 | ?float $calculatedValue, |
| 107 | 112 | ?bool $adultContent, |
| 108 | 113 | ?string $deliverOn |
| 109 | 114 | ): void { |
| 115 | + | |
| 110 | 116 | $form->setDefaults( |
| 111 | 117 | [ |
| 112 | 118 | self::FIELD_WEIGHT => $weight, |
| 113 | 119 | self::FIELD_ORIGINAL_WEIGHT => $originalWeight, |
| @@ -130,9 +136,9 @@ | ||
| 130 | 136 | * @param array<string, string> $validationResult Validation result. |
| 131 | 137 | * |
| 132 | 138 | * @return string[] |
| 133 | 139 | */ |
| 134 | - public function getInvalidFieldsFromValidationResult( array $validationResult ): array { | |
| 140 | + public static function getInvalidFieldsFromValidationResult( array $validationResult ): array { | |
| 135 | 141 | $validationFormInputMapping = [ |
| 136 | 142 | self::FIELD_VALUE => Validator\Order::ERROR_TRANSLATION_KEY_VALUE, |
| 137 | 143 | self::FIELD_WEIGHT => Validator\Order::ERROR_TRANSLATION_KEY_WEIGHT, |
| 138 | 144 | self::FIELD_HEIGHT => Validator\Order::ERROR_TRANSLATION_KEY_HEIGHT, |
| @@ -149,45 +155,6 @@ | ||
| 149 | 155 | $fields[] = $fieldName; |
| 150 | 156 | } |
| 151 | 157 | |
| 152 | 158 | return $fields; |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * @param string[] $invalidFieldNames | |
| 157 | - * | |
| 158 | - * @return string | |
| 159 | - */ | |
| 160 | - public function getInvalidFieldsMessageFromValidationResult( array $invalidFieldNames, Order $order ): string { | |
| 161 | - $fieldTranslations = [ | |
| 162 | - self::FIELD_VALUE => $this->wpAdapter->__( 'value', 'packeta' ), | |
| 163 | - self::FIELD_WEIGHT => $this->wpAdapter->__( 'weight', 'packeta' ), | |
| 164 | - self::FIELD_HEIGHT => $this->wpAdapter->__( 'height', 'packeta' ), | |
| 165 | - self::FIELD_WIDTH => $this->wpAdapter->__( 'width', 'packeta' ), | |
| 166 | - self::FIELD_LENGTH => $this->wpAdapter->__( 'length', 'packeta' ), | |
| 167 | - ]; | |
| 168 | - $message = ''; | |
| 169 | - $invalidFields = []; | |
| 170 | - | |
| 171 | - foreach ( $invalidFieldNames as $fieldName ) { | |
| 172 | - if ( isset( $fieldTranslations[ $fieldName ] ) ) { | |
| 173 | - $invalidFields[] = $fieldTranslations[ $fieldName ]; | |
| 174 | - } | |
| 175 | - } | |
| 176 | - | |
| 177 | - if ( $invalidFields !== [] ) { | |
| 178 | - $invalidFieldsString = implode( ', ', $invalidFields ); | |
| 179 | - | |
| 180 | - $message = sprintf( | |
| 181 | - // translators: %s: Required fields. | |
| 182 | - (string) $this->wpAdapter->__( 'Please fill in all required shipment details (%s) before submitting.', 'packeta' ), | |
| 183 | - $invalidFieldsString | |
| 184 | - ); | |
| 185 | - } | |
| 186 | - | |
| 187 | - if ( $order->hasToFillCustomsDeclaration() ) { | |
| 188 | - $message .= " {$this->wpAdapter->__( 'Customs declaration has to be filled in order detail.', 'packeta' )}"; | |
| 189 | - } | |
| 190 | - | |
| 191 | - return $message; | |
| 192 | 159 | } |
| 193 | 160 | } |