-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframework.php
More file actions
88 lines (71 loc) · 1.93 KB
/
framework.php
File metadata and controls
88 lines (71 loc) · 1.93 KB
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
<?php
include_once __DIR__ . '/services/utils.php';
$config = arrayToObject([
'json' => [
'users' => __DIR__ . '/data/users.json'
]
]);
include_once __DIR__ . '/services/db.php';
include_once __DIR__ . '/services/user.php';
include_once __DIR__ . '/services/users.php';
include_once __DIR__ . '/services/validation.php';
include_once __DIR__ . '/services/email.php';
include_once __DIR__ . '/services/classes.php';
class App {
public $user;
public $users;
public $db;
public $email;
public $classes;
public function __construct() {
$this->db = new DbConnection($this);
$this->user = new SessionUser($this);
$this->users = new SqlUsersManager($this);
$this->email = new EmailManager($this);
$this->classes = new SqlClassManager($this);
}
public function bind(array $requirements, $source = null) {
if (!$source) {
$source = $_POST;
}
$data = [];
$errors = [];
foreach ($requirements as $key => $validator) {
$value = null;
if (isset($source[$key])) {
$value = $source[$key];
}
$data[$key] = $value;
if (!$validator->validate($value, $key, $source)) {
$errors[$key] = $validator->error;
}
}
return new ModelState($data, $errors);
}
}
$app = new App();
$match = new ValidatorFactory();
// MVC
function model($data = [], $errors = []) {
return new ModelState($data, $errors);
}
function view(string $name, array $data = []) {
global $app, $config;
$model = model();
foreach ($data as $key => $value) {
$$key = $value;
}
include __DIR__ . '/views/' . $name . '.php';
}
function status($code) {
http_response_code($code);
}
function json($data = [], $code = 200) {
http_response_code($code);
header('Content-Type: application/json;charset=utf-8');
echo json_encode($data);
}
function redirect(string $url, $data = [], $statusCode = 303) {
header('Location: ' . buildLink($url, $data), true, $statusCode);
}
?>