@@ -15,110 +15,110 @@ |
||
| 15 | 15 | class InputOptions |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var JsonDataHandler |
|
| 20 | - */ |
|
| 21 | - private $json_data_handler; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Options for ENUM type inputs like checkboxes, radio buttons, select inputs, etc |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $options; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * HelpText constructor. |
|
| 33 | - * |
|
| 34 | - * @param JsonDataHandler $json_data_handler |
|
| 35 | - * @param array $options |
|
| 36 | - */ |
|
| 37 | - public function __construct(JsonDataHandler $json_data_handler, array $options) |
|
| 38 | - { |
|
| 39 | - $this->json_data_handler = $json_data_handler; |
|
| 40 | - $this->setOptions($options); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @param string $json |
|
| 46 | - * @return InputOptions |
|
| 47 | - */ |
|
| 48 | - public static function fromJson(string $json): InputOptions |
|
| 49 | - { |
|
| 50 | - $json_data_handler = new JsonDataHandler(); |
|
| 51 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_ARRAY); |
|
| 52 | - $data = $json_data_handler->decodeJson($json); |
|
| 53 | - $options = $data['options'] ?? []; |
|
| 54 | - return new InputOptions($json_data_handler, $options); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @return array |
|
| 60 | - */ |
|
| 61 | - public function toArray(): array |
|
| 62 | - { |
|
| 63 | - return ['options' => $this->options]; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return string |
|
| 69 | - */ |
|
| 70 | - public function toJson(): string |
|
| 71 | - { |
|
| 72 | - return $this->json_data_handler->encodeData($this->toArray()); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * an array where keys are option values and values are option labels |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function options(): array |
|
| 82 | - { |
|
| 83 | - return $this->options; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param int|float|string $option_value |
|
| 89 | - * @param int|float|string $display_text |
|
| 90 | - */ |
|
| 91 | - public function addOption($option_value, $display_text): void |
|
| 92 | - { |
|
| 93 | - $option_value = sanitize_key($option_value); |
|
| 94 | - if (! isset($this->options[ $option_value ])) { |
|
| 95 | - $this->options[ $option_value ] = sanitize_text_field($display_text); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param int|float|string $option_value |
|
| 102 | - */ |
|
| 103 | - public function removeOption($option_value): void |
|
| 104 | - { |
|
| 105 | - $option_value = sanitize_key($option_value); |
|
| 106 | - unset($this->options[ $option_value ]); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param array $options an array where keys are option values and values are option labels |
|
| 112 | - */ |
|
| 113 | - public function setOptions(array $options): void |
|
| 114 | - { |
|
| 115 | - // grab all of the keys and sanitize those for use as option values |
|
| 116 | - $keys = array_keys($options); |
|
| 117 | - $keys = array_map('sanitize_key', $keys); |
|
| 118 | - // grab all of the array values and sanitize those for use as option display text |
|
| 119 | - $values = array_values($options); |
|
| 120 | - $values = array_map('sanitize_text_field', $values); |
|
| 121 | - // recombine sanitized values |
|
| 122 | - $this->options = array_combine($keys, $values); |
|
| 123 | - } |
|
| 18 | + /** |
|
| 19 | + * @var JsonDataHandler |
|
| 20 | + */ |
|
| 21 | + private $json_data_handler; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Options for ENUM type inputs like checkboxes, radio buttons, select inputs, etc |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $options; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * HelpText constructor. |
|
| 33 | + * |
|
| 34 | + * @param JsonDataHandler $json_data_handler |
|
| 35 | + * @param array $options |
|
| 36 | + */ |
|
| 37 | + public function __construct(JsonDataHandler $json_data_handler, array $options) |
|
| 38 | + { |
|
| 39 | + $this->json_data_handler = $json_data_handler; |
|
| 40 | + $this->setOptions($options); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @param string $json |
|
| 46 | + * @return InputOptions |
|
| 47 | + */ |
|
| 48 | + public static function fromJson(string $json): InputOptions |
|
| 49 | + { |
|
| 50 | + $json_data_handler = new JsonDataHandler(); |
|
| 51 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_ARRAY); |
|
| 52 | + $data = $json_data_handler->decodeJson($json); |
|
| 53 | + $options = $data['options'] ?? []; |
|
| 54 | + return new InputOptions($json_data_handler, $options); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @return array |
|
| 60 | + */ |
|
| 61 | + public function toArray(): array |
|
| 62 | + { |
|
| 63 | + return ['options' => $this->options]; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return string |
|
| 69 | + */ |
|
| 70 | + public function toJson(): string |
|
| 71 | + { |
|
| 72 | + return $this->json_data_handler->encodeData($this->toArray()); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * an array where keys are option values and values are option labels |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function options(): array |
|
| 82 | + { |
|
| 83 | + return $this->options; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param int|float|string $option_value |
|
| 89 | + * @param int|float|string $display_text |
|
| 90 | + */ |
|
| 91 | + public function addOption($option_value, $display_text): void |
|
| 92 | + { |
|
| 93 | + $option_value = sanitize_key($option_value); |
|
| 94 | + if (! isset($this->options[ $option_value ])) { |
|
| 95 | + $this->options[ $option_value ] = sanitize_text_field($display_text); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param int|float|string $option_value |
|
| 102 | + */ |
|
| 103 | + public function removeOption($option_value): void |
|
| 104 | + { |
|
| 105 | + $option_value = sanitize_key($option_value); |
|
| 106 | + unset($this->options[ $option_value ]); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param array $options an array where keys are option values and values are option labels |
|
| 112 | + */ |
|
| 113 | + public function setOptions(array $options): void |
|
| 114 | + { |
|
| 115 | + // grab all of the keys and sanitize those for use as option values |
|
| 116 | + $keys = array_keys($options); |
|
| 117 | + $keys = array_map('sanitize_key', $keys); |
|
| 118 | + // grab all of the array values and sanitize those for use as option display text |
|
| 119 | + $values = array_values($options); |
|
| 120 | + $values = array_map('sanitize_text_field', $values); |
|
| 121 | + // recombine sanitized values |
|
| 122 | + $this->options = array_combine($keys, $values); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -15,141 +15,141 @@ |
||
| 15 | 15 | class HelpText |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var JsonDataHandler |
|
| 20 | - */ |
|
| 21 | - private $json_data_handler; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Additional text displayed alongside a form input to assist users with completing the form. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - private $helpText; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Custom HTML classes to be applied to this form input's help text. |
|
| 32 | - * |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - private $htmlClasses; |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * HelpText constructor. |
|
| 40 | - * |
|
| 41 | - * @param JsonDataHandler $json_data_handler |
|
| 42 | - * @param array $htmlClass |
|
| 43 | - * @param string $helpText |
|
| 44 | - */ |
|
| 45 | - public function __construct( |
|
| 46 | - JsonDataHandler $json_data_handler, |
|
| 47 | - array $htmlClass, |
|
| 48 | - string $helpText |
|
| 49 | - ) { |
|
| 50 | - $this->json_data_handler = $json_data_handler; |
|
| 51 | - $this->setHtmlClasses($htmlClass); |
|
| 52 | - $this->setHelpText($helpText); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param string $json |
|
| 58 | - * @return HelpText |
|
| 59 | - */ |
|
| 60 | - public static function fromJson(string $json): HelpText |
|
| 61 | - { |
|
| 62 | - $json_data_handler = new JsonDataHandler(); |
|
| 63 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 64 | - $data = $json_data_handler->decodeJson($json); |
|
| 65 | - $htmlClass = (array) $data->htmlClass ?? []; |
|
| 66 | - $helpText = $data->helpText ?? ''; |
|
| 67 | - return new HelpText($json_data_handler, $htmlClass, $helpText); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function toArray(): array |
|
| 75 | - { |
|
| 76 | - return [ |
|
| 77 | - 'helpText' => $this->helpText, |
|
| 78 | - 'htmlClass' => $this->htmlClasses(), |
|
| 79 | - ]; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function toJson(): string |
|
| 87 | - { |
|
| 88 | - return $this->json_data_handler->encodeData($this->toArray()); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Input label displayed on public forms, ie: the actual question text. |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function helpText(): string |
|
| 98 | - { |
|
| 99 | - return $this->helpText; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $helpText |
|
| 105 | - */ |
|
| 106 | - public function setHelpText(string $helpText): void |
|
| 107 | - { |
|
| 108 | - $this->helpText = sanitize_text_field($helpText); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Custom HTML classes to be applied to this form input's help text. |
|
| 114 | - * returns a concatenated string unless $as_array is set to true |
|
| 115 | - * |
|
| 116 | - * @return array|string |
|
| 117 | - */ |
|
| 118 | - public function htmlClasses($as_array = false) |
|
| 119 | - { |
|
| 120 | - return $as_array |
|
| 121 | - ? $this->htmlClasses |
|
| 122 | - : implode(' ', $this->htmlClasses); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $htmlClass |
|
| 128 | - */ |
|
| 129 | - public function addHtmlClass(string $htmlClass): void |
|
| 130 | - { |
|
| 131 | - $htmlClass = sanitize_key($htmlClass); |
|
| 132 | - if (! in_array($htmlClass, $this->htmlClasses, true)) { |
|
| 133 | - $this->htmlClasses[] = $htmlClass; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param string $htmlClass |
|
| 140 | - */ |
|
| 141 | - public function removeHtmlClass(string $htmlClass): void |
|
| 142 | - { |
|
| 143 | - $htmlClass = sanitize_key($htmlClass); |
|
| 144 | - unset($this->htmlClasses[ $htmlClass ]); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @param array $htmlClasses |
|
| 150 | - */ |
|
| 151 | - public function setHtmlClasses(array $htmlClasses): void |
|
| 152 | - { |
|
| 153 | - $this->htmlClasses = array_map('sanitize_key', $htmlClasses); |
|
| 154 | - } |
|
| 18 | + /** |
|
| 19 | + * @var JsonDataHandler |
|
| 20 | + */ |
|
| 21 | + private $json_data_handler; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Additional text displayed alongside a form input to assist users with completing the form. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + private $helpText; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Custom HTML classes to be applied to this form input's help text. |
|
| 32 | + * |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + private $htmlClasses; |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * HelpText constructor. |
|
| 40 | + * |
|
| 41 | + * @param JsonDataHandler $json_data_handler |
|
| 42 | + * @param array $htmlClass |
|
| 43 | + * @param string $helpText |
|
| 44 | + */ |
|
| 45 | + public function __construct( |
|
| 46 | + JsonDataHandler $json_data_handler, |
|
| 47 | + array $htmlClass, |
|
| 48 | + string $helpText |
|
| 49 | + ) { |
|
| 50 | + $this->json_data_handler = $json_data_handler; |
|
| 51 | + $this->setHtmlClasses($htmlClass); |
|
| 52 | + $this->setHelpText($helpText); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param string $json |
|
| 58 | + * @return HelpText |
|
| 59 | + */ |
|
| 60 | + public static function fromJson(string $json): HelpText |
|
| 61 | + { |
|
| 62 | + $json_data_handler = new JsonDataHandler(); |
|
| 63 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 64 | + $data = $json_data_handler->decodeJson($json); |
|
| 65 | + $htmlClass = (array) $data->htmlClass ?? []; |
|
| 66 | + $helpText = $data->helpText ?? ''; |
|
| 67 | + return new HelpText($json_data_handler, $htmlClass, $helpText); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function toArray(): array |
|
| 75 | + { |
|
| 76 | + return [ |
|
| 77 | + 'helpText' => $this->helpText, |
|
| 78 | + 'htmlClass' => $this->htmlClasses(), |
|
| 79 | + ]; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function toJson(): string |
|
| 87 | + { |
|
| 88 | + return $this->json_data_handler->encodeData($this->toArray()); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Input label displayed on public forms, ie: the actual question text. |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function helpText(): string |
|
| 98 | + { |
|
| 99 | + return $this->helpText; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $helpText |
|
| 105 | + */ |
|
| 106 | + public function setHelpText(string $helpText): void |
|
| 107 | + { |
|
| 108 | + $this->helpText = sanitize_text_field($helpText); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Custom HTML classes to be applied to this form input's help text. |
|
| 114 | + * returns a concatenated string unless $as_array is set to true |
|
| 115 | + * |
|
| 116 | + * @return array|string |
|
| 117 | + */ |
|
| 118 | + public function htmlClasses($as_array = false) |
|
| 119 | + { |
|
| 120 | + return $as_array |
|
| 121 | + ? $this->htmlClasses |
|
| 122 | + : implode(' ', $this->htmlClasses); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $htmlClass |
|
| 128 | + */ |
|
| 129 | + public function addHtmlClass(string $htmlClass): void |
|
| 130 | + { |
|
| 131 | + $htmlClass = sanitize_key($htmlClass); |
|
| 132 | + if (! in_array($htmlClass, $this->htmlClasses, true)) { |
|
| 133 | + $this->htmlClasses[] = $htmlClass; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param string $htmlClass |
|
| 140 | + */ |
|
| 141 | + public function removeHtmlClass(string $htmlClass): void |
|
| 142 | + { |
|
| 143 | + $htmlClass = sanitize_key($htmlClass); |
|
| 144 | + unset($this->htmlClasses[ $htmlClass ]); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @param array $htmlClasses |
|
| 150 | + */ |
|
| 151 | + public function setHtmlClasses(array $htmlClasses): void |
|
| 152 | + { |
|
| 153 | + $this->htmlClasses = array_map('sanitize_key', $htmlClasses); |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -15,110 +15,110 @@ |
||
| 15 | 15 | class Required |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var JsonDataHandler |
|
| 20 | - */ |
|
| 21 | - private $json_data_handler; |
|
| 22 | - |
|
| 23 | - /** Whether or not the input must be supplied with a value in order to complete the form. |
|
| 24 | - * |
|
| 25 | - * @var bool |
|
| 26 | - */ |
|
| 27 | - private $required; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Custom validation text displayed alongside a required form input to assist users with completing the form. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private $validationText; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Required constructor. |
|
| 39 | - * |
|
| 40 | - * @param JsonDataHandler $json_data_handler |
|
| 41 | - * @param bool $required |
|
| 42 | - * @param string $validationText |
|
| 43 | - */ |
|
| 44 | - public function __construct(JsonDataHandler $json_data_handler, bool $required, string $validationText) |
|
| 45 | - { |
|
| 46 | - $this->json_data_handler = $json_data_handler; |
|
| 47 | - $this->setRequired($required); |
|
| 48 | - $this->setValidationText($validationText); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param string $json |
|
| 54 | - * @return Required |
|
| 55 | - */ |
|
| 56 | - public static function fromJson(string $json): Required |
|
| 57 | - { |
|
| 58 | - $json_data_handler = new JsonDataHandler(); |
|
| 59 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 60 | - $data = $json_data_handler->decodeJson($json); |
|
| 61 | - $required = $data->required ?? false; |
|
| 62 | - $validationText = $data->validationText ?? ''; |
|
| 63 | - return new Required($json_data_handler, $required, $validationText); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public function toArray(): array |
|
| 71 | - { |
|
| 72 | - return [ |
|
| 73 | - 'required' => $this->required, |
|
| 74 | - 'validationText' => $this->validationText, |
|
| 75 | - ]; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public function toJson(): string |
|
| 83 | - { |
|
| 84 | - return $this->json_data_handler->encodeData($this->toArray()); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function isRequired(): ?bool |
|
| 94 | - { |
|
| 95 | - return $this->required; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param bool|int|string $required |
|
| 101 | - */ |
|
| 102 | - public function setRequired($required = true) |
|
| 103 | - { |
|
| 104 | - $this->required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - public function validationText(): ?string |
|
| 112 | - { |
|
| 113 | - return $this->validationText; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param string $validationText |
|
| 119 | - */ |
|
| 120 | - public function setValidationText(string $validationText) |
|
| 121 | - { |
|
| 122 | - $this->validationText = sanitize_text_field($validationText); |
|
| 123 | - } |
|
| 18 | + /** |
|
| 19 | + * @var JsonDataHandler |
|
| 20 | + */ |
|
| 21 | + private $json_data_handler; |
|
| 22 | + |
|
| 23 | + /** Whether or not the input must be supplied with a value in order to complete the form. |
|
| 24 | + * |
|
| 25 | + * @var bool |
|
| 26 | + */ |
|
| 27 | + private $required; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Custom validation text displayed alongside a required form input to assist users with completing the form. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private $validationText; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Required constructor. |
|
| 39 | + * |
|
| 40 | + * @param JsonDataHandler $json_data_handler |
|
| 41 | + * @param bool $required |
|
| 42 | + * @param string $validationText |
|
| 43 | + */ |
|
| 44 | + public function __construct(JsonDataHandler $json_data_handler, bool $required, string $validationText) |
|
| 45 | + { |
|
| 46 | + $this->json_data_handler = $json_data_handler; |
|
| 47 | + $this->setRequired($required); |
|
| 48 | + $this->setValidationText($validationText); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param string $json |
|
| 54 | + * @return Required |
|
| 55 | + */ |
|
| 56 | + public static function fromJson(string $json): Required |
|
| 57 | + { |
|
| 58 | + $json_data_handler = new JsonDataHandler(); |
|
| 59 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 60 | + $data = $json_data_handler->decodeJson($json); |
|
| 61 | + $required = $data->required ?? false; |
|
| 62 | + $validationText = $data->validationText ?? ''; |
|
| 63 | + return new Required($json_data_handler, $required, $validationText); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public function toArray(): array |
|
| 71 | + { |
|
| 72 | + return [ |
|
| 73 | + 'required' => $this->required, |
|
| 74 | + 'validationText' => $this->validationText, |
|
| 75 | + ]; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public function toJson(): string |
|
| 83 | + { |
|
| 84 | + return $this->json_data_handler->encodeData($this->toArray()); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function isRequired(): ?bool |
|
| 94 | + { |
|
| 95 | + return $this->required; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param bool|int|string $required |
|
| 101 | + */ |
|
| 102 | + public function setRequired($required = true) |
|
| 103 | + { |
|
| 104 | + $this->required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + public function validationText(): ?string |
|
| 112 | + { |
|
| 113 | + return $this->validationText; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param string $validationText |
|
| 119 | + */ |
|
| 120 | + public function setValidationText(string $validationText) |
|
| 121 | + { |
|
| 122 | + $this->validationText = sanitize_text_field($validationText); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -17,182 +17,182 @@ |
||
| 17 | 17 | class Attributes |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var JsonDataHandler |
|
| 22 | - */ |
|
| 23 | - private $json_data_handler; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var InputTypes |
|
| 27 | - */ |
|
| 28 | - private $input_types; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $attributes = []; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $attribute_types = [ |
|
| 39 | - 'accept' => 'string', |
|
| 40 | - 'accesskey' => 'string', |
|
| 41 | - 'alt' => 'string', |
|
| 42 | - 'autocomplete' => 'bool', |
|
| 43 | - 'autofocus' => 'bool', |
|
| 44 | - 'checked' => 'bool', |
|
| 45 | - // Custom HTML classes to be applied to the form input's container |
|
| 46 | - 'class' => 'string', |
|
| 47 | - 'contenteditable' => 'bool', |
|
| 48 | - 'dir' => 'string', |
|
| 49 | - 'disabled' => 'bool', |
|
| 50 | - 'height' => 'string', |
|
| 51 | - 'hidden' => 'bool', |
|
| 52 | - 'id' => 'string', |
|
| 53 | - 'list' => 'string', |
|
| 54 | - // Maximum numeric value allowed for form input answer. |
|
| 55 | - 'max' => 'int', |
|
| 56 | - // Maximum characters allowed for form input answer. |
|
| 57 | - 'maxlength' => 'int', |
|
| 58 | - // Minimum numeric value allowed for form input answer. |
|
| 59 | - 'min' => 'int', |
|
| 60 | - 'multiple' => 'bool', |
|
| 61 | - 'name' => 'string', |
|
| 62 | - 'pattern' => 'string', |
|
| 63 | - // Example text displayed within an input to assist users with completing the form. |
|
| 64 | - 'placeholder' => 'string', |
|
| 65 | - 'readonly' => 'bool', |
|
| 66 | - 'size' => 'int', |
|
| 67 | - 'spellcheck' => 'bool', |
|
| 68 | - 'step' => 'float', |
|
| 69 | - 'style' => 'string', |
|
| 70 | - 'tabindex' => 'int', |
|
| 71 | - 'title' => 'string', |
|
| 72 | - 'translate' => 'bool', |
|
| 73 | - // Form input type. Values correspond to the Input::TYPE_* constants. |
|
| 74 | - 'type' => 'string', |
|
| 75 | - 'value' => 'string', |
|
| 76 | - 'width' => 'string', |
|
| 77 | - ]; |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Attributes constructor. |
|
| 82 | - * |
|
| 83 | - * @param JsonDataHandler $json_data_handler |
|
| 84 | - * @param InputTypes $input_types |
|
| 85 | - * @param array $attributes |
|
| 86 | - */ |
|
| 87 | - public function __construct(JsonDataHandler $json_data_handler, array $attributes, InputTypes $input_types) |
|
| 88 | - { |
|
| 89 | - $this->json_data_handler = $json_data_handler; |
|
| 90 | - $this->input_types = $input_types; |
|
| 91 | - $this->setAttributes($attributes); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param string $json |
|
| 97 | - * @return Attributes |
|
| 98 | - */ |
|
| 99 | - public static function fromJson(string $json): Attributes |
|
| 100 | - { |
|
| 101 | - $json_data_handler = new JsonDataHandler(); |
|
| 102 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 103 | - $attributes = $json_data_handler->decodeJson($json); |
|
| 104 | - return LoaderFactory::getNew(Attributes::class, [ $json_data_handler, $attributes ]); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @return array |
|
| 110 | - */ |
|
| 111 | - public function toArray(): array |
|
| 112 | - { |
|
| 113 | - return $this->attributes(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function toJson(): string |
|
| 121 | - { |
|
| 122 | - return $this->json_data_handler->encodeData($this->attributes()); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $attribute |
|
| 128 | - * @param bool|float|int|string $value |
|
| 129 | - * @return bool|float|int|string |
|
| 130 | - */ |
|
| 131 | - private function sanitize(string $attribute, $value) |
|
| 132 | - { |
|
| 133 | - if ($attribute === 'type') { |
|
| 134 | - $valid_types = $this->input_types->validTypeOptions(); |
|
| 135 | - return in_array($value, $valid_types, true) ?: Text::TYPE_TEXT; |
|
| 136 | - } |
|
| 137 | - $type = $this->attribute_types[ $attribute ] ?? 'string'; |
|
| 138 | - switch ($type) { |
|
| 139 | - case 'bool': |
|
| 140 | - return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 141 | - case 'int': |
|
| 142 | - return filter_var($value, FILTER_SANITIZE_NUMBER_INT); |
|
| 143 | - case 'float': |
|
| 144 | - return filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); |
|
| 145 | - case 'string': |
|
| 146 | - default: |
|
| 147 | - return filter_var( |
|
| 148 | - $value, |
|
| 149 | - FILTER_SANITIZE_STRING, |
|
| 150 | - FILTER_FLAG_ENCODE_LOW | FILTER_FLAG_ENCODE_HIGH | FILTER_FLAG_ENCODE_AMP |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Custom HTML classes to be applied to this form input's help text. |
|
| 158 | - * returns a concatenated string unless $as_array is set to true |
|
| 159 | - * |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function attributes(): array |
|
| 163 | - { |
|
| 164 | - return $this->attributes; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @param string $attribute |
|
| 170 | - * @param bool|float|int|string $value |
|
| 171 | - */ |
|
| 172 | - public function addAttribute(string $attribute, $value): void |
|
| 173 | - { |
|
| 174 | - if (array_key_exists($attribute, $this->attributes)) { |
|
| 175 | - $this->attributes[ $attribute ] = $this->sanitize($attribute, $value); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param string $attribute |
|
| 182 | - */ |
|
| 183 | - public function removeAttribute(string $attribute): void |
|
| 184 | - { |
|
| 185 | - unset($this->attributes[ $attribute ]); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param array $attributes array where keys are the attribute name and values are the attribute's value |
|
| 191 | - */ |
|
| 192 | - public function setAttributes(array $attributes): void |
|
| 193 | - { |
|
| 194 | - foreach ($attributes as $attribute => $value) { |
|
| 195 | - $this->addAttribute($attribute, $value); |
|
| 196 | - } |
|
| 197 | - } |
|
| 20 | + /** |
|
| 21 | + * @var JsonDataHandler |
|
| 22 | + */ |
|
| 23 | + private $json_data_handler; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var InputTypes |
|
| 27 | + */ |
|
| 28 | + private $input_types; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $attributes = []; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $attribute_types = [ |
|
| 39 | + 'accept' => 'string', |
|
| 40 | + 'accesskey' => 'string', |
|
| 41 | + 'alt' => 'string', |
|
| 42 | + 'autocomplete' => 'bool', |
|
| 43 | + 'autofocus' => 'bool', |
|
| 44 | + 'checked' => 'bool', |
|
| 45 | + // Custom HTML classes to be applied to the form input's container |
|
| 46 | + 'class' => 'string', |
|
| 47 | + 'contenteditable' => 'bool', |
|
| 48 | + 'dir' => 'string', |
|
| 49 | + 'disabled' => 'bool', |
|
| 50 | + 'height' => 'string', |
|
| 51 | + 'hidden' => 'bool', |
|
| 52 | + 'id' => 'string', |
|
| 53 | + 'list' => 'string', |
|
| 54 | + // Maximum numeric value allowed for form input answer. |
|
| 55 | + 'max' => 'int', |
|
| 56 | + // Maximum characters allowed for form input answer. |
|
| 57 | + 'maxlength' => 'int', |
|
| 58 | + // Minimum numeric value allowed for form input answer. |
|
| 59 | + 'min' => 'int', |
|
| 60 | + 'multiple' => 'bool', |
|
| 61 | + 'name' => 'string', |
|
| 62 | + 'pattern' => 'string', |
|
| 63 | + // Example text displayed within an input to assist users with completing the form. |
|
| 64 | + 'placeholder' => 'string', |
|
| 65 | + 'readonly' => 'bool', |
|
| 66 | + 'size' => 'int', |
|
| 67 | + 'spellcheck' => 'bool', |
|
| 68 | + 'step' => 'float', |
|
| 69 | + 'style' => 'string', |
|
| 70 | + 'tabindex' => 'int', |
|
| 71 | + 'title' => 'string', |
|
| 72 | + 'translate' => 'bool', |
|
| 73 | + // Form input type. Values correspond to the Input::TYPE_* constants. |
|
| 74 | + 'type' => 'string', |
|
| 75 | + 'value' => 'string', |
|
| 76 | + 'width' => 'string', |
|
| 77 | + ]; |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Attributes constructor. |
|
| 82 | + * |
|
| 83 | + * @param JsonDataHandler $json_data_handler |
|
| 84 | + * @param InputTypes $input_types |
|
| 85 | + * @param array $attributes |
|
| 86 | + */ |
|
| 87 | + public function __construct(JsonDataHandler $json_data_handler, array $attributes, InputTypes $input_types) |
|
| 88 | + { |
|
| 89 | + $this->json_data_handler = $json_data_handler; |
|
| 90 | + $this->input_types = $input_types; |
|
| 91 | + $this->setAttributes($attributes); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param string $json |
|
| 97 | + * @return Attributes |
|
| 98 | + */ |
|
| 99 | + public static function fromJson(string $json): Attributes |
|
| 100 | + { |
|
| 101 | + $json_data_handler = new JsonDataHandler(); |
|
| 102 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 103 | + $attributes = $json_data_handler->decodeJson($json); |
|
| 104 | + return LoaderFactory::getNew(Attributes::class, [ $json_data_handler, $attributes ]); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @return array |
|
| 110 | + */ |
|
| 111 | + public function toArray(): array |
|
| 112 | + { |
|
| 113 | + return $this->attributes(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function toJson(): string |
|
| 121 | + { |
|
| 122 | + return $this->json_data_handler->encodeData($this->attributes()); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $attribute |
|
| 128 | + * @param bool|float|int|string $value |
|
| 129 | + * @return bool|float|int|string |
|
| 130 | + */ |
|
| 131 | + private function sanitize(string $attribute, $value) |
|
| 132 | + { |
|
| 133 | + if ($attribute === 'type') { |
|
| 134 | + $valid_types = $this->input_types->validTypeOptions(); |
|
| 135 | + return in_array($value, $valid_types, true) ?: Text::TYPE_TEXT; |
|
| 136 | + } |
|
| 137 | + $type = $this->attribute_types[ $attribute ] ?? 'string'; |
|
| 138 | + switch ($type) { |
|
| 139 | + case 'bool': |
|
| 140 | + return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 141 | + case 'int': |
|
| 142 | + return filter_var($value, FILTER_SANITIZE_NUMBER_INT); |
|
| 143 | + case 'float': |
|
| 144 | + return filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); |
|
| 145 | + case 'string': |
|
| 146 | + default: |
|
| 147 | + return filter_var( |
|
| 148 | + $value, |
|
| 149 | + FILTER_SANITIZE_STRING, |
|
| 150 | + FILTER_FLAG_ENCODE_LOW | FILTER_FLAG_ENCODE_HIGH | FILTER_FLAG_ENCODE_AMP |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Custom HTML classes to be applied to this form input's help text. |
|
| 158 | + * returns a concatenated string unless $as_array is set to true |
|
| 159 | + * |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function attributes(): array |
|
| 163 | + { |
|
| 164 | + return $this->attributes; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @param string $attribute |
|
| 170 | + * @param bool|float|int|string $value |
|
| 171 | + */ |
|
| 172 | + public function addAttribute(string $attribute, $value): void |
|
| 173 | + { |
|
| 174 | + if (array_key_exists($attribute, $this->attributes)) { |
|
| 175 | + $this->attributes[ $attribute ] = $this->sanitize($attribute, $value); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param string $attribute |
|
| 182 | + */ |
|
| 183 | + public function removeAttribute(string $attribute): void |
|
| 184 | + { |
|
| 185 | + unset($this->attributes[ $attribute ]); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param array $attributes array where keys are the attribute name and values are the attribute's value |
|
| 191 | + */ |
|
| 192 | + public function setAttributes(array $attributes): void |
|
| 193 | + { |
|
| 194 | + foreach ($attributes as $attribute => $value) { |
|
| 195 | + $this->addAttribute($attribute, $value); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | 198 | } |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | $json_data_handler = new JsonDataHandler(); |
| 102 | 102 | $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
| 103 | 103 | $attributes = $json_data_handler->decodeJson($json); |
| 104 | - return LoaderFactory::getNew(Attributes::class, [ $json_data_handler, $attributes ]); |
|
| 104 | + return LoaderFactory::getNew(Attributes::class, [$json_data_handler, $attributes]); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | $valid_types = $this->input_types->validTypeOptions(); |
| 135 | 135 | return in_array($value, $valid_types, true) ?: Text::TYPE_TEXT; |
| 136 | 136 | } |
| 137 | - $type = $this->attribute_types[ $attribute ] ?? 'string'; |
|
| 137 | + $type = $this->attribute_types[$attribute] ?? 'string'; |
|
| 138 | 138 | switch ($type) { |
| 139 | 139 | case 'bool': |
| 140 | 140 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | public function addAttribute(string $attribute, $value): void |
| 173 | 173 | { |
| 174 | 174 | if (array_key_exists($attribute, $this->attributes)) { |
| 175 | - $this->attributes[ $attribute ] = $this->sanitize($attribute, $value); |
|
| 175 | + $this->attributes[$attribute] = $this->sanitize($attribute, $value); |
|
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | public function removeAttribute(string $attribute): void |
| 184 | 184 | { |
| 185 | - unset($this->attributes[ $attribute ]); |
|
| 185 | + unset($this->attributes[$attribute]); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | |
@@ -15,144 +15,144 @@ |
||
| 15 | 15 | class FormLabel |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var JsonDataHandler |
|
| 20 | - */ |
|
| 21 | - private $json_data_handler; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Input label displayed in the admin to help differentiate input from others |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - private $adminLabel; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Input label displayed on public forms, ie: the actual question text. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - private $publicLabel; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var bool |
|
| 39 | - */ |
|
| 40 | - private $showLabel; |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * FormLabel constructor. |
|
| 45 | - * |
|
| 46 | - * @param JsonDataHandler $json_data_handler |
|
| 47 | - * @param string $adminLabel |
|
| 48 | - * @param string $publicLabel |
|
| 49 | - * @param bool $showLabel |
|
| 50 | - */ |
|
| 51 | - public function __construct( |
|
| 52 | - JsonDataHandler $json_data_handler, |
|
| 53 | - string $adminLabel, |
|
| 54 | - string $publicLabel, |
|
| 55 | - bool $showLabel |
|
| 56 | - ) { |
|
| 57 | - $this->json_data_handler = $json_data_handler; |
|
| 58 | - $this->setAdminLabel($adminLabel); |
|
| 59 | - $this->setPublicLabel($publicLabel); |
|
| 60 | - $this->setShowLabel($showLabel); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param string $json |
|
| 66 | - * @return FormLabel |
|
| 67 | - */ |
|
| 68 | - public static function fromJson(string $json): FormLabel |
|
| 69 | - { |
|
| 70 | - $json_data_handler = new JsonDataHandler(); |
|
| 71 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 72 | - $data = $json_data_handler->decodeJson($json); |
|
| 73 | - $adminLabel = $data->adminLabel ?? ''; |
|
| 74 | - $publicLabel = $data->publicLabel ?? ''; |
|
| 75 | - $showLabel = $data->showLabel ?? false; |
|
| 76 | - return new FormLabel($json_data_handler, $adminLabel, $publicLabel, $showLabel); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - public function toArray(): array |
|
| 84 | - { |
|
| 85 | - return [ |
|
| 86 | - 'adminLabel' => $this->adminLabel, |
|
| 87 | - 'publicLabel' => $this->publicLabel, |
|
| 88 | - 'showLabel' => $this->showLabel, |
|
| 89 | - ]; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function toJson(): string |
|
| 97 | - { |
|
| 98 | - return $this->json_data_handler->encodeData($this->toArray()); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Input label displayed in the admin to help differentiate input from others |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function adminLabel(): string |
|
| 108 | - { |
|
| 109 | - return $this->adminLabel; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param string $adminLabel |
|
| 115 | - */ |
|
| 116 | - public function setAdminLabel(string $adminLabel): void |
|
| 117 | - { |
|
| 118 | - $this->adminLabel = sanitize_text_field($adminLabel); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Input label displayed on public forms, ie: the actual question text. |
|
| 124 | - * |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public function publicLabel(): string |
|
| 128 | - { |
|
| 129 | - return $this->publicLabel; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param string $publicLabel |
|
| 135 | - */ |
|
| 136 | - public function setPublicLabel(string $publicLabel): void |
|
| 137 | - { |
|
| 138 | - $this->publicLabel = sanitize_text_field($publicLabel); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return bool |
|
| 144 | - */ |
|
| 145 | - public function showLabel(): bool |
|
| 146 | - { |
|
| 147 | - return $this->showLabel; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param bool|int|string $showLabel |
|
| 153 | - */ |
|
| 154 | - public function setShowLabel($showLabel): void |
|
| 155 | - { |
|
| 156 | - $this->showLabel = filter_var($showLabel, FILTER_VALIDATE_BOOLEAN); |
|
| 157 | - } |
|
| 18 | + /** |
|
| 19 | + * @var JsonDataHandler |
|
| 20 | + */ |
|
| 21 | + private $json_data_handler; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Input label displayed in the admin to help differentiate input from others |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + private $adminLabel; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Input label displayed on public forms, ie: the actual question text. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + private $publicLabel; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var bool |
|
| 39 | + */ |
|
| 40 | + private $showLabel; |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * FormLabel constructor. |
|
| 45 | + * |
|
| 46 | + * @param JsonDataHandler $json_data_handler |
|
| 47 | + * @param string $adminLabel |
|
| 48 | + * @param string $publicLabel |
|
| 49 | + * @param bool $showLabel |
|
| 50 | + */ |
|
| 51 | + public function __construct( |
|
| 52 | + JsonDataHandler $json_data_handler, |
|
| 53 | + string $adminLabel, |
|
| 54 | + string $publicLabel, |
|
| 55 | + bool $showLabel |
|
| 56 | + ) { |
|
| 57 | + $this->json_data_handler = $json_data_handler; |
|
| 58 | + $this->setAdminLabel($adminLabel); |
|
| 59 | + $this->setPublicLabel($publicLabel); |
|
| 60 | + $this->setShowLabel($showLabel); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param string $json |
|
| 66 | + * @return FormLabel |
|
| 67 | + */ |
|
| 68 | + public static function fromJson(string $json): FormLabel |
|
| 69 | + { |
|
| 70 | + $json_data_handler = new JsonDataHandler(); |
|
| 71 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 72 | + $data = $json_data_handler->decodeJson($json); |
|
| 73 | + $adminLabel = $data->adminLabel ?? ''; |
|
| 74 | + $publicLabel = $data->publicLabel ?? ''; |
|
| 75 | + $showLabel = $data->showLabel ?? false; |
|
| 76 | + return new FormLabel($json_data_handler, $adminLabel, $publicLabel, $showLabel); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + public function toArray(): array |
|
| 84 | + { |
|
| 85 | + return [ |
|
| 86 | + 'adminLabel' => $this->adminLabel, |
|
| 87 | + 'publicLabel' => $this->publicLabel, |
|
| 88 | + 'showLabel' => $this->showLabel, |
|
| 89 | + ]; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function toJson(): string |
|
| 97 | + { |
|
| 98 | + return $this->json_data_handler->encodeData($this->toArray()); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Input label displayed in the admin to help differentiate input from others |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function adminLabel(): string |
|
| 108 | + { |
|
| 109 | + return $this->adminLabel; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param string $adminLabel |
|
| 115 | + */ |
|
| 116 | + public function setAdminLabel(string $adminLabel): void |
|
| 117 | + { |
|
| 118 | + $this->adminLabel = sanitize_text_field($adminLabel); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Input label displayed on public forms, ie: the actual question text. |
|
| 124 | + * |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public function publicLabel(): string |
|
| 128 | + { |
|
| 129 | + return $this->publicLabel; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param string $publicLabel |
|
| 135 | + */ |
|
| 136 | + public function setPublicLabel(string $publicLabel): void |
|
| 137 | + { |
|
| 138 | + $this->publicLabel = sanitize_text_field($publicLabel); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return bool |
|
| 144 | + */ |
|
| 145 | + public function showLabel(): bool |
|
| 146 | + { |
|
| 147 | + return $this->showLabel; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param bool|int|string $showLabel |
|
| 153 | + */ |
|
| 154 | + public function setShowLabel($showLabel): void |
|
| 155 | + { |
|
| 156 | + $this->showLabel = filter_var($showLabel, FILTER_VALIDATE_BOOLEAN); |
|
| 157 | + } |
|
| 158 | 158 | } |