<?php

namespace app\models\mikrotik\routing\bgp;

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

    public $id;
    public $peer;
    public $prefix;
    public $nexthop;
    public $as_path;
    public $origin;
    public $local_pref;
    public $med;

    public function rules() {
        return [
            ['id', 'string'],
            ['peer', 'string'],
            ['prefix', 'string'],
            ['nexthop', 'string'],
            ['as_path', 'string'],
            ['origin', 'string'],
            ['local_pref', 'string'],
            ['med', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/advertisements/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Advertisements();
                $c->id = $response->getProperty('.id');
                $c->peer = $response->getProperty('peer');
                $c->prefix = $response->getProperty('prefix');
                $c->nexthop = $response->getProperty('nexthop');
                $c->as_path = $response->getProperty('as-path');
                $c->origin = $response->getProperty('origin');
                $c->local_pref = $response->getProperty('local-pref');
                $c->med = $response->getProperty('med');
                $return[] = $c;
            }
        }
        return $return;
    }

}
