Skip to content

Commit

Permalink
Merge pull request #12 from tanhongit/4-clean_code
Browse files Browse the repository at this point in the history
4 clean code
  • Loading branch information
tanhongit authored Jan 3, 2023
2 parents 33a6e41 + cc60dd5 commit b7dc9c4
Show file tree
Hide file tree
Showing 15 changed files with 12,534 additions and 2,073 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
/config/database.php
.vscode
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ npm run build

You need to change the connection information to the database if you want to store and use data for the website.

Path: [`/config/database.php`](https://github.com/TanHongIT/yl-mvc-structure/tree/main/config)
Path: [`/config/database.php.example`](https://github.com/TanHongIT/yl-mvc-structure/tree/main/config)

Copy the file `database.php.example` to `database.php` and edit the information in the file.

```php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_PASSWORD', '');
define('DB_NAME', 'mvc-structure');
```

Expand Down
15 changes: 12 additions & 3 deletions app/controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CategoryController extends Controller
public function __construct()
{
$this->loadModel('CategoryModel');
$this->categoryModel = new CategoryModel;
$this->categoryModel = new CategoryModel();
}

public function index()
Expand All @@ -25,8 +25,17 @@ public function index()
));
}

public function run()
/**
* @return string
*/
public function run(): string
{
echo __METHOD__;
return __METHOD__;
}

public function all()
{
$this->renderView('frontend.products.all');
var_dump($this->categoryModel->getAll());
}
}
4 changes: 3 additions & 1 deletion app/controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Controller
const MODEL_PATH = 'app/models';

/**
* index
* Index
* @return void
*/
public function index()
{
Expand Down Expand Up @@ -42,6 +43,7 @@ public function renderPartial($partialPath, array $data = [])
}

/**
* Load model
* @param $modelPath
*/
protected function loadModel($modelPath)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProductController extends Controller
public function __construct()
{
$this->loadModel('ProductModel');
$this->productModel = new ProductModel;
$this->productModel = new ProductModel();
}

public function index()
Expand Down
29 changes: 18 additions & 11 deletions app/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
class Database
{
protected $connection = NULL;
public $connectResult;

public function __construct()
{
$this->connectResult = $this->connect();
}

/**
Expand All @@ -16,7 +18,7 @@ public function connect()
{
// Create connection
if (!$this->connection) {
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$this->connection->set_charset('utf8mb4');
}
return $this->connection;
Expand All @@ -29,10 +31,8 @@ public function connect()
*/
public function select($sql)
{
$items = array();
$sql->execute();
$items = $sql->get_result()->fetch_all(MYSQLI_ASSOC);
return $items;
return $sql->get_result()->fetch_all(MYSQLI_ASSOC);
}

/**
Expand All @@ -41,9 +41,9 @@ public function select($sql)
* @param array $options
* @return array|void
*/
public function getByOptions($table, $options = array())
public function getByOptions($table, array $options = array())
{
$select = isset($options['select']) ? $options['select'] : '*';
$select = $options['select'] ?? '*';
$where = isset($options['where']) ? 'WHERE ' . $options['where'] : '';
$join = isset($options['join']) ? 'LEFT JOIN ' . $options['join'] : '';
$order_by = isset($options['order_by']) ? 'ORDER BY ' . $options['order_by'] : '';
Expand All @@ -65,9 +65,9 @@ public function getByOptions($table, $options = array())
* @param $table
* @param $id
* @param string $select
* @return array|false|string[]|void|null
* @return array|false|void|null
*/
public function getRecordByID($table, $id, $select = '*')
public function getRecordByID($table, $id, string $select = '*')
{
$id = intval($id);
$sql = "SELECT $select FROM `$table` WHERE id=$id";
Expand All @@ -86,7 +86,7 @@ public function getRecordByID($table, $id, $select = '*')
* @param array $data
* @return int|string|void
*/
public function save($table, $data = array())
public function save($table, array $data = array())
{
$values = array();
foreach ($data as $key => $value) {
Expand All @@ -108,10 +108,17 @@ public function save($table, $data = array())
* Delete data from table by ID
* @param $table
* @param $id
* @return array|false|void|null
*/
public function destroy($table, $id)
{
$sql = "DELETE FROM `$table` WHERE id=$id";
$this->_query($sql) or die(mysqli_error($this->connectResult));
$record = $this->getRecordByID($table, $id);

if ($record) {
$sql = "DELETE FROM `$table` WHERE id=$id";
$this->_query($sql) or die(mysqli_error($this->connectResult));
}

return $record;
}
}
12 changes: 5 additions & 7 deletions app/core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Model extends Database
{

public function __construct()
{
parent::__construct();
}

/**
Expand All @@ -13,7 +13,7 @@ public function __construct()
* @param array $config
* @return false|string
*/
public function upload($field, $config = array())
public function upload($field, array $config = array())
{
$options = array(
'name' => '',
Expand Down Expand Up @@ -52,10 +52,9 @@ public function upload($field, $config = array())
*/
public static function slug($str)
{
$str = self::convert_name($str);
$str = (new Model)->convert_name($str);
$str = strtolower($str); //mb_strtolower($str, 'UTF-8');
$str = str_replace(' ', '-', $str);
return $str;
return str_replace(' ', '-', $str);
}

/**
Expand All @@ -80,7 +79,6 @@ public function convert_name($str)
$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
$str = preg_replace("/(Đ)/", 'D', $str);
$str = preg_replace("/(\“|\”|\‘|\’|\,|\!|\&|\;|\@|\#|\%|\~|\`|\=|\_|\'|\]|\[|\}|\{|\)|\(|\+|\^)/", '-', $str);
$str = preg_replace("/( )/", '-', $str);
return $str;
return preg_replace("/( )/", '-', $str);
}
}
20 changes: 10 additions & 10 deletions app/models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

class BaseModel extends Model
{
protected $connectResult;

public function __construct()
{
$this->connectResult = $this->connect();
parent::__construct();
}

/**
* Create new data to table
* @param $table
* @param $data
* @return int|string|void
* @return int|string
*/
public function create($table, $data)
{
Expand All @@ -25,7 +23,7 @@ public function create($table, $data)
* Update data to table (use ID in $data)
* @param $table
* @param $data
* @return int|string|void
* @return int|string
*/
public function update($table, $data)
{
Expand All @@ -36,6 +34,7 @@ public function update($table, $data)
* Delete data from table by ID
* @param $table
* @param $id
* @return array|false|null
*/
public function delete($table, $id)
{
Expand All @@ -45,10 +44,10 @@ public function delete($table, $id)
/**
* Get all data in the table
* @param $table
* @param $attributes
* @return array|void
* @param array $attributes
* @return array|null
*/
public function all($table, $attributes = array())
public function all($table, array $attributes = array())
{
return $this->getByOptions($table, $attributes);
}
Expand All @@ -57,7 +56,7 @@ public function all($table, $attributes = array())
* Get data in table by ID
* @param $table
* @param $id
* @return array|false|string[]|void|null
* @return array|false|string[]|null
*/
public function find($table, $id)
{
Expand All @@ -76,8 +75,9 @@ public function _query($sql)
/**
* Escape special characters in string
* @param $str
* @return string
*/
public function escape($str)
public function escape($str): string
{
return mysqli_real_escape_string($this->connectResult, $str);
}
Expand Down
16 changes: 10 additions & 6 deletions app/models/CategoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ class CategoryModel extends BaseModel
{
const TABLE = 'categories';

public function getAll()
/**
* @return array
*/
public function getAll(): array
{
return __METHOD__;
return $this->all(self::TABLE);
}

/**
* @param $id
* @return array|false|string[]|void|null
* @return array|false|string[]|null
*/
public function findByID($id)
{
Expand All @@ -20,6 +23,7 @@ public function findByID($id)

/**
* @param $id
* @return array|false|null
*/
public function deleteByID($id)
{
Expand All @@ -28,7 +32,7 @@ public function deleteByID($id)

/**
* @param $data
* @return int|string|void
* @return int|string
*/
public function store($data)
{
Expand All @@ -37,7 +41,7 @@ public function store($data)

/**
* @param $data
* @return int|string|void
* @return int|string
*/
public function updateData($data)
{
Expand All @@ -46,7 +50,7 @@ public function updateData($data)

/**
* @param $attributes
* @return array|void
* @return array|null
*/
public function findByAttribute($attributes)
{
Expand Down
11 changes: 6 additions & 5 deletions app/models/ProductModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProductModel extends BaseModel
const TABLE = 'products';

/**
* @return array|void
* @return array|null
*/
public function getAll()
{
Expand All @@ -14,7 +14,7 @@ public function getAll()

/**
* @param $id
* @return array|false|string[]|void|null
* @return array|false|string[]|null
*/
public function findByID($id)
{
Expand All @@ -23,6 +23,7 @@ public function findByID($id)

/**
* @param $id
* @return array|false|null
*/
public function deleteByID($id)
{
Expand All @@ -31,7 +32,7 @@ public function deleteByID($id)

/**
* @param $data
* @return int|string|void
* @return int|string
*/
public function store($data)
{
Expand All @@ -40,7 +41,7 @@ public function store($data)

/**
* @param $data
* @return int|string|void
* @return int|string
*/
public function updateData($data)
{
Expand All @@ -49,7 +50,7 @@ public function updateData($data)

/**
* @param $attributes
* @return array|void
* @return array|null
*/
public function findByAttribute($attributes)
{
Expand Down
5 changes: 0 additions & 5 deletions config/database.php

This file was deleted.

Loading

0 comments on commit b7dc9c4

Please sign in to comment.