abterphp /
contact
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Contact\Form\Factory; |
||
| 6 | |||
| 7 | use AbterPhp\Admin\Form\Factory\Base; |
||
| 8 | use AbterPhp\Framework\Constant\Html5; |
||
| 9 | use AbterPhp\Framework\Form\Container\FormGroup; |
||
| 10 | use AbterPhp\Framework\Form\Element\Input; |
||
| 11 | use AbterPhp\Framework\Form\Element\Textarea; |
||
| 12 | use AbterPhp\Framework\Form\Form; |
||
|
0 ignored issues
–
show
|
|||
| 13 | use AbterPhp\Framework\Form\IForm; |
||
| 14 | use AbterPhp\Framework\Form\Label\Label; |
||
| 15 | use AbterPhp\Framework\Html\Component\Button; |
||
| 16 | use Opulence\Framework\Http\CsrfTokenChecker; |
||
| 17 | use Opulence\Orm\IEntity; |
||
| 18 | |||
| 19 | class Contact extends Base |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 23 | * |
||
| 24 | * @param string $action |
||
| 25 | * @param string $method |
||
| 26 | * @param string $showUrl |
||
| 27 | * @param IEntity|null $entity |
||
| 28 | * |
||
| 29 | * @return Form |
||
| 30 | */ |
||
| 31 | public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm |
||
| 32 | { |
||
| 33 | $this->createForm($action, $method) |
||
| 34 | ->addDefaultElements() |
||
| 35 | ->addFromName() |
||
| 36 | ->addFromEmail() |
||
| 37 | ->addFromPhone() |
||
| 38 | ->addSubject() |
||
| 39 | ->addBody() |
||
| 40 | ->addSubmit(); |
||
| 41 | |||
| 42 | $form = $this->form; |
||
| 43 | |||
| 44 | $this->form = null; |
||
| 45 | |||
| 46 | return $form; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | protected function addDefaultElements(): Base |
||
| 53 | { |
||
| 54 | $name = CsrfTokenChecker::TOKEN_INPUT_NAME; |
||
| 55 | $value = (string)$this->session->get($name); |
||
| 56 | |||
| 57 | $attributes = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN]; |
||
| 58 | |||
| 59 | $this->form[] = new Input($name, $name, $value, [], $attributes); |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | protected function addFromName(): Contact |
||
| 68 | { |
||
| 69 | $input = new Input('from_name', 'from_name', ''); |
||
| 70 | $label = new Label('from_name', 'contact:fromName'); |
||
| 71 | |||
| 72 | $this->form[] = new FormGroup($input, $label, null); |
||
| 73 | |||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | protected function addFromEmail(): Contact |
||
| 81 | { |
||
| 82 | $input = new Input('from_email', 'from_email', '', [], [Html5::ATTR_TYPE => Input::TYPE_EMAIL]); |
||
| 83 | $label = new Label('from_email', 'contact:fromEmail'); |
||
| 84 | |||
| 85 | $this->form[] = new FormGroup($input, $label); |
||
| 86 | |||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return $this |
||
| 92 | */ |
||
| 93 | protected function addFromPhone(): Contact |
||
| 94 | { |
||
| 95 | $input = new Input('from_phone', 'from_phone', '', [], [Html5::ATTR_TYPE => Input::TYPE_TEL]); |
||
| 96 | $label = new Label('from_phone', 'contact:fromPhone'); |
||
| 97 | |||
| 98 | $this->form[] = new FormGroup($input, $label); |
||
| 99 | |||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | protected function addSubject(): Contact |
||
| 107 | { |
||
| 108 | $input = new Input( |
||
| 109 | 'subject', |
||
| 110 | 'subject', |
||
| 111 | '' |
||
| 112 | ); |
||
| 113 | $label = new Label('subject', 'contact:subject'); |
||
| 114 | |||
| 115 | $this->form[] = new FormGroup($input, $label); |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | protected function addBody(): Contact |
||
| 124 | { |
||
| 125 | $input = new Textarea('body', 'body', '', [], [Html5::ATTR_ROWS => '15']); |
||
| 126 | $label = new Label('body', 'contact:body'); |
||
| 127 | |||
| 128 | $this->form[] = new FormGroup($input, $label); |
||
| 129 | |||
| 130 | return $this; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | protected function addSubmit(): Contact |
||
| 137 | { |
||
| 138 | $content = $this->translator->translate('contact:submit'); |
||
| 139 | |||
| 140 | $this->form[] = new Button($content, [Button::INTENT_PRIMARY]); |
||
| 141 | |||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | } |
||
| 145 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: