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