|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Transformers\TaskTransformer; |
|
6
|
|
|
use App\Task; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
|
9
|
|
|
use Illuminate\Support\Facades\Input; |
|
10
|
|
|
|
|
11
|
|
|
class TaskController extends ApiController |
|
12
|
|
|
{ |
|
13
|
|
|
protected $taskTransformer; |
|
14
|
|
|
/** |
|
15
|
|
|
* TaskController constructor. |
|
16
|
|
|
* @param $taskTransformer |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(TaskTransformer $taskTransformer) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->taskTransformer = $taskTransformer; |
|
21
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Display a listing of the resource. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Illuminate\Http\Response |
|
28
|
|
|
*/ |
|
29
|
|
|
public function index() |
|
30
|
|
|
{ |
|
31
|
|
|
$task = Task::all(); |
|
32
|
|
|
return $this->respond($this->taskTransformer->transformCollection($task->all())); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Show the form for creating a new resource. |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Illuminate\Http\Response |
|
39
|
|
|
*/ |
|
40
|
|
|
public function create() |
|
41
|
|
|
{ |
|
42
|
|
|
// |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Store a newly created resource in storage. |
|
47
|
|
|
* @return \Illuminate\Http\Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function store() |
|
50
|
|
|
{ |
|
51
|
|
|
if (!Input::get('name') or !Input::get('done') or !Input::get('priority')) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY) |
|
54
|
|
|
->respondWithError('Parameters failed validation for a task.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
Task::create(Input::all()); |
|
58
|
|
|
|
|
59
|
|
|
return $this->respondCreated('Task successfully created.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Display the specified resource. |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $id |
|
66
|
|
|
* @return \Illuminate\Http\Response |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$task = Task::find($id); |
|
71
|
|
|
|
|
72
|
|
|
if (!$task) { |
|
73
|
|
|
return $this->respondNotFound('Task does not exsist'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->respond([ |
|
77
|
|
|
'data' => $this->taskTransformer->transform($task) |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Show the form for editing the specified resource. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $id |
|
85
|
|
|
* @return \Illuminate\Http\Response |
|
86
|
|
|
*/ |
|
87
|
|
|
public function edit($id) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
// |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Update the specified resource in storage. |
|
94
|
|
|
* |
|
95
|
|
|
* @param \Illuminate\Http\Request $request |
|
96
|
|
|
* @param int $id |
|
97
|
|
|
* @return \Illuminate\Http\Response |
|
98
|
|
|
*/ |
|
99
|
|
|
public function update(Request $request, $id) |
|
100
|
|
|
{ |
|
101
|
|
|
$task = Task::find($id); |
|
102
|
|
|
|
|
103
|
|
|
if (!$task) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->respondNotFound('Task does not exist!!'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$task->name = $request->name; |
|
109
|
|
|
$task->priority = $request->priority; |
|
110
|
|
|
$task->done = $request->done; |
|
111
|
|
|
$task->save(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Remove the specified resource from storage. |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $id |
|
118
|
|
|
* @return \Illuminate\Http\Response |
|
119
|
|
|
*/ |
|
120
|
|
|
public function destroy($id) |
|
121
|
|
|
{ |
|
122
|
|
|
Task::destroy($id); |
|
123
|
|
|
} |
|
124
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.