<?php

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

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

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

        $this->rdata = new SRV();
        $this->rdata->priority = $contentArray[0];
        $this->rdata->weight = $contentArray[1];
        $this->rdata->port = $contentArray[2];
        $this->rdata->target = $this->contentToRelative(IdnaHelper::idnaDecode($contentArray[3]));
    }

    /**
     * @param SRV $rdata
     * @return string
     */
    public function parseContentToApiFormat( $rdata )
    {
        $content = [
            $rdata->priority,
            $rdata->weight,
            $rdata->port,
            $this->contentToAbsolute(IdnaHelper::idnaEncode($rdata->target))
        ];

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