<?php

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

use MGModule\DNSManager2\mgLibs\custom\dns\record\type\NAPTR;
use MGModule\DNSManager2\mgLibs\custom\dns\record\type\NS;
use MGModule\DNSManager2\mgLibs\custom\dns\record\type\RecordTypeAbstract;
use MGModule\DNSManager2\mgLibs\custom\helpers\IdnaHelper;

class NAPTRAdapter extends AbstractRCodeZeroAdapter
{
    /**
     * @param string $content
     */
    public function createRdata( $content )
    {
        $contentArray = explode(' ', $content);

        $this->rdata = new NAPTR();
        $this->rdata->order = $contentArray[0];
        $this->rdata->preference = $contentArray[1];
        $this->rdata->flags = trim( $contentArray[2],'"');
        $this->rdata->services = trim( $contentArray[3],'"');
        $this->rdata->regexp = trim( $contentArray[4],'"');
        $this->rdata->replacement = $this->contentToRelative(IdnaHelper::idnaDecode($contentArray[5]));
    }
    /**
     * @param NAPTR $rdata
     * @return mixed
     */
    public function parseContentToApiFormat( $rdata )
    {
        $content = [
            $rdata->order,
            $rdata->preference,
            ('"'.trim( $rdata->flags,'"').'"'),
            ('"'.trim( $rdata->services,'"').'"'),
            ('"'.trim( $rdata->regexp,'"').'"'),
            $this->contentToAbsolute(IdnaHelper::idnaEncode($rdata->replacement))
        ];

        return implode(' ', $content);
    }
}