<?php

namespace app\models\mikrotik\routing\rip;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Routes extends Model {

    public $id;
    public $dst_address;
    public $metric;
    public $gateway;
    public $from;
    public $timeout;

    public function rules() {
        return [
            ['id', 'string'],
            ['dst_address', 'string'],
            ['metric', 'string'],
            ['gateway', 'string'],
            ['from', 'string'],
            ['timeout', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/route/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Routes();
                $c->id = $response->getProperty('.id');
                $c->dst_address = $response->getProperty('dst-address');
                $c->metric = $response->getProperty('metric');
                $c->gateway = (($response->getProperty('gateway') == '') ? '0.0.0.0' : $response->getProperty('gateway'));
                $c->from = (($response->getProperty('from') == '') ? '0.0.0.0' : $response->getProperty('from'));
                $c->timeout = (($response->getProperty('timeout') == '') ? '00:00:00' : $response->getProperty('timeout'));
                $return[] = $c;
            }
        }
        return $return;
    }

}
