<?php

namespace MGModule\DNSManager2\mgLibs\custom\dns\record\type;

/**
 * http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml
 */
abstract class RecordTypeAbstract implements \ArrayAccess {
    public function __set($name, $value) {
        return ;
    }
    
    public function toArray($uppercase = true) {
        $out = array();
        foreach(get_object_vars($this) as $k => $v)
            $out[$uppercase?strtoupper($k):strtolower($k)] = $v;
        return $out;
    }
    
    public function setFirstProperty($value) {
        $vars = get_object_vars($this);
        $keys = array_keys($vars);
        $k = reset($keys);
        if($k !== false)
            $this->$k = $value;
    }

    public function setDataFromArray( $array )
    {
        foreach( $array as $k => $value )
        {
            $k = strtolower($k);
            if( property_exists($this, $k) )
            {
                $customMethodName = 'set' . ucfirst($k);
                if( method_exists($this,$customMethodName ) )
                {
                    $this->{$customMethodName}($value);
                }
                else
                {
                    $this->$k = $value;
                }
            }
        }
    }
    
    public static function newFromArray($array) {
        $record = new self();
        $record->setDataFromArray($array);
        return $record;
    }
    
    public function __toString() {
        return $this->toString();
    }

    public function toString( $onlyThis = [] )
    {
        $vars = get_object_vars($this);
        if( !empty($onlyThis) )
        {
            $vars = array_intersect_key($vars, array_flip($onlyThis));
        }

        return implode("\t", $vars);
    }

    public function fromString($string, $delimeter = '\s') {
        $vars = get_object_vars($this);
        $exp = preg_split('/'.$delimeter.'/', $string,count($vars));
        $exp = array_pad($exp, count($vars), '');
        $i = 0;
        foreach($vars as $k => $v)
            $this->$k = $exp[$i++];
    }
    
    public function encode() {
        foreach(get_object_vars($this) as $k => $v) {
            $this->$k = htmlspecialchars($v);
        }
    }
    
    public function decode() {
        foreach(get_object_vars($this) as $k => $v) {
            $this->$k = htmlspecialchars_decode($v);
        }
    }
    
    //IMPLEMENTS
    public function offsetExists($offset) {
        return isset($this->$offset);
    }

    public function offsetGet($offset) {
        return $this->$offset;
    }

    public function offsetSet($offset, $value) {
        if($this->offsetExists($offset)) 
            $this->$offset = $value;
    }

    public function offsetUnset($offset) {
        if($this->offsetExists($offset)) 
            $this->$offset = '';
    }

    public function validate()
    {
        foreach(get_object_vars($this) as $k => $v) {
            $method = $k.'Validate';
            if(method_exists($this, $method) && !$this->$method($v))
            {
                throw new \Exception('Validation failed for field: '.ucfirst($k).' and value: '.$v);
            }
        }
    }

    public function unify(){}
}