Fields.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FormAPI; |
| 4 | |
| 5 | use Give\FormAPI\Form\Colorpicker; |
| 6 | use Give\FormAPI\Form\File; |
| 7 | use Give\FormAPI\Form\Group; |
| 8 | use Give\FormAPI\Form\Media; |
| 9 | use Give\FormAPI\Form\Radio; |
| 10 | use Give\FormAPI\Form\Select; |
| 11 | use Give\FormAPI\Form\Text; |
| 12 | use Give\FormAPI\Form\Textarea; |
| 13 | use Give\FormAPI\Form\Wysiwyg; |
| 14 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 15 | |
| 16 | final class Fields |
| 17 | { |
| 18 | /** |
| 19 | * Field vs class name mapping array |
| 20 | * |
| 21 | * @since 2.7.0 |
| 22 | * @var array |
| 23 | */ |
| 24 | private $fieldClasses = [ |
| 25 | 'text' => Text::class, |
| 26 | 'textarea' => Textarea::class, |
| 27 | 'file' => File::class, |
| 28 | 'media' => Media::class, |
| 29 | 'radio' => Radio::class, |
| 30 | 'wysiwyg' => Wysiwyg::class, |
| 31 | 'colorpicker' => Colorpicker::class, |
| 32 | 'group' => Group::class, |
| 33 | 'select' => Select::class, |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * Get field object. |
| 38 | * |
| 39 | * @since 2.7.0 |
| 40 | * |
| 41 | * @param array $array |
| 42 | * |
| 43 | * @return Form\Field |
| 44 | */ |
| 45 | public static function fromArray($array) |
| 46 | { |
| 47 | $field = new static(); |
| 48 | $field->validate($array); |
| 49 | |
| 50 | /** |
| 51 | * Filter the field classes |
| 52 | * |
| 53 | * @since 2.7.0 |
| 54 | * |
| 55 | * @param Form\Field[] $fieldClasses |
| 56 | */ |
| 57 | $field->fieldClasses = apply_filters('give_form_api_field_classes', $field->fieldClasses); |
| 58 | |
| 59 | /* @var Form\Field $fieldClass */ |
| 60 | $fieldClass = $field->fieldClasses[$field->getFieldType($array['type'])]; |
| 61 | |
| 62 | return $fieldClass::fromArray($array); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get field class name. |
| 67 | * Note: |
| 68 | * 1. Field name create with {fieldType_modifier} logic. Use underscore in field type only if you want to add a modifier. For example: text_small, radio_inline etc. |
| 69 | * 2. This function exist for backward compatibility and can be remove in future |
| 70 | * |
| 71 | * @since 2.7.0 |
| 72 | * |
| 73 | * @param $type |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | private function getFieldType($type) |
| 78 | { |
| 79 | if (false !== strpos($type, '_')) { |
| 80 | $type = current(explode('_', $type, 2)); |
| 81 | } |
| 82 | |
| 83 | return $type; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Validate field arguments |
| 88 | * |
| 89 | * @since 2.7.0 |
| 90 | * |
| 91 | * @param array $array |
| 92 | * |
| 93 | * @throws InvalidArgumentException |
| 94 | */ |
| 95 | private function validate($array) |
| 96 | { |
| 97 | $required = ['id', 'name', 'type']; |
| 98 | $array = array_filter($array); // Remove empty values. |
| 99 | |
| 100 | if (array_diff($required, array_keys($array))) { |
| 101 | throw new InvalidArgumentException( |
| 102 | __('To create a Field object, please provide valid id, name and type.', 'give') |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 |