Skip to content

Commit 010a951

Browse files
committed
before refactor
0 parents  commit 010a951

26 files changed

+654
-0
lines changed

Database.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
// Connect to the database and execute a query.
4+
// whenever you have php file that only contains a class, it's common to name it the same as the class.
5+
// general conventions that says make first latter uppercase.
6+
7+
class Database
8+
{
9+
public $connection;
10+
public $statement;
11+
12+
public function __construct($config, $username = 'root', $password = 'password')
13+
{
14+
15+
// this is a function that takes an associative array and converts it to a query string.
16+
// http_build_query($data);// example.com?host=localhost&port=3306&dbname=testdb&charset=utf8mb4
17+
// dd('mysql:' . http_build_query($config, '', ';')); // host=localhost;port=3306;dbname=testdb;charset=utf8mb4
18+
19+
$dsn = 'mysql:' . http_build_query($config, '', ';'); // host=localhost;port=3306;dbname=testdb;charset=utf8mb4
20+
21+
// This is automatically called when you create a new instance of the class.
22+
// both are same
23+
// $dsn = 'mysql:' . http_build_query($config, '', ';');
24+
// $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['dbname']};charset={$config['charset']}";
25+
26+
//$this for this instance of the class
27+
$this->connection = new PDO($dsn, $username, $password, [
28+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
29+
]);
30+
}
31+
32+
public function query($query, $params = [])
33+
{
34+
35+
$this->statement = $this->connection->prepare($query);
36+
$this->statement->execute($params);
37+
38+
// return $statement;
39+
return $this;
40+
}
41+
42+
public function find()
43+
{
44+
// $statement->fetch();
45+
return $this->statement->fetch();
46+
}
47+
48+
public function findOrFail()
49+
{
50+
$result = $this->find();
51+
52+
if (! $result) {
53+
abort();
54+
}
55+
56+
return $result;
57+
}
58+
59+
public function get()
60+
{
61+
return $this->statement->fetchAll();
62+
}
63+
}
64+
65+
// $posts = $db->query('select * from posts');
66+
// foreach ($posts as $post) {
67+
// echo '<li>' . $post['title'] . '</li>';
68+
// }
69+
70+
// this class expect an argument
71+
// new PDO($dsn);
72+
// => dsn stands for Data Source Name
73+
// think of that as a connection string:a string that declares your connection to the database
74+
75+
// class Person
76+
// {
77+
// public $name;
78+
// public $age;
79+
80+
// // Functions with in a class are called methods.
81+
// // Technically you don't have to do public function breathe()
82+
// // because the default visibility is public.
83+
// // it's just common practice to be explicit.
84+
// // just follow practices like being explicit with visibility
85+
86+
// public function breathe()
87+
// {
88+
// echo $this->name . ' is breathing!';
89+
// }
90+
// }
91+
92+
// $person = new Person();
93+
// $person->name = 'John';
94+
// $person->age = 30;
95+
96+
// $person->breathe();
97+
98+
// return $statement->fetchAll(PDO::FETCH_ASSOC);
99+
// return $statement->fetch(PDO::FETCH_ASSOC);
100+
// $post = $db->query('select * from posts where id = 1')->fetch(PDO::FETCH_ASSOC);
101+
// $posts = $db->query('select * from posts')->fetchAll(PDO::FETCH_ASSOC);

Response.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class Response {
4+
const NOT_FOUND = 404;
5+
const FORBIDDEN = 403;
6+
}

Validator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class Validator
4+
{
5+
// this method is what we call a pure function
6+
// A pure function is a function that is not contention or dependent upon state or values from the outside world.
7+
// and we can make pure functions a static function
8+
// and we can call them without creating an instance of the class
9+
// like this if (! Validator::string($_POST['body'], 1, 1000)) {}
10+
// NOTE: you can't call non-static method with ::
11+
public static function string($value, $min = 1, $max = INF)
12+
{
13+
$value = trim($value);
14+
15+
return strlen($value) >= $min && strlen($value) <= $max;
16+
}
17+
18+
public static function email($value)
19+
{
20+
// Validator::email('user@example.com');
21+
// NOTE: you can do this with very dramatic regular expressions
22+
// instead we are gonna use PHP's built-in filter_var function
23+
// this gives us to sanitize and validate strings in number of ways
24+
// invalid email return false, valid email return the email address
25+
return filter_var($value, FILTER_VALIDATE_EMAIL);
26+
}
27+
}

config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'database' => [
5+
'host' => 'localhost',
6+
'port' => 3306,
7+
'dbname' => 'testdb',
8+
'charset' => 'utf8mb4'
9+
]
10+
];

controllers/about.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
// require 'functions.php';
3+
4+
// $heading = 'About Us';
5+
// // echo($_SERVER['REQUEST_URI']);
6+
7+
8+
// include base_path('views/about.view.php');
9+
10+
view('about.view.php', [
11+
'heading' => 'About Us',
12+
]);

controllers/contact.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
// require 'functions.php';
3+
4+
// $heading = 'Contact Us';
5+
// // echo($_SERVER['REQUEST_URI']);
6+
// include base_path('views/contact.view.php');
7+
8+
view('contact.view.php', [
9+
'heading' => 'Contact Us',
10+
]);

controllers/index.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
// require 'functions.php';
3+
4+
5+
view('index.view.php', [
6+
'heading' => 'Home',
7+
]);

controllers/notes/create.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require base_path('Validator.php');
4+
5+
$config = require(base_path('config.php'));
6+
$db = new Database($config['database']);
7+
8+
$heading = 'Create Note';
9+
10+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
11+
$errors = [];
12+
13+
if (! Validator::string($_POST['body'], 1, 1000)) {
14+
$errors['body'] = 'A body of no more than 1000 characters is required.';
15+
}
16+
17+
18+
if (empty($errors)) {
19+
$db->query('INSERT INTO notes(body, user_id) VALUES(:body, :user_id)', [
20+
'body' => $_POST['body'],
21+
'user_id' => 5
22+
]);
23+
}
24+
}
25+
26+
require base_path('views/notes/create.view.php');

controllers/notes/index.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$config = require(base_path('config.php'));
4+
$db = new Database($config['database']);
5+
6+
// $heading = 'My Notes';
7+
8+
$notes = $db->query('SELECT * FROM notes where user_id = 5')->get();
9+
10+
// include base_path('views/notes/index.view.php');
11+
12+
// turns out i don't need to access $config and $db variable from my views
13+
// before this refactor you could do that
14+
// now this two variables are only available inside our view
15+
view('notes/index.view.php', [
16+
'heading' => 'My Notes',
17+
'notes' => $notes
18+
]);

controllers/notes/show.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$config = require(base_path('config.php'));
4+
$db = new Database($config['database']);
5+
6+
$heading = 'Note';
7+
$currentUserId = 5;
8+
9+
$note = $db->query('SELECT * FROM notes where id = :id', [
10+
'id' => $_GET['id']
11+
])->findOrFail();
12+
13+
14+
authorize($note['user_id'] === $currentUserId);
15+
16+
include base_path('views/notes/show.view.php');
17+
18+
19+
// both is acceptable with : and without : no difference
20+
// $notes = $db->query('SELECT * FROM notes where id = :id', ['id' => $id])->fetch();
21+
// $notes = $db->query('SELECT * FROM notes where id = :id', [':id' => $id])->fetch();

0 commit comments

Comments
 (0)