-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase-mvc-cours-cedric.php
181 lines (137 loc) · 3.44 KB
/
base-mvc-cours-cedric.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
// src/Controller/ArticlesListController.php
class ArticlesListController
{
public function execute($select)
{
// Appeler la base pour recuperer les articles
// -> en fonction du $_GET['page']
// $length = 20;
// $start = ($_GET['page'] - 1) * $length;
// SELECT * FROM articles LIMIT $start, $length
$articles = Article::getList($_GET['page'] ?? 1);
// Passer les articles au template
// Envoyer le collage à l'user echo
(new ArticlesListView)->render($articles);
}
}
// src/Model/Article.php
class Article
{
private string $title;
private DateTime $createdAt;
public static function createFromSqlRow(array $row): self
{
$article = new self();
$article->title = $row['title'];
$article->createdAt = DateTime::createFromFormat('Y-m-d H:i:s', $row['created_at']);
return $article;
}
Article::createFromSqlRow($row);
/**
* @return Article[]
*/
public static function getList(int $page = 1): array
{
// $sql = ......
$articles = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$articles[] = Article::createFromSqlRow($row);
}
return $articles;
}
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function getAge(): int
{
$diff = $this->getCreatedAt()->diff(new DateTime());
return $diff->days;
}
}
// src/View/ArticlesListView.php
class ArticlesListView
{
public function render(array $articles)
{
include "articles.html.php";
}
}
// articles.html.php
include "header.php";
?>
<div>
<?php foreach ($articles as $article): ?>
<h1><?php echo $article->getCreatedAt(); ?></h1>
<?php endforeach; ?>
</div>
<?php
// src/Security/Security.php
class Security
{
public function isGranted(string $role): bool
{
return false;
}
}
// public/login.php
$security = new Security();
if ($security->isLoggedIn()) {
header('Location: index.php');
return;
}
// public/articles.php
$security = new Security();
if (!$security->isGranted('USER')) {
return $security->redirectToLogin();
}
(new ArticlesListController())->execute();
<?php
class Flight
{
public static function createFromSqlRow(array $row): self
{
}
public function toSqlArray() : array
{
return [
'beginDate' => $this->beginDate->format('u'),
];
}
}
class FlightRepository
{
/**
* @return Flight[]
*/
public static function getList(
?DateTime $beginDate = null
): array
{
$sql = "SELECT *
FROM flights
INNER JOIN airport fromAirp ON ...
INNER JOIN airport toAirp ON ...
WHERE 1 ";
if ($beginDate) {
$sql .= " AND beginDate = :beginDate";
$params['beginDate'] = $beginDate;
}
}
}
FlightRepository::getList()
FlightRepository::getList(
beginDate: new DateTime('3 month ago'),
);
Database::insert('flight', $flight->toSqlArray());
Database::update('flight', $flight->toSqlArray(), $flight->getId());
class Database
{
public static function insert($table, array $data)
{
$cols = implode(', ', array_keys($data));
$values = implode(', ', array_values($data));
$sql = "INSERT INTO $table ($cols) VALUES ($values)";
}
}