<?php

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

use MGModule\DNSManager2\mgLibs\custom\dns\record\type\SOA;
use MGModule\DNSManager2\mgLibs\custom\helpers\IdnaHelper;

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

        $this->rdata          = new SOA();
        $this->rdata->mname   = IdnaHelper::idnaDecode($contentArray[0]);
        $this->rdata->rname   = IdnaHelper::idnaDecode($contentArray[1]);
        $this->rdata->serial  = $contentArray[2];
        $this->rdata->refresh = $contentArray[3];
        $this->rdata->retry   = $contentArray[4];
        $this->rdata->expire  = $contentArray[5];
        $this->rdata->minimum = $contentArray[6];
    }

    /**
     * @param SOA $rdata
     *
     * @return string
     */
    public function parseContentToApiFormat( $rdata )
    {
        $content = [
            $this->contentToAbsolute(IdnaHelper::idnaEncode($rdata->mname)),
            $this->contentToAbsolute(IdnaHelper::idnaEncode($rdata->rname)),
            $rdata->serial,
            $rdata->refresh,
            $rdata->retry,
            $rdata->expire,
            $rdata->minimum,
        ];

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