|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Api\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\User; |
|
6
|
|
|
use App\Port; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class PortController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct() { |
|
12
|
|
|
|
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Display a listing of the resource. |
|
17
|
|
|
* |
|
18
|
|
|
* @return \Illuminate\Http\Response |
|
19
|
|
|
*/ |
|
20
|
|
|
public function index(Request $request) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($request->user()->level >= 10 || $request->user()->level == 5) { |
|
23
|
|
|
return Port::all(); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
else { |
|
26
|
|
|
return User::find($request->user()->user_id)->ports()->get(); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Show the form for creating a new resource. |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Illuminate\Http\Response |
|
34
|
|
|
*/ |
|
35
|
|
|
public function create() |
|
36
|
|
|
{ |
|
37
|
|
|
// |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Store a newly created resource in storage. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Illuminate\Http\Request $request |
|
44
|
|
|
* @return \Illuminate\Http\Response |
|
45
|
|
|
*/ |
|
46
|
|
|
public function store(Request $request) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
// |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Display the specified resource. |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $id |
|
55
|
|
|
* @return \Illuminate\Http\Response |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function show(Request $request, $id) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if ($request->user()->level >= 10 || $request->user()->level == 5) { |
|
60
|
|
|
return Port::find($id); |
|
61
|
|
|
} |
|
62
|
|
|
else { |
|
63
|
|
|
$user = User::find($request->user()->user_id); |
|
64
|
|
|
return $user->ports()->find($id); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Show the form for editing the specified resource. |
|
70
|
|
|
* |
|
71
|
|
|
* @param int $id |
|
72
|
|
|
* @return \Illuminate\Http\Response |
|
73
|
|
|
*/ |
|
74
|
|
|
public function edit($id) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
// |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Update the specified resource in storage. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Illuminate\Http\Request $request |
|
83
|
|
|
* @param int $id |
|
84
|
|
|
* @return \Illuminate\Http\Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function update(Request $request, $id) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
// |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Remove the specified resource from storage. |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $id |
|
95
|
|
|
* @return \Illuminate\Http\Response |
|
96
|
|
|
*/ |
|
97
|
|
|
public function destroy($id) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
// |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.