<?php

namespace app\models\mikrotik\ip\dns;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Dnscache extends Model {

    public $id;
    public $name;
    public $type;
    public $data;
    public $ttl;

    public function rules() {
        return [
            ['name', 'string'],
            ['type', 'ip'],
            ['ttl', 'string'],
            ['data', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dns/cache/all/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dnscache();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->type = $response->getProperty('type');
                $c->ttl = $response->getProperty('ttl');
                $c->data = $response->getProperty('data');

                $return[] = $c;
            }
        }
        return $return;
    }



    public function flush() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dns/cache/flush');
        return $connect->getConnection()->sendSync($getRequest);
        
    }

}
