<?php

namespace app\models\mikrotik\switchm;

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

    public $id;
    public $name;
    public $switch;
    public $vlan_mode;
    public $vlan_header;
    public $default_vlan_id;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['switch', 'string'],
            ['vlan_mode', 'string'],
            ['vlan_header', 'string'],
            ['default_vlan_id', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/port/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Port();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->switch = $response->getProperty('switch');
                $c->vlan_mode = $response->getProperty('vlan-mode');
                $c->vlan_header = $response->getProperty('vlan-header');
                $c->default_vlan_id = $response->getProperty('default-vlan-id');
                $return[] = $c;
            }
        }
        return $return;
    }

}
