| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class FormFactory |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use Packetery\Module\Options\OptionsProvider; |
| 13 |
use Packetery\Nette\Forms\Container; |
| 14 |
use Packetery\Nette\Forms\Controls\TextInput; |
| 15 |
use Packetery\Nette\Forms\Form; |
| 16 |
use Packetery\Nette\Forms\Validator; |
| 17 |
use Packetery\Nette\Http\Request; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class FormFactory |
| 21 |
* |
| 22 |
* @package Packetery |
| 23 |
*/ |
| 24 |
class FormFactory { |
| 25 |
/** |
| 26 |
* @var Request |
| 27 |
*/ |
| 28 |
private $request; |
| 29 |
|
| 30 |
public function __construct( Request $request ) { |
| 31 |
add_action( |
| 32 |
'init', |
| 33 |
function () { |
| 34 |
// translators: keep %d placeholder intact. |
| 35 |
Validator::$messages[ Form::MIN ] = __( 'Please enter value greater than or equal to %d', 'packeta' ); |
| 36 |
// translators: keep %d placeholder intact. |
| 37 |
Validator::$messages[ Form::MAX ] = __( 'Please enter value less than or equal to %d', 'packeta' ); |
| 38 |
|
| 39 |
Validator::$messages[ Form::INTEGER ] = __( 'Enter valid number', 'packeta' ); |
| 40 |
Validator::$messages[ Form::FLOAT ] = __( 'Enter valid number', 'packeta' ); |
| 41 |
Validator::$messages[ Form::FILLED ] = __( 'This field is required', 'packeta' ); |
| 42 |
// translators: %d is number of characters. |
| 43 |
Validator::$messages[ Form::LENGTH ] = __( 'Please enter exactly %d characters', 'packeta' ); |
| 44 |
// translators: %d is number of characters. |
| 45 |
Validator::$messages[ Form::MAX_LENGTH ] = __( 'Please enter max %d characters', 'packeta' ); |
| 46 |
}, |
| 47 |
11 |
| 48 |
); |
| 49 |
$this->request = $request; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates Form |
| 54 |
* |
| 55 |
* @param string|null $name Form name. |
| 56 |
* |
| 57 |
* @return Form |
| 58 |
*/ |
| 59 |
public function create( ?string $name = null ): Form { |
| 60 |
$form = new Form( $name ); |
| 61 |
$form->httpRequest = $this->request; |
| 62 |
$form->allowCrossOrigin(); |
| 63 |
|
| 64 |
return $form; |
| 65 |
} |
| 66 |
|
| 67 |
public function addDimension( Container $container, string $name, string $label, string $unit ): TextInput { |
| 68 |
$numberInputType = $this->getNumType( $unit ); |
| 69 |
|
| 70 |
switch ( $numberInputType ) { |
| 71 |
case Form::INTEGER: |
| 72 |
$numberInput = $container->addInteger( $name, sprintf( '%s (%s)', $label, $unit ) ); |
| 73 |
|
| 74 |
break; |
| 75 |
case Form::FLOAT: |
| 76 |
$numberInput = $container->addText( $name, sprintf( '%s (%s)', $label, $unit ) )->addRule( Form::FLOAT ); |
| 77 |
|
| 78 |
break; |
| 79 |
default: |
| 80 |
throw new \InvalidArgumentException( 'Unsupported number input type: ' . $numberInputType ); |
| 81 |
} |
| 82 |
|
| 83 |
$numberInput |
| 84 |
->setHtmlType( 'number' ) |
| 85 |
->setHtmlAttribute( 'step', 'any' ) |
| 86 |
->addRule( Form::MIN, null, $this->setMinValue( $unit ) ); |
| 87 |
|
| 88 |
return $numberInput; |
| 89 |
} |
| 90 |
|
| 91 |
private function getNumType( string $unit ): string { |
| 92 |
if ( $unit === OptionsProvider::DIMENSIONS_UNIT_CM ) { |
| 93 |
return Form::FLOAT; |
| 94 |
} |
| 95 |
|
| 96 |
return Form::INTEGER; |
| 97 |
} |
| 98 |
|
| 99 |
private function setMinValue( string $unit ): float { |
| 100 |
if ( $unit === OptionsProvider::DIMENSIONS_UNIT_CM ) { |
| 101 |
return 0.1; |
| 102 |
} |
| 103 |
|
| 104 |
return 1; |
| 105 |
} |
| 106 |
} |
| 107 |
|