<?php

namespace GriffinForms\Admin\Secure;

use GriffinForms\Traits\Singleton;

class Format extends \GriffinForms\Admin\Format
{
    use Singleton;
    
    protected $meta;
    protected $choices;
    protected $data;
    protected $errors = [];
    
    protected function setChoices()
    {
        $this->setHelper('choices');
    }

    public function hasErrors()
    {
        return !empty($this->errors);
    }

    public function getErrors()
    {
        return $this->errors;
    }
    
    public function secureData($data, $serialize = true)
    {
        $secured_data = array();
        
        foreach($data as $name => $value)
        {
            $secure_method = 'secure' . $this->purgeString($name);
            if(method_exists($this, $secure_method)) {
                if ($serialize === true) {
                    $secured_data[$name] = maybe_serialize($this->$secure_method($value));
                } else {
                    $secured_data[$name] = $this->$secure_method($value);
                }
            }
        }
        
        return $secured_data;
    }
    
    public function data($type, $value, $data = '')
    {
        $method = 'secure' . $this->purgeString($type);
        return $this->$method($value, $data);
    }
    
    public function setData($data = array())
    {
        $this->data = $data;
    }
    
    protected function secureItemType($type)
    {
        if (in_array($type, $this->config->getItemTypes())) {
            return $type;
        } else {
            return null;
        }
    }
    
    protected function secureParentType($type)
    {
        if (in_array($type, $this->config->getItemTypes())) {
            return $type;
        } else {
            return null;
        }
    }
    
    protected function secureParentId($id)
    {
        return $this->secureId($id);
    }
    
    protected function secureItemId($id)
    {
        return $this->secureId($id);
    }
    
    protected function secureFormId($id)
    {
        return $this->secureId($id);
    }
    
    protected function secureId($id)
    {
        if(!is_numeric($id)) {
            return null;
        }
        
        $secured_value = absint($id);
        return $secured_value;
    }
    
    protected function secureIds($value)
    {
        if(!is_array($value)) {
            return array();
        }
        
        $secured_value = array_map(array($this, 'secureId'), $value);
        return $secured_value;
    }
    
    protected function secureName($name)
    {
        return sanitize_text_field($name);
    }    
    
    protected function secureHeading($heading)
    {
        return sanitize_text_field($heading);
    }    
    
    protected function secureDescription($description)
    {
        return sanitize_textarea_field($description);
    }
    
    protected function secureFormStyle($value)
    {
        return $this->secureId($value);
    }    
    
    protected function secureToggle($value)
    {
        if($value == 1 or $value === true) {
            return 1;
        } else {
            return 0;
        }
    }
}
