|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class Port extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The table associated with the model. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $table = 'ports'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The primary key column name. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $primaryKey = 'port_id'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Indicates if the model should be timestamped. |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public $timestamps = false; |
|
29
|
|
|
|
|
30
|
|
|
// TODO: transform DB to be snake case? |
|
31
|
|
|
// public static $snakeAttributes = false; |
|
32
|
|
|
|
|
33
|
|
|
// ---- Accessors/Mutators ---- |
|
34
|
|
|
|
|
35
|
|
|
//TODO this is the wrong place for this as it messes up sorting |
|
36
|
|
|
// public function getifSpeedAttribute($ifSpeed) { |
|
37
|
|
|
// return $this->getifSpeedHumanAttribute($ifSpeed); |
|
38
|
|
|
// } |
|
39
|
|
|
|
|
40
|
|
|
public function getifSpeedHumanAttribute($ifSpeed) { |
|
41
|
|
|
return $this->formatBps($ifSpeed); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// TODO: move to a common file |
|
46
|
|
|
// base = 1024 for bits, 1000 for mibibits |
|
47
|
|
|
function formatBps($bits, $precision = 2, $base = 1000) { |
|
|
|
|
|
|
48
|
|
|
$units = array('bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'); |
|
49
|
|
|
|
|
50
|
|
|
$bits = max($bits, 0); |
|
51
|
|
|
$pow = floor(($bits ? log($bits) : 0) / log($base)); |
|
52
|
|
|
$pow = min($pow, count($units) - 1); |
|
53
|
|
|
|
|
54
|
|
|
$bits /= pow($base, $pow); |
|
55
|
|
|
|
|
56
|
|
|
return round($bits, $precision).' '.$units[$pow]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
// ---- Define Reletionships ---- |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the device this port belongs to. |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
|
|
public function device() { |
|
67
|
|
|
return $this->belongsTo('App\Device', 'device_id', 'device_id'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns a list of users that can access this port. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function users() { |
|
74
|
|
|
return $this->belongsToMany('App\User', 'ports_perms', 'port_id', 'user_id'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.