Skip to content
Snippets Groups Projects
Select Git revision
  • be343b5640e420ee46ec3526b139c6127466ecd3
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.2
  • 2.3.1
  • 2.3
25 results

BasicField.php

Blame
  • BasicField.php 5.85 KiB
    <?php
    
    namespace WPDesk\Forms\Field;
    
    use BadMethodCallException;
    use WPDesk\Forms\Field;
    use WPDesk\Forms\Sanitizer;
    use WPDesk\Forms\Sanitizer\NoSanitize;
    use WPDesk\Forms\Serializer;
    use WPDesk\Forms\Validator;
    use WPDesk\Forms\Validator\ChainValidator;
    use WPDesk\Forms\Validator\RequiredValidator;
    
    /**
     * Base class for fields. Is responsible for settings all required field values and provides standard implementation for
     * the field interface.
     */
    abstract class BasicField implements Field {
    
    	use Field\Traits\HtmlAttributes;
    
    	const DEFAULT_PRIORITY = 10;
    
    	/** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */
    	protected $meta = [
    		'priority'        => self::DEFAULT_PRIORITY,
    		'default_value'   => '',
    		'label'           => '',
    		'description'     => '',
    		'description_tip' => '',
    		'data'            => [],
    	];
    
    	public function should_override_form_template(): bool {
    		return false;
    	}
    
    	public function get_type(): string {
    		return 'text';
    	}
    
    	public function get_validator(): Validator {
    		$chain = new ChainValidator();
    		if ( $this->is_required() ) {
    			$chain->attach( new RequiredValidator() );
    		}
    
    		return $chain;
    	}
    
    	public function get_sanitizer(): Sanitizer {
    		return new NoSanitize();
    	}
    
    	public function has_serializer(): bool {
    		return false;
    	}
    
    	public function get_serializer(): Serializer {
    		throw new BadMethodCallException( 'You must define your serializer in a child class.' );
    	}
    
    	final public function get_name(): string {
    		return $this->attributes['name'] ?? '';
    	}
    
    	final public function get_label(): string {
    		return $this->meta['label'] ?? '';
    	}