<?php

namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\Adapters;

use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPRecordAdapterType;

class CAAAdapter extends Record
{
    /**
     * CAAAdapter constructor.
     * @param Record $record
     */
    public function __construct( Record $record)
    {
        $this->line = $record->line;
        $this->name = $record->name;
        $this->type = 'CAA';
        $this->class = $record->class;
        $this->ttl = $record->ttl;
        $this->rdlength = $record->rdlength;

        $this->rdata = new OPRecordAdapterType();
        $this->rdata->flag = $record->rdata->flag;
        $this->rdata->tag = $record->rdata->tag;
        $this->rdata->target = str_replace('"', "", htmlspecialchars_decode($record->rdata->target));

        $this->absoluteName = $record->absoluteName;
        $this->customData = $record->customData;
    }

    public function toDnsManagerRecord($record)
    {

        $valueArray = explode(' ', $record['value'], 3);

        $templateData = [
            'name' => $record['name'],
            'type' => $record['type'],
            'ttl' => $record['ttl'],
            'flag' => $valueArray[0],
            'tag' => $valueArray[1],
            'target' => str_replace('"', '', $valueArray[2]),
        ];

        return Record::tryToCreateFromArray($templateData);
    }
}