<?php
namespace Storzen\Contracts;
defined( 'ABSPATH' ) || exit;

/**
 * Abstract base for every field type. PHP 7.4+ compatible.
 */
abstract class Field {

    /** @var string */  protected $slug;
    /** @var string */  protected $label;
    /** @var string */  protected $description = '';
    /** @var mixed  */  protected $default      = null;
    /** @var bool   */  protected $pro          = false;

    /** @return static */
    public static function make( $slug, $label ) {
        return new static( $slug, $label );
    }

    public function __construct( $slug, $label ) {
        $this->slug  = $slug;
        $this->label = $label;
    }

    /** @return static */
    public function set_description( $d ) { $this->description = $d; return $this; }
    /** @return static */
    public function desc( $d )            { return $this->set_description( $d ); }
    /** @return static */
    public function set_default( $v )     { $this->default = $v;     return $this; }
    /** @return static */
    public function set_pro( $p = true )  { $this->pro = (bool)$p;   return $this; }
    /** @return static */
    public function pro( $p = true )      { return $this->set_pro($p); }

    public function get_slug()        { return $this->slug; }
    public function get_label()       { return $this->label; }
    public function get_description() { return $this->description; }
    public function get_default()     { return $this->default; }
    public function is_pro()          { return $this->pro; }

    public function get_type() {
        $parts = explode( '\\', get_class( $this ) );
        return strtolower( end( $parts ) );
    }

    protected function field_id()   { return 'pk_' . $this->slug; }
    protected function field_name() { return 'pk_fields[' . $this->slug . ']'; }
    protected function dis()        { return $this->pro ? ' disabled' : ''; }

    abstract public function sanitize( $raw );
    abstract public function render( $value );
}
