<?php

namespace app\models\mikrotik\ip\routes;

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 Nexthops extends Model {

    public $id;
    public $address;
    public $gw_state;
    public $forwarding_nexthop;
    public $interface;
    public $scope;
    public $check_gateway;

    public function rules() {
        return [
            ['id', 'string'],
            ['address', 'string'],
            ['gw_state', 'string'],
            ['forwarding_nexthop', 'string'],
            ['interface', 'string'],
            ['scope', 'string'],
            ['check_gateway', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/nexthop/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Nexthops();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->gw_state = $response->getProperty('gw-state');
                $c->forwarding_nexthop = $response->getProperty('forwarding-nexthop');
                $c->interface = $response->getProperty('interface');
                $c->scope = $response->getProperty('scope');
                $c->check_gateway = $response->getProperty('check-gateway');
                $return[] = $c;
            }
        }
        return $return;
    }

}
